From 839a17bedbbccefac8e536dc2ee61397c80d819f Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Sun, 16 Aug 2020 19:07:52 +0800 Subject: [PATCH] Remove impossible race conditions from test This makes the output of go test --race less noisy. --- cache_test.go | 7 ++++--- layeredcache_test.go | 13 +++++++------ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/cache_test.go b/cache_test.go index c017bc6..2578b92 100644 --- a/cache_test.go +++ b/cache_test.go @@ -2,6 +2,7 @@ package ccache import ( "strconv" + "sync/atomic" "testing" "time" @@ -81,10 +82,10 @@ func (_ CacheTests) DeletesAFunc() { } func (_ CacheTests) OnDeleteCallbackCalled() { - onDeleteFnCalled := false + onDeleteFnCalled := int32(0) onDeleteFn := func(item *Item) { if item.key == "spice" { - onDeleteFnCalled = true + atomic.AddInt32(&onDeleteFnCalled, 1) } } @@ -98,7 +99,7 @@ func (_ CacheTests) OnDeleteCallbackCalled() { Expect(cache.Get("spice")).To.Equal(nil) Expect(cache.Get("worm").Value()).To.Equal("sand") - Expect(onDeleteFnCalled).To.Equal(true) + Expect(atomic.LoadInt32(&onDeleteFnCalled)).To.Eql(1) } func (_ CacheTests) FetchesExpiredItems() { diff --git a/layeredcache_test.go b/layeredcache_test.go index 1338be0..f1f7a65 100644 --- a/layeredcache_test.go +++ b/layeredcache_test.go @@ -2,6 +2,7 @@ package ccache import ( "strconv" + "sync/atomic" "testing" "time" @@ -125,12 +126,10 @@ func (_ *LayeredCacheTests) DeletesAFunc() { } func (_ *LayeredCacheTests) OnDeleteCallbackCalled() { - - onDeleteFnCalled := false + onDeleteFnCalled := int32(0) onDeleteFn := func(item *Item) { - if item.group == "spice" && item.key == "flow" { - onDeleteFnCalled = true + atomic.AddInt32(&onDeleteFnCalled, 1) } } @@ -148,7 +147,7 @@ func (_ *LayeredCacheTests) OnDeleteCallbackCalled() { Expect(cache.Get("spice", "worm")).To.Equal(nil) Expect(cache.Get("leto", "sister").Value()).To.Equal("ghanima") - Expect(onDeleteFnCalled).To.Equal(true) + Expect(atomic.LoadInt32(&onDeleteFnCalled)).To.Eql(1) } func (_ *LayeredCacheTests) DeletesALayer() { @@ -263,7 +262,9 @@ func (_ LayeredCacheTests) ResizeOnTheFly() { } func newLayered() *LayeredCache { - return Layered(Configure()) + c := Layered(Configure()) + c.Clear() + return c } func (_ LayeredCacheTests) RemovesOldestItemWhenFullBySizer() {