blockservice_test.go 1.29 KB
Newer Older
1 2 3 4 5
package blockservice

import (
	"testing"

6
	"github.com/ipfs/go-ipfs/blocks"
7 8 9 10
	"github.com/ipfs/go-ipfs/blocks/blockstore"
	butil "github.com/ipfs/go-ipfs/blocks/blocksutil"
	offline "github.com/ipfs/go-ipfs/exchange/offline"

Jeromy's avatar
Jeromy committed
11 12
	ds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore"
	dssync "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/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
}