Skip to content

Commit b25883b

Browse files
committed
Drop gomega dependency
1 parent 573b9ef commit b25883b

File tree

5 files changed

+116
-117
lines changed

5 files changed

+116
-117
lines changed

cache_test.go

+46-67
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,26 @@ import (
1010
"time"
1111

1212
"github.com/mgnsk/evcache/v3"
13-
. "github.com/onsi/gomega"
13+
. "github.com/mgnsk/evcache/v3/internal/testing"
1414
)
1515

1616
func TestLoadOrStoreNotExists(t *testing.T) {
17-
g := NewWithT(t)
18-
1917
c := evcache.New[string, int](0)
2018

2119
_, loaded := c.LoadOrStore("key", 0, 1)
22-
g.Expect(loaded).To(BeFalse())
23-
g.Expect(c.Exists("key")).To(BeTrue())
24-
g.Expect(c.Len()).To(Equal(1))
20+
AssertEqual(t, loaded, false)
21+
AssertEqual(t, c.Exists("key"), true)
22+
AssertEqual(t, c.Len(), 1)
2523
}
2624

2725
func TestLoadOrStoreExists(t *testing.T) {
28-
g := NewWithT(t)
29-
3026
c := evcache.New[string, int](0)
3127

3228
c.LoadOrStore("key", 0, 1)
3329

3430
v, loaded := c.LoadOrStore("key", 0, 2)
35-
g.Expect(loaded).To(BeTrue())
36-
g.Expect(v).To(Equal(1))
31+
AssertEqual(t, loaded, true)
32+
AssertEqual(t, v, 1)
3733
}
3834

3935
func TestFetchCallbackBlocks(t *testing.T) {
@@ -54,62 +50,50 @@ func TestFetchCallbackBlocks(t *testing.T) {
5450
<-fetchStarted
5551

5652
t.Run("non-blocking Exists", func(t *testing.T) {
57-
g := NewWithT(t)
58-
59-
g.Expect(c.Exists("key")).To(BeFalse())
53+
AssertEqual(t, c.Exists("key"), false)
6054
})
6155

6256
t.Run("non-blocking Evict", func(t *testing.T) {
63-
g := NewWithT(t)
64-
6557
_, ok := c.Evict("key")
66-
g.Expect(ok).To(BeFalse())
58+
AssertEqual(t, ok, false)
6759
})
6860

6961
t.Run("non-blocking Get", func(t *testing.T) {
70-
g := NewWithT(t)
71-
7262
_, ok := c.Get("key")
73-
g.Expect(ok).To(BeFalse())
63+
AssertEqual(t, ok, false)
7464
})
7565

7666
t.Run("autoexpiry for other keys works", func(t *testing.T) {
77-
g := NewWithT(t)
78-
7967
c.LoadOrStore("key1", time.Millisecond, "value1")
8068

81-
g.Eventually(func() bool {
82-
return c.Exists("key1")
83-
}).Should(BeFalse())
69+
AssertEventuallyTrue(t, func() bool {
70+
return !c.Exists("key1")
71+
})
8472
})
8573

8674
t.Run("non-blocking Range", func(t *testing.T) {
87-
g := NewWithT(t)
88-
8975
c.LoadOrStore("key1", 0, "value1")
9076

9177
n := 0
9278
c.Range(func(key, value string) bool {
9379
if key == "key" {
94-
g.Fail("expected to skip key")
80+
t.Fatal("expected to skip key")
9581
}
9682

9783
v, ok := c.Evict(key)
98-
g.Expect(ok).To(BeTrue())
99-
g.Expect(v).To(Equal(value))
100-
g.Expect(c.Len()).To(Equal(0))
84+
AssertEqual(t, ok, true)
85+
AssertEqual(t, v, value)
86+
AssertEqual(t, c.Len(), 0)
10187

10288
n++
10389
return true
10490
})
10591

106-
g.Expect(n).To(Equal(1))
92+
AssertEqual(t, n, 1)
10793
})
10894
}
10995

11096
func TestFetchCallbackPanic(t *testing.T) {
111-
g := NewWithT(t)
112-
11397
c := evcache.New[string, string](0)
11498

11599
func() {
@@ -127,14 +111,12 @@ func TestFetchCallbackPanic(t *testing.T) {
127111
return "new value", nil
128112
})
129113

130-
g.Expect(err).NotTo(HaveOccurred())
131-
g.Expect(v).To(Equal("new value"))
114+
AssertSuccess(t, err)
115+
AssertEqual(t, v, "new value")
132116
}
133117

134118
func TestConcurrentFetch(t *testing.T) {
135119
t.Run("returns error", func(t *testing.T) {
136-
g := NewWithT(t)
137-
138120
c := evcache.New[string, string](0)
139121

140122
errCh := make(chan error)
@@ -154,13 +136,11 @@ func TestConcurrentFetch(t *testing.T) {
154136
return "value", nil
155137
})
156138

157-
g.Expect(err).NotTo(HaveOccurred())
158-
g.Expect(v).To(Equal("value"))
139+
AssertSuccess(t, err)
140+
AssertEqual(t, v, "value")
159141
})
160142

161143
t.Run("returns value", func(t *testing.T) {
162-
g := NewWithT(t)
163-
164144
c := evcache.New[string, string](0)
165145

166146
valueCh := make(chan string)
@@ -180,40 +160,37 @@ func TestConcurrentFetch(t *testing.T) {
180160
return "value1", nil
181161
})
182162

183-
g.Expect(err).NotTo(HaveOccurred())
184-
g.Expect(v).To(Equal("value"))
163+
AssertSuccess(t, err)
164+
AssertEqual(t, v, "value")
185165
})
186166
}
187167

188168
func TestEvict(t *testing.T) {
189-
g := NewWithT(t)
190-
191169
c := evcache.New[string, string](0)
192170

193171
c.LoadOrStore("key", 0, "value")
194172

195173
v, ok := c.Evict("key")
196-
g.Expect(ok).To(BeTrue())
197-
g.Expect(v).To(Equal("value"))
198-
g.Expect(c.Exists("key")).To(BeFalse())
174+
175+
AssertEqual(t, ok, true)
176+
AssertEqual(t, v, "value")
177+
AssertEqual(t, c.Exists("key"), false)
199178
}
200179

201180
func TestOverflow(t *testing.T) {
202-
g := NewWithT(t)
203-
204181
capacity := 100
205182
c := evcache.New[int, int](capacity)
206183

207184
for i := 0; i < 2*capacity; i++ {
208185
c.LoadOrStore(i, 0, 0)
209186
}
210187

211-
g.Eventually(c.Len).Should(Equal(capacity))
188+
AssertEventuallyTrue(t, func() bool {
189+
return c.Len() == capacity
190+
})
212191
}
213192

214193
func TestExpire(t *testing.T) {
215-
g := NewWithT(t)
216-
217194
c := evcache.New[int, int](0)
218195

219196
n := 10
@@ -223,33 +200,35 @@ func TestExpire(t *testing.T) {
223200
_, _ = c.LoadOrStore(i, d, 0)
224201
}
225202

226-
g.Eventually(c.Len).Should(BeZero())
203+
AssertEventuallyTrue(t, func() bool {
204+
return c.Len() == 0
205+
})
227206
}
228207

229208
func TestExpireEdgeCase(t *testing.T) {
230-
g := NewWithT(t)
231-
232209
c := evcache.New[int, *string](0)
233210

234211
v1 := new(string)
235212

236213
c.LoadOrStore(0, 10*time.Millisecond, v1)
237214

238215
// Wait until v1 expires.
239-
g.Eventually(c.Len).Should(BeZero())
216+
AssertEventuallyTrue(t, func() bool {
217+
return c.Len() == 0
218+
})
240219

241220
// Assert that after v1 expires, v2 with a longer TTL than v1, can expire,
242221
// specifically that runGC() resets earliestExpireAt to zero,
243222
// so that LoadOrStore schedules the GC loop.
244223
v2 := new(string)
245224
c.LoadOrStore(1, time.Second, v2)
246225

247-
g.Eventually(c.Len, 2*time.Second).Should(BeZero())
226+
AssertEventuallyTrue(t, func() bool {
227+
return c.Len() == 0
228+
}, 2*time.Second)
248229
}
249230

250231
func TestOverflowEvictionOrdering(t *testing.T) {
251-
g := NewWithT(t)
252-
253232
capacity := 10
254233
c := evcache.New[int, *string](capacity)
255234
evictedKeys := make(chan int)
@@ -263,19 +242,19 @@ func TestOverflowEvictionOrdering(t *testing.T) {
263242
})
264243

265244
_, loaded := c.LoadOrStore(i, 0, value)
266-
g.Expect(loaded).To(BeFalse())
267-
g.Expect(c.Len()).To(Equal(i))
245+
AssertEqual(t, loaded, false)
246+
AssertEqual(t, c.Len(), i)
268247
}
269248

270249
// Overflow the cache and catch evicted keys.
271250
var keys []int
272251
for i := capacity + 1; i <= 2*capacity; i++ {
273252
_, loaded := c.LoadOrStore(i, 0, nil)
274-
g.Expect(loaded).To(BeFalse())
253+
AssertEqual(t, loaded, false)
275254

276255
// Run the GC until the value is garbage collected
277256
// and the value finalizer runs.
278-
g.Eventually(func() bool {
257+
AssertEventuallyTrue(t, func() bool {
279258
runtime.GC()
280259
select {
281260
case key := <-evictedKeys:
@@ -284,11 +263,11 @@ func TestOverflowEvictionOrdering(t *testing.T) {
284263
default:
285264
return false
286265
}
287-
}).Should(BeTrue())
266+
})
288267
}
289268

290-
g.Expect(keys).To(HaveLen(capacity))
291-
g.Expect(sort.IntsAreSorted(keys)).To(BeTrue())
269+
AssertEqual(t, len(keys), capacity)
270+
AssertEqual(t, sort.IntsAreSorted(keys), true)
292271
}
293272

294273
func BenchmarkFetchAndEvictParallel(b *testing.B) {

go.mod

+1-14
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,4 @@ module github.com/mgnsk/evcache/v3
22

33
go 1.19
44

5-
require (
6-
github.com/mgnsk/ringlist v0.0.0-20231016175938-072bd9da3594
7-
github.com/onsi/gomega v1.29.0
8-
)
9-
10-
require (
11-
github.com/google/go-cmp v0.6.0 // indirect
12-
github.com/kr/pretty v0.1.0 // indirect
13-
golang.org/x/net v0.17.0 // indirect
14-
golang.org/x/text v0.13.0 // indirect
15-
golang.org/x/tools v0.13.0 // indirect
16-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
17-
gopkg.in/yaml.v3 v3.0.1 // indirect
18-
)
5+
require github.com/mgnsk/ringlist v0.0.0-20231025190742-1f621b925911

go.sum

+2-27
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,2 @@
1-
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
2-
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
3-
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
4-
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
5-
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
6-
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
7-
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
8-
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
9-
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
10-
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
11-
github.com/mgnsk/ringlist v0.0.0-20231016175938-072bd9da3594 h1:jUaUheJ+LrYgnsp2oBz1wIJcIJk8aQtZLWIHbSlq754=
12-
github.com/mgnsk/ringlist v0.0.0-20231016175938-072bd9da3594/go.mod h1:bVpxwIf/UWlX72d9usjtuZoNRgFLdTL7iABnOpEZubk=
13-
github.com/onsi/ginkgo/v2 v2.13.0 h1:0jY9lJquiL8fcf3M4LAXN5aMlS/b2BV86HFFPCPMgE4=
14-
github.com/onsi/gomega v1.29.0 h1:KIA/t2t5UBzoirT4H9tsML45GEbo3ouUnBHsCfD2tVg=
15-
github.com/onsi/gomega v1.29.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
16-
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
17-
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
18-
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
19-
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
20-
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
21-
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
22-
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
23-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
24-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
25-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
26-
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
27-
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1+
github.com/mgnsk/ringlist v0.0.0-20231025190742-1f621b925911 h1:/RUn/noOFPYcnnlsXmdmHSvKBp5gwQ5OnlNX9bRgeZE=
2+
github.com/mgnsk/ringlist v0.0.0-20231025190742-1f621b925911/go.mod h1:fyW9CbxFtYtWazjV96eJt4r+B2ABDRu3/rXQOxxTjLE=

internal/backend/backend_test.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,21 @@ import (
55
"time"
66

77
"github.com/mgnsk/evcache/v3/internal/backend"
8-
. "github.com/onsi/gomega"
8+
. "github.com/mgnsk/evcache/v3/internal/testing"
99
)
1010

1111
func TestMapShrinkUninitializedRecords(t *testing.T) {
1212
t.Run("realloc", func(t *testing.T) {
13-
g := NewWithT(t)
14-
1513
b := newBackend(size - 1)
16-
g.Expect(b.Len()).To(Equal(size - 1))
17-
g.Expect(getInitializedMapLen(b)).To(Equal(size - 1))
14+
AssertEqual(t, b.Len(), size-1)
15+
AssertEqual(t, getInitializedMapLen(b), size-1)
1816

1917
// Store uninitialized record.
2018
elem := b.Reserve()
2119
b.LoadOrStore(size-1, elem)
2220

23-
g.Expect(b.Len()).To(Equal(size-1), "list len only initialized elements")
24-
g.Expect(getInitializedMapLen(b)).To(Equal(size-1), "map len only initialized elements")
21+
AssertEqual(t, b.Len(), size-1)
22+
AssertEqual(t, b.Len(), size-1)
2523

2624
// Evict half of records.
2725
for i := 0; i < size/2; i++ {
@@ -33,8 +31,8 @@ func TestMapShrinkUninitializedRecords(t *testing.T) {
3331
// Initialize the record.
3432
b.Initialize(size-1, 0, 0)
3533

36-
g.Expect(b.Len()).To(Equal((size / 2)), "list len only initialized elements")
37-
g.Expect(getInitializedMapLen(b)).To(Equal((size / 2)), "map len only initialized elements")
34+
AssertEqual(t, b.Len(), size/2)
35+
AssertEqual(t, b.Len(), size/2)
3836
})
3937
}
4038

0 commit comments

Comments
 (0)