test: fix races in bloomcache tests
License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
Jakub Sztandera committed
Jul 4, 2016 at 20:34 UTC
e92e6662a7e4c24e36fe7470183dcf1e1c3bb26b
1 file changed
+25
-8
blocks/blockstore/bloom_cache_test.go
+25
-8
@@ -2,12 +2,15 @@ package blockstore
2
3
import (
4
"fmt"
5
+ "sync"
6
+ "testing"
7
+ "time"
8
+
9
"github.com/ipfs/go-ipfs/blocks"
10
+
11
ds "gx/ipfs/QmfQzVugPq1w5shWRcLWSeiHF4a2meBX7yVD8Vw7GWJM9o/go-datastore"
12
dsq "gx/ipfs/QmfQzVugPq1w5shWRcLWSeiHF4a2meBX7yVD8Vw7GWJM9o/go-datastore/query"
13
syncds "gx/ipfs/QmfQzVugPq1w5shWRcLWSeiHF4a2meBX7yVD8Vw7GWJM9o/go-datastore/sync"
9
- "testing"
10
- "time"
14
)
15
16
func TestReturnsErrorWhenSizeNegative(t *testing.T) {
@@ -32,7 +35,10 @@ func TestRemoveCacheEntryOnDelete(t *testing.T) {
35
}
36
cachedbs.Put(b)
37
38
+ cd.Lock()
39
writeHitTheDatastore := false
40
+ cd.Unlock()
41
+
42
cd.SetFunc(func() {
43
writeHitTheDatastore = true
44
})
@@ -93,34 +99,45 @@ func TestHasIsBloomCached(t *testing.T) {
99
}
100
101
type callbackDatastore struct {
102
+ sync.Mutex
103
f func()
104
ds ds.Datastore
105
}
106
100
-func (c *callbackDatastore) SetFunc(f func()) { c.f = f }
107
+func (c *callbackDatastore) SetFunc(f func()) {
108
+ c.Lock()
109
+ defer c.Unlock()
110
+ c.f = f
111
+}
112
+
113
+func (c *callbackDatastore) CallF() {
114
+ c.Lock()
115
+ defer c.Unlock()
116
+ c.f()
117
+}
118
119
func (c *callbackDatastore) Put(key ds.Key, value interface{}) (err error) {
103
- c.f()
120
+ c.CallF()
121
return c.ds.Put(key, value)
122
}
123
124
func (c *callbackDatastore) Get(key ds.Key) (value interface{}, err error) {
108
- c.f()
125
+ c.CallF()
126
return c.ds.Get(key)
127
}
128
129
func (c *callbackDatastore) Has(key ds.Key) (exists bool, err error) {
113
- c.f()
130
+ c.CallF()
131
return c.ds.Has(key)
132
}
133
134
func (c *callbackDatastore) Delete(key ds.Key) (err error) {
118
- c.f()
135
+ c.CallF()
136
return c.ds.Delete(key)
137
}
138
139
func (c *callbackDatastore) Query(q dsq.Query) (dsq.Results, error) {
123
- c.f()
140
+ c.CallF()
141
return c.ds.Query(q)
142
}
143