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"
9
	"gx/ipfs/QmXxGS5QsUxpR3iqL5DjmsYPHR1Yz74siRQ4ChJqWFosMh/go-block-format"
10

11 12
	ds "gx/ipfs/QmVSase1JP7cq9QkPT46oNwdp9pT6kBkG3oqS14y3QcZjG/go-datastore"
	dssync "gx/ipfs/QmVSase1JP7cq9QkPT46oNwdp9pT6kBkG3oqS14y3QcZjG/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
}