Skip to content

Commit

Permalink
Drop gomega dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
mgnsk committed Oct 25, 2023
1 parent 573b9ef commit b25883b
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 117 deletions.
113 changes: 46 additions & 67 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,26 @@ import (
"time"

"github.com/mgnsk/evcache/v3"
. "github.com/onsi/gomega"
. "github.com/mgnsk/evcache/v3/internal/testing"
)

func TestLoadOrStoreNotExists(t *testing.T) {
g := NewWithT(t)

c := evcache.New[string, int](0)

_, loaded := c.LoadOrStore("key", 0, 1)
g.Expect(loaded).To(BeFalse())
g.Expect(c.Exists("key")).To(BeTrue())
g.Expect(c.Len()).To(Equal(1))
AssertEqual(t, loaded, false)
AssertEqual(t, c.Exists("key"), true)
AssertEqual(t, c.Len(), 1)
}

func TestLoadOrStoreExists(t *testing.T) {
g := NewWithT(t)

c := evcache.New[string, int](0)

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

v, loaded := c.LoadOrStore("key", 0, 2)
g.Expect(loaded).To(BeTrue())
g.Expect(v).To(Equal(1))
AssertEqual(t, loaded, true)
AssertEqual(t, v, 1)
}

func TestFetchCallbackBlocks(t *testing.T) {
Expand All @@ -54,62 +50,50 @@ func TestFetchCallbackBlocks(t *testing.T) {
<-fetchStarted

t.Run("non-blocking Exists", func(t *testing.T) {
g := NewWithT(t)

g.Expect(c.Exists("key")).To(BeFalse())
AssertEqual(t, c.Exists("key"), false)
})

t.Run("non-blocking Evict", func(t *testing.T) {
g := NewWithT(t)

_, ok := c.Evict("key")
g.Expect(ok).To(BeFalse())
AssertEqual(t, ok, false)
})

t.Run("non-blocking Get", func(t *testing.T) {
g := NewWithT(t)

_, ok := c.Get("key")
g.Expect(ok).To(BeFalse())
AssertEqual(t, ok, false)
})

t.Run("autoexpiry for other keys works", func(t *testing.T) {
g := NewWithT(t)

c.LoadOrStore("key1", time.Millisecond, "value1")

g.Eventually(func() bool {
return c.Exists("key1")
}).Should(BeFalse())
AssertEventuallyTrue(t, func() bool {
return !c.Exists("key1")
})
})

t.Run("non-blocking Range", func(t *testing.T) {
g := NewWithT(t)

c.LoadOrStore("key1", 0, "value1")

n := 0
c.Range(func(key, value string) bool {
if key == "key" {
g.Fail("expected to skip key")
t.Fatal("expected to skip key")
}

v, ok := c.Evict(key)
g.Expect(ok).To(BeTrue())
g.Expect(v).To(Equal(value))
g.Expect(c.Len()).To(Equal(0))
AssertEqual(t, ok, true)
AssertEqual(t, v, value)
AssertEqual(t, c.Len(), 0)

n++
return true
})

g.Expect(n).To(Equal(1))
AssertEqual(t, n, 1)
})
}

func TestFetchCallbackPanic(t *testing.T) {
g := NewWithT(t)

c := evcache.New[string, string](0)

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

g.Expect(err).NotTo(HaveOccurred())
g.Expect(v).To(Equal("new value"))
AssertSuccess(t, err)
AssertEqual(t, v, "new value")
}

func TestConcurrentFetch(t *testing.T) {
t.Run("returns error", func(t *testing.T) {
g := NewWithT(t)

c := evcache.New[string, string](0)

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

g.Expect(err).NotTo(HaveOccurred())
g.Expect(v).To(Equal("value"))
AssertSuccess(t, err)
AssertEqual(t, v, "value")
})

t.Run("returns value", func(t *testing.T) {
g := NewWithT(t)

c := evcache.New[string, string](0)

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

g.Expect(err).NotTo(HaveOccurred())
g.Expect(v).To(Equal("value"))
AssertSuccess(t, err)
AssertEqual(t, v, "value")
})
}

func TestEvict(t *testing.T) {
g := NewWithT(t)

c := evcache.New[string, string](0)

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

v, ok := c.Evict("key")
g.Expect(ok).To(BeTrue())
g.Expect(v).To(Equal("value"))
g.Expect(c.Exists("key")).To(BeFalse())

AssertEqual(t, ok, true)
AssertEqual(t, v, "value")
AssertEqual(t, c.Exists("key"), false)
}

func TestOverflow(t *testing.T) {
g := NewWithT(t)

capacity := 100
c := evcache.New[int, int](capacity)

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

g.Eventually(c.Len).Should(Equal(capacity))
AssertEventuallyTrue(t, func() bool {
return c.Len() == capacity
})
}

func TestExpire(t *testing.T) {
g := NewWithT(t)

c := evcache.New[int, int](0)

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

g.Eventually(c.Len).Should(BeZero())
AssertEventuallyTrue(t, func() bool {
return c.Len() == 0
})
}

func TestExpireEdgeCase(t *testing.T) {
g := NewWithT(t)

c := evcache.New[int, *string](0)

v1 := new(string)

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

// Wait until v1 expires.
g.Eventually(c.Len).Should(BeZero())
AssertEventuallyTrue(t, func() bool {
return c.Len() == 0
})

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

g.Eventually(c.Len, 2*time.Second).Should(BeZero())
AssertEventuallyTrue(t, func() bool {
return c.Len() == 0
}, 2*time.Second)
}

func TestOverflowEvictionOrdering(t *testing.T) {
g := NewWithT(t)

capacity := 10
c := evcache.New[int, *string](capacity)
evictedKeys := make(chan int)
Expand All @@ -263,19 +242,19 @@ func TestOverflowEvictionOrdering(t *testing.T) {
})

_, loaded := c.LoadOrStore(i, 0, value)
g.Expect(loaded).To(BeFalse())
g.Expect(c.Len()).To(Equal(i))
AssertEqual(t, loaded, false)
AssertEqual(t, c.Len(), i)
}

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

// Run the GC until the value is garbage collected
// and the value finalizer runs.
g.Eventually(func() bool {
AssertEventuallyTrue(t, func() bool {
runtime.GC()
select {
case key := <-evictedKeys:
Expand All @@ -284,11 +263,11 @@ func TestOverflowEvictionOrdering(t *testing.T) {
default:
return false
}
}).Should(BeTrue())
})
}

g.Expect(keys).To(HaveLen(capacity))
g.Expect(sort.IntsAreSorted(keys)).To(BeTrue())
AssertEqual(t, len(keys), capacity)
AssertEqual(t, sort.IntsAreSorted(keys), true)
}

func BenchmarkFetchAndEvictParallel(b *testing.B) {
Expand Down
15 changes: 1 addition & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,4 @@ module github.com/mgnsk/evcache/v3

go 1.19

require (
github.com/mgnsk/ringlist v0.0.0-20231016175938-072bd9da3594
github.com/onsi/gomega v1.29.0
)

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

"github.com/mgnsk/evcache/v3/internal/backend"
. "github.com/onsi/gomega"
. "github.com/mgnsk/evcache/v3/internal/testing"
)

func TestMapShrinkUninitializedRecords(t *testing.T) {
t.Run("realloc", func(t *testing.T) {
g := NewWithT(t)

b := newBackend(size - 1)
g.Expect(b.Len()).To(Equal(size - 1))
g.Expect(getInitializedMapLen(b)).To(Equal(size - 1))
AssertEqual(t, b.Len(), size-1)
AssertEqual(t, getInitializedMapLen(b), size-1)

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

g.Expect(b.Len()).To(Equal(size-1), "list len only initialized elements")
g.Expect(getInitializedMapLen(b)).To(Equal(size-1), "map len only initialized elements")
AssertEqual(t, b.Len(), size-1)
AssertEqual(t, b.Len(), size-1)

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

g.Expect(b.Len()).To(Equal((size / 2)), "list len only initialized elements")
g.Expect(getInitializedMapLen(b)).To(Equal((size / 2)), "map len only initialized elements")
AssertEqual(t, b.Len(), size/2)
AssertEqual(t, b.Len(), size/2)
})
}

Expand Down
Loading

0 comments on commit b25883b

Please sign in to comment.