From e92e6662a7e4c24e36fe7470183dcf1e1c3bb26b Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Mon, 4 Jul 2016 20:34:07 +0200 Subject: [PATCH] test: fix races in bloomcache tests License: MIT Signed-off-by: Jakub Sztandera --- blocks/blockstore/bloom_cache_test.go | 33 ++++++++++++++++++++------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/blocks/blockstore/bloom_cache_test.go b/blocks/blockstore/bloom_cache_test.go index 6c9f13cdc..3b8bb8b91 100644 --- a/blocks/blockstore/bloom_cache_test.go +++ b/blocks/blockstore/bloom_cache_test.go @@ -2,12 +2,15 @@ package blockstore import ( "fmt" + "sync" + "testing" + "time" + "github.com/ipfs/go-ipfs/blocks" + ds "gx/ipfs/QmfQzVugPq1w5shWRcLWSeiHF4a2meBX7yVD8Vw7GWJM9o/go-datastore" dsq "gx/ipfs/QmfQzVugPq1w5shWRcLWSeiHF4a2meBX7yVD8Vw7GWJM9o/go-datastore/query" syncds "gx/ipfs/QmfQzVugPq1w5shWRcLWSeiHF4a2meBX7yVD8Vw7GWJM9o/go-datastore/sync" - "testing" - "time" ) func TestReturnsErrorWhenSizeNegative(t *testing.T) { @@ -32,7 +35,10 @@ func TestRemoveCacheEntryOnDelete(t *testing.T) { } cachedbs.Put(b) + cd.Lock() writeHitTheDatastore := false + cd.Unlock() + cd.SetFunc(func() { writeHitTheDatastore = true }) @@ -93,34 +99,45 @@ func TestHasIsBloomCached(t *testing.T) { } type callbackDatastore struct { + sync.Mutex f func() ds ds.Datastore } -func (c *callbackDatastore) SetFunc(f func()) { c.f = f } +func (c *callbackDatastore) SetFunc(f func()) { + c.Lock() + defer c.Unlock() + c.f = f +} + +func (c *callbackDatastore) CallF() { + c.Lock() + defer c.Unlock() + c.f() +} func (c *callbackDatastore) Put(key ds.Key, value interface{}) (err error) { - c.f() + c.CallF() return c.ds.Put(key, value) } func (c *callbackDatastore) Get(key ds.Key) (value interface{}, err error) { - c.f() + c.CallF() return c.ds.Get(key) } func (c *callbackDatastore) Has(key ds.Key) (exists bool, err error) { - c.f() + c.CallF() return c.ds.Has(key) } func (c *callbackDatastore) Delete(key ds.Key) (err error) { - c.f() + c.CallF() return c.ds.Delete(key) } func (c *callbackDatastore) Query(q dsq.Query) (dsq.Results, error) { - c.f() + c.CallF() return c.ds.Query(q) }