blockservice_test.go 3 KB
Newer Older
1 2 3
package blockservice

import (
4
	"context"
5 6
	"testing"

Jeromy's avatar
Jeromy committed
7 8 9 10 11
	blocks "github.com/ipfs/go-block-format"
	ds "github.com/ipfs/go-datastore"
	dssync "github.com/ipfs/go-datastore/sync"
	blockstore "github.com/ipfs/go-ipfs-blockstore"
	butil "github.com/ipfs/go-ipfs-blocksutil"
12
	exchange "github.com/ipfs/go-ipfs-exchange-interface"
Jeromy's avatar
Jeromy committed
13
	offline "github.com/ipfs/go-ipfs-exchange-offline"
14 15 16
)

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

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

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
func TestLazySessionInitialization(t *testing.T) {
	ctx := context.Background()
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()

	bstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
	bstore2 := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
	bstore3 := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
	session := offline.Exchange(bstore2)
	exchange := offline.Exchange(bstore3)
	sessionExch := &fakeSessionExchange{Interface: exchange, session: session}
	bservSessEx := NewWriteThrough(bstore, sessionExch)
	bgen := butil.NewBlockGenerator()

	block := bgen.Next()
	bstore.Put(block)

	block2 := bgen.Next()
	session.HasBlock(block2)

	bsession := NewSession(ctx, bservSessEx)
	if bsession.ses != nil {
		t.Fatal("Session exchange should not instantiated session immediately")
	}
	returnedBlock, err := bsession.GetBlock(ctx, block.Cid())
	if err != nil {
		t.Fatal("Should have fetched block locally")
	}
	if returnedBlock.Cid() != block.Cid() {
		t.Fatal("Got incorrect block")
	}
	if bsession.ses != nil {
		t.Fatal("Session exchange should not instantiated session if local store had block")
	}
	returnedBlock, err = bsession.GetBlock(ctx, block2.Cid())
	if err != nil {
		t.Fatal("Should have fetched block remotely")
	}
	if returnedBlock.Cid() != block2.Cid() {
		t.Fatal("Got incorrect block")
	}
	if bsession.ses != session {
		t.Fatal("Should have initialized session to fetch block")
	}
}

86
var _ blockstore.Blockstore = (*PutCountingBlockstore)(nil)
87

88
type PutCountingBlockstore struct {
89
	blockstore.Blockstore
90
	PutCounter int
91 92
}

93 94
func (bs *PutCountingBlockstore) Put(block blocks.Block) error {
	bs.PutCounter++
95
	return bs.Blockstore.Put(block)
96
}
97 98 99 100 101 102 103 104

var _ exchange.SessionExchange = (*fakeSessionExchange)(nil)

type fakeSessionExchange struct {
	exchange.Interface
	session exchange.Fetcher
}

Steven Allen's avatar
Steven Allen committed
105 106 107 108
func (fe *fakeSessionExchange) NewSession(ctx context.Context) exchange.Fetcher {
	if ctx == nil {
		panic("nil context")
	}
109 110
	return fe.session
}