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 ...@@ -3,42 +3,47 @@ package blockservice
import ( import (
"testing" "testing"
"github.com/ipfs/go-ipfs/blocks"
"github.com/ipfs/go-ipfs/blocks/blockstore" "github.com/ipfs/go-ipfs/blocks/blockstore"
butil "github.com/ipfs/go-ipfs/blocks/blocksutil" butil "github.com/ipfs/go-ipfs/blocks/blocksutil"
offline "github.com/ipfs/go-ipfs/exchange/offline" offline "github.com/ipfs/go-ipfs/exchange/offline"
cid "gx/ipfs/QmakyCk6Vnn16WEKjbkxieZmM2YLTzkFWizbmGowoYPjro/go-cid"
ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore" ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
dssync "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore/sync" dssync "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore/sync"
) )
func TestWriteThroughWorks(t *testing.T) { func TestWriteThroughWorks(t *testing.T) {
dstore := dssync.MutexWrap(ds.NewMapDatastore()) bstore := &PutCountingBlockstore{
bstore := HasFailingBlockstore{ blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore())),
blockstore.NewBlockstore(dstore), 0,
t,
true,
} }
exch := offline.Exchange(bstore) bstore2 := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
exch := offline.Exchange(bstore2)
bserv := NewWriteThrough(bstore, exch) bserv := NewWriteThrough(bstore, exch)
bgen := butil.NewBlockGenerator() 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 blockstore.GCBlockstore
t *testing.T PutCounter int
Fail bool
} }
func (bs HasFailingBlockstore) Has(k *cid.Cid) (bool, error) { func (bs *PutCountingBlockstore) Put(block blocks.Block) error {
if bs.Fail { bs.PutCounter++
bs.t.Fatal("Has shouldn't be called") return bs.GCBlockstore.Put(block)
return false, nil
}
return bs.GCBlockstore.Has(k)
} }
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