Commit 865b8327 authored by Jakub Sztandera's avatar Jakub Sztandera Committed by Kevin Atkinson

Change the test from being Has based to Put based

License: MIT
Signed-off-by: default avatarJakub Sztandera <kubuxu@protonmail.ch>
parent 06c5dbf8
......@@ -3,42 +3,47 @@ package blockservice
import (
"testing"
"github.com/ipfs/go-ipfs/blocks"
"github.com/ipfs/go-ipfs/blocks/blockstore"
butil "github.com/ipfs/go-ipfs/blocks/blocksutil"
offline "github.com/ipfs/go-ipfs/exchange/offline"
cid "gx/ipfs/QmakyCk6Vnn16WEKjbkxieZmM2YLTzkFWizbmGowoYPjro/go-cid"
ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
dssync "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore/sync"
)
func TestWriteThroughWorks(t *testing.T) {
dstore := dssync.MutexWrap(ds.NewMapDatastore())
bstore := HasFailingBlockstore{
blockstore.NewBlockstore(dstore),
t,
true,
bstore := &PutCountingBlockstore{
blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore())),
0,
}
exch := offline.Exchange(bstore)
bstore2 := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
exch := offline.Exchange(bstore2)
bserv := NewWriteThrough(bstore, exch)
bgen := butil.NewBlockGenerator()
bserv.AddBlock(bgen.Next())
block := bgen.Next()
t.Logf("PutCounter: %d", bstore.PutCounter)
bserv.AddBlock(block)
if bstore.PutCounter != 1 {
t.Fatalf("expected just one Put call, have: %d", bstore.PutCounter)
}
bserv.AddBlock(block)
if bstore.PutCounter != 2 {
t.Fatal("Put should have called again, should be 2 is: %d", bstore.PutCounter)
}
}
var _ blockstore.GCBlockstore = (*HasFailingBlockstore)(nil)
var _ blockstore.GCBlockstore = (*PutCountingBlockstore)(nil)
type HasFailingBlockstore struct {
type PutCountingBlockstore struct {
blockstore.GCBlockstore
t *testing.T
Fail bool
PutCounter int
}
func (bs HasFailingBlockstore) Has(k *cid.Cid) (bool, error) {
if bs.Fail {
bs.t.Fatal("Has shouldn't be called")
return false, nil
}
return bs.GCBlockstore.Has(k)
func (bs *PutCountingBlockstore) Put(block blocks.Block) error {
bs.PutCounter++
return bs.GCBlockstore.Put(block)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment