testutils.go 2.47 KB
Newer Older
1 2 3
package bitswap

import (
4 5 6
	"time"

	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
7 8
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
	ds_sync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
9 10
	blockstore "github.com/jbenet/go-ipfs/blocks/blockstore"
	exchange "github.com/jbenet/go-ipfs/exchange"
11
	tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
12
	peer "github.com/jbenet/go-ipfs/peer"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
13
	mockrouting "github.com/jbenet/go-ipfs/routing/mock"
14 15
	datastore2 "github.com/jbenet/go-ipfs/util/datastore2"
	delay "github.com/jbenet/go-ipfs/util/delay"
16 17 18
)

func NewSessionGenerator(
Brian Tiger Chow's avatar
Brian Tiger Chow committed
19
	net tn.Network, rs mockrouting.Server) SessionGenerator {
20 21 22
	return SessionGenerator{
		net: net,
		rs:  rs,
23
		ps:  peer.NewPeerstore(),
24 25 26 27 28 29 30
		seq: 0,
	}
}

type SessionGenerator struct {
	seq int
	net tn.Network
Brian Tiger Chow's avatar
Brian Tiger Chow committed
31
	rs  mockrouting.Server
32
	ps  peer.Peerstore
33 34 35 36
}

func (g *SessionGenerator) Next() Instance {
	g.seq++
37
	return session(g.net, g.rs, g.ps, []byte(string(g.seq)))
38 39 40 41 42 43 44 45 46 47 48 49 50 51
}

func (g *SessionGenerator) Instances(n int) []Instance {
	instances := make([]Instance, 0)
	for j := 0; j < n; j++ {
		inst := g.Next()
		instances = append(instances, inst)
	}
	return instances
}

type Instance struct {
	Peer       peer.Peer
	Exchange   exchange.Interface
52 53 54 55 56 57 58 59 60 61 62
	blockstore blockstore.Blockstore

	blockstoreDelay delay.D
}

func (i *Instance) Blockstore() blockstore.Blockstore {
	return i.blockstore
}

func (i *Instance) SetBlockstoreLatency(t time.Duration) time.Duration {
	return i.blockstoreDelay.Set(t)
63 64 65 66 67 68 69
}

// session creates a test bitswap session.
//
// NB: It's easy make mistakes by providing the same peer ID to two different
// sessions. To safeguard, use the SessionGenerator to generate sessions. It's
// just a much better idea.
Brian Tiger Chow's avatar
Brian Tiger Chow committed
70
func session(net tn.Network, rs mockrouting.Server, ps peer.Peerstore, id peer.ID) Instance {
71
	p := ps.WithID(id)
72 73 74

	adapter := net.Adapter(p)
	htc := rs.Client(p)
75 76

	bsdelay := delay.Fixed(0)
77 78 79 80 81 82
	const kWriteCacheElems = 100
	bstore, err := blockstore.WriteCached(blockstore.NewBlockstore(ds_sync.MutexWrap(datastore2.WithDelay(ds.NewMapDatastore(), bsdelay))), kWriteCacheElems)
	if err != nil {
		// FIXME perhaps change signature and return error.
		panic(err.Error())
	}
83 84 85 86 87 88 89

	const alwaysSendToPeer = true
	ctx := context.TODO()

	bs := New(ctx, p, adapter, htc, bstore, alwaysSendToPeer)

	return Instance{
90 91 92 93
		Peer:            p,
		Exchange:        bs,
		blockstore:      bstore,
		blockstoreDelay: bsdelay,
94 95
	}
}