Change the test from being Has based to Put based
License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
Jakub Sztandera committed
Oct 10, 2016 at 15:52 UTC
7e4b74bf5ec7e453185084b1567a100b89cac060
1 file changed
+24
-19
blockservice/blockservice_test.go
+24
-19
@@ -3,42 +3,47 @@ package blockservice
3
import (
4
"testing"
5
6
+ "github.com/ipfs/go-ipfs/blocks"
7
"github.com/ipfs/go-ipfs/blocks/blockstore"
8
butil "github.com/ipfs/go-ipfs/blocks/blocksutil"
9
offline "github.com/ipfs/go-ipfs/exchange/offline"
10
10
- cid "gx/ipfs/QmakyCk6Vnn16WEKjbkxieZmM2YLTzkFWizbmGowoYPjro/go-cid"
11
ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
12
dssync "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore/sync"
13
)
14
15
func TestWriteThroughWorks(t *testing.T) {
16
- dstore := dssync.MutexWrap(ds.NewMapDatastore())
17
- bstore := HasFailingBlockstore{
18
- blockstore.NewBlockstore(dstore),
19
- t,
20
- true,
16
+ bstore := &PutCountingBlockstore{
17
+ blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore())),
18
+ 0,
19
}
22
- exch := offline.Exchange(bstore)
20
+ bstore2 := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
21
+ exch := offline.Exchange(bstore2)
22
bserv := NewWriteThrough(bstore, exch)
23
bgen := butil.NewBlockGenerator()
24
26
- bserv.AddBlock(bgen.Next())
25
+ block := bgen.Next()
26
+
27
+ t.Logf("PutCounter: %d", bstore.PutCounter)
28
+ bserv.AddBlock(block)
29
+ if bstore.PutCounter != 1 {
30
+ t.Fatalf("expected just one Put call, have: %d", bstore.PutCounter)
31
+ }
32
+
33
+ bserv.AddBlock(block)
34
+ if bstore.PutCounter != 2 {
35
+ t.Fatal("Put should have called again, should be 2 is: %d", bstore.PutCounter)
36
+ }
37
}
38
29
-var _ blockstore.GCBlockstore = (*HasFailingBlockstore)(nil)
39
+var _ blockstore.GCBlockstore = (*PutCountingBlockstore)(nil)
40
31
-type HasFailingBlockstore struct {
41
+type PutCountingBlockstore struct {
42
blockstore.GCBlockstore
33
- t *testing.T
34
- Fail bool
43
+ PutCounter int
44
}
45
37
-func (bs HasFailingBlockstore) Has(k *cid.Cid) (bool, error) {
38
- if bs.Fail {
39
- bs.t.Fatal("Has shouldn't be called")
40
- return false, nil
41
- }
42
- return bs.GCBlockstore.Has(k)
43
-
46
+func (bs *PutCountingBlockstore) Put(block blocks.Block) error {
47
+ bs.PutCounter++
48
+ return bs.GCBlockstore.Put(block)
49
}