testutils.go 2.66 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 13 14
	peer "github.com/jbenet/go-ipfs/peer"
	datastore2 "github.com/jbenet/go-ipfs/util/datastore2"
	delay "github.com/jbenet/go-ipfs/util/delay"
15 16 17
)

func NewSessionGenerator(
18
	net tn.Network) SessionGenerator {
19
	ctx, cancel := context.WithCancel(context.TODO())
20
	return SessionGenerator{
21 22 23
		ps:     peer.NewPeerstore(),
		net:    net,
		seq:    0,
Brian Tiger Chow's avatar
Brian Tiger Chow committed
24
		ctx:    ctx, // TODO take ctx as param to Next, Instances
25
		cancel: cancel,
26 27 28
	}
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
29
// TODO move this SessionGenerator to the core package and export it as the core generator
30
type SessionGenerator struct {
31 32 33 34 35 36 37
	seq    int
	net    tn.Network
	ps     peer.Peerstore
	ctx    context.Context
	cancel context.CancelFunc
}

38
func (g *SessionGenerator) Close() error {
39
	g.cancel()
40
	return nil // for Closer interface
41 42 43 44
}

func (g *SessionGenerator) Next() Instance {
	g.seq++
45
	return session(g.ctx, g.net, g.ps, peer.ID(g.seq))
46 47 48 49 50 51 52 53 54 55 56 57
}

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 {
58
	Peer       peer.ID
59
	Exchange   exchange.Interface
60 61 62 63 64 65 66 67 68 69 70
	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)
71 72 73 74 75 76 77
}

// 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.
78
func session(ctx context.Context, net tn.Network, ps peer.Peerstore, p peer.ID) Instance {
79 80

	adapter := net.Adapter(p)
81 82

	bsdelay := delay.Fixed(0)
83 84 85 86 87 88
	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())
	}
89 90 91

	const alwaysSendToPeer = true

92
	bs := New(ctx, p, adapter, bstore, alwaysSendToPeer)
93 94

	return Instance{
95 96 97 98
		Peer:            p,
		Exchange:        bs,
		blockstore:      bstore,
		blockstoreDelay: bsdelay,
99 100
	}
}