blockservice_test.go 1.33 KB
Newer Older
1 2 3 4 5 6 7 8
package blockservice

import (
	"testing"

	"github.com/ipfs/go-ipfs/blocks/blockstore"
	butil "github.com/ipfs/go-ipfs/blocks/blocksutil"
	offline "github.com/ipfs/go-ipfs/exchange/offline"
Steven Allen's avatar
Steven Allen committed
9
	"gx/ipfs/Qmej7nf81hi2x2tvjRBF3mcp74sQyuDH4VMYDGd1YtXjb2/go-block-format"
10

Steven Allen's avatar
Steven Allen committed
11 12
	ds "gx/ipfs/QmPpegoMqhAEqjncrzArm7KVWAkCm78rqL2DPuNjhPrshg/go-datastore"
	dssync "gx/ipfs/QmPpegoMqhAEqjncrzArm7KVWAkCm78rqL2DPuNjhPrshg/go-datastore/sync"
13 14 15
)

func TestWriteThroughWorks(t *testing.T) {
16 17 18
	bstore := &PutCountingBlockstore{
		blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore())),
		0,
19
	}
20 21
	bstore2 := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
	exch := offline.Exchange(bstore2)
22 23 24
	bserv := NewWriteThrough(bstore, exch)
	bgen := butil.NewBlockGenerator()

25 26 27 28 29 30 31 32 33 34
	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 {
Jakub Sztandera's avatar
Jakub Sztandera committed
35
		t.Fatalf("Put should have called again, should be 2 is: %d", bstore.PutCounter)
36
	}
37 38
}

39
var _ blockstore.Blockstore = (*PutCountingBlockstore)(nil)
40

41
type PutCountingBlockstore struct {
42
	blockstore.Blockstore
43
	PutCounter int
44 45
}

46 47
func (bs *PutCountingBlockstore) Put(block blocks.Block) error {
	bs.PutCounter++
48
	return bs.Blockstore.Put(block)
49
}