Remove impossible race conditions from test

This makes the output of go test --race less noisy.
This commit is contained in:
Karl Seguin 2020-08-16 19:07:52 +08:00
parent 0dbf3f125f
commit 839a17bedb
2 changed files with 11 additions and 9 deletions

View file

@ -2,6 +2,7 @@ package ccache
import ( import (
"strconv" "strconv"
"sync/atomic"
"testing" "testing"
"time" "time"
@ -81,10 +82,10 @@ func (_ CacheTests) DeletesAFunc() {
} }
func (_ CacheTests) OnDeleteCallbackCalled() { func (_ CacheTests) OnDeleteCallbackCalled() {
onDeleteFnCalled := false onDeleteFnCalled := int32(0)
onDeleteFn := func(item *Item) { onDeleteFn := func(item *Item) {
if item.key == "spice" { 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("spice")).To.Equal(nil)
Expect(cache.Get("worm").Value()).To.Equal("sand") Expect(cache.Get("worm").Value()).To.Equal("sand")
Expect(onDeleteFnCalled).To.Equal(true) Expect(atomic.LoadInt32(&onDeleteFnCalled)).To.Eql(1)
} }
func (_ CacheTests) FetchesExpiredItems() { func (_ CacheTests) FetchesExpiredItems() {

View file

@ -2,6 +2,7 @@ package ccache
import ( import (
"strconv" "strconv"
"sync/atomic"
"testing" "testing"
"time" "time"
@ -125,12 +126,10 @@ func (_ *LayeredCacheTests) DeletesAFunc() {
} }
func (_ *LayeredCacheTests) OnDeleteCallbackCalled() { func (_ *LayeredCacheTests) OnDeleteCallbackCalled() {
onDeleteFnCalled := int32(0)
onDeleteFnCalled := false
onDeleteFn := func(item *Item) { onDeleteFn := func(item *Item) {
if item.group == "spice" && item.key == "flow" { 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("spice", "worm")).To.Equal(nil)
Expect(cache.Get("leto", "sister").Value()).To.Equal("ghanima") Expect(cache.Get("leto", "sister").Value()).To.Equal("ghanima")
Expect(onDeleteFnCalled).To.Equal(true) Expect(atomic.LoadInt32(&onDeleteFnCalled)).To.Eql(1)
} }
func (_ *LayeredCacheTests) DeletesALayer() { func (_ *LayeredCacheTests) DeletesALayer() {
@ -263,7 +262,9 @@ func (_ LayeredCacheTests) ResizeOnTheFly() {
} }
func newLayered() *LayeredCache { func newLayered() *LayeredCache {
return Layered(Configure()) c := Layered(Configure())
c.Clear()
return c
} }
func (_ LayeredCacheTests) RemovesOldestItemWhenFullBySizer() { func (_ LayeredCacheTests) RemovesOldestItemWhenFullBySizer() {