-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathstmtcache_test.go
190 lines (162 loc) · 4.39 KB
/
stmtcache_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package godb
import (
"strconv"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestNewCache(t *testing.T) {
Convey("New create a new cache", t, func() {
c := newStmtCache()
Convey("With default settings", func() {
So(c.IsEnabled(), ShouldBeTrue)
So(c.GetSize(), ShouldEqual, DefaultStmtCacheSize)
})
Convey("With zero as LRU counter", func() {
So(c.lastUse, ShouldBeZeroValue)
})
Convey("With a valid content map", func() {
So(c.content, ShouldNotBeNil)
})
})
}
func TestEnabling(t *testing.T) {
Convey("Given a cache", t, func() {
c := newStmtCache()
Convey("Enable enables the cache", func() {
c.isEnabled = false
c.Enable()
So(c.isEnabled, ShouldBeTrue)
So(c.IsEnabled(), ShouldBeTrue)
})
Convey("Disable disables the cache", func() {
c.isEnabled = true
c.Disable()
So(c.isEnabled, ShouldBeFalse)
So(c.IsEnabled(), ShouldBeFalse)
})
})
}
func TestSize(t *testing.T) {
Convey("Given a cache", t, func() {
c := newStmtCache()
Convey("SetSize changes the cache size", func() {
newSize := 2 * DefaultStmtCacheSize
c.SetSize(newSize)
So(c.maxSize, ShouldEqual, newSize)
})
Convey("SetSize reduces the cache size and close statements if needed", func() {
db := fixturesSetup(t)
defer db.Close()
c.SetSize(4)
query := "select * from dummies where id="
for i := 0; i < 4; i++ {
iQuery := query + strconv.Itoa(i)
stmt, _ := db.CurrentDB().Prepare(iQuery)
c.add(iQuery, stmt)
}
c.SetSize(2)
So(len(c.content), ShouldEqual, 2)
})
Convey("GetSize returns the cache size", func() {
So(c.GetSize(), ShouldEqual, c.maxSize)
})
})
}
func TestAdd(t *testing.T) {
Convey("Given a cache", t, func() {
c := newStmtCache()
db := fixturesSetup(t)
defer db.Close()
Convey("add adds a stmt into the cache", func() {
query := "select * from dummies"
stmt, _ := db.CurrentDB().Prepare(query)
c.add(query, stmt)
So(len(c.content), ShouldEqual, 1)
})
Convey("add keeps cache size at it's max allowed value", func() {
c.SetSize(2)
query := "select * from dummies where id="
for i := 1; i <= 3; i++ {
iQuery := query + strconv.Itoa(i)
stmt, _ := db.CurrentDB().Prepare(iQuery)
c.add(iQuery, stmt)
}
So(len(c.content), ShouldEqual, 2)
})
})
}
func TestRemoveLeastRecentlyUsed(t *testing.T) {
Convey("Given a cache", t, func() {
c := newStmtCache()
db := fixturesSetup(t)
defer db.Close()
Convey("removeLeastRecentlyUsed remove the least recently used item from the cache", func() {
query := "select * from dummies where id="
// add fixtures
for i := 0; i < 10; i++ {
iQuery := query + strconv.Itoa(i)
stmt, _ := db.CurrentDB().Prepare(iQuery)
c.add(iQuery, stmt)
}
// ensure stmt with 'id=3' is the looser
for i := 0; i < 10; i++ {
if i != 3 {
iQuery := query + strconv.Itoa(i)
_ = c.get(iQuery)
}
}
c.removeLeastRecentlyUsed()
removedQuery := query + strconv.Itoa(3)
So(c.get(removedQuery), ShouldBeNil)
})
})
}
func TestGet(t *testing.T) {
Convey("Given a cache", t, func() {
c := newStmtCache()
db := fixturesSetup(t)
defer db.Close()
Convey("get returns the stmt from the cache corresponding to the given query", func() {
query := "select * from dummies"
stmt, _ := db.CurrentDB().Prepare(query)
c.add(query, stmt)
So(c.get(query), ShouldEqual, stmt)
})
Convey("get returns nil if the query was not found", func() {
query := "select * from dummies"
So(c.get(query), ShouldBeNil)
})
})
}
func TestClear(t *testing.T) {
Convey("Given a cache", t, func() {
c := newStmtCache()
db := fixturesSetup(t)
defer db.Close()
Convey("Clears close the stmt and remove all entries from the cache", func() {
query := "select * from dummies"
stmt, _ := db.CurrentDB().Prepare(query)
c.add(query, stmt)
c.Clear()
So(len(c.content), ShouldEqual, 0)
_, err := stmt.Query()
So(err, ShouldNotBeNil)
})
})
}
func TestClearWithoutClosingStmt(t *testing.T) {
Convey("Given a cache", t, func() {
c := newStmtCache()
db := fixturesSetup(t)
defer db.Close()
Convey("Clears remove all entries from the cache but does not close stmt", func() {
query := "select * from dummies"
stmt, _ := db.CurrentDB().Prepare(query)
c.add(query, stmt)
c.clearWithoutClosingStmt()
So(len(c.content), ShouldEqual, 0)
_, err := stmt.Query()
So(err, ShouldBeNil)
})
})
}