testutils.go 2.91 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"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12
	peer "github.com/jbenet/go-ipfs/p2p/peer"
13
	p2ptestutil "github.com/jbenet/go-ipfs/p2p/test/util"
14 15
	datastore2 "github.com/jbenet/go-ipfs/util/datastore2"
	delay "github.com/jbenet/go-ipfs/util/delay"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
16
	testutil "github.com/jbenet/go-ipfs/util/testutil"
17 18
)

19 20
// WARNING: this uses RandTestBogusIdentity DO NOT USE for NON TESTS!
func NewTestSessionGenerator(
21
	net tn.Network) SessionGenerator {
22
	ctx, cancel := context.WithCancel(context.TODO())
23
	return SessionGenerator{
24 25
		net:    net,
		seq:    0,
Brian Tiger Chow's avatar
Brian Tiger Chow committed
26
		ctx:    ctx, // TODO take ctx as param to Next, Instances
27
		cancel: cancel,
28 29 30
	}
}

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

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

func (g *SessionGenerator) Next() Instance {
	g.seq++
46
	p, err := p2ptestutil.RandTestBogusIdentity()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
47 48 49 50
	if err != nil {
		panic("FIXME") // TODO change signature
	}
	return session(g.ctx, g.net, p)
51 52 53 54 55 56 57 58 59 60 61 62
}

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 {
63
	Peer       peer.ID
64
	Exchange   exchange.Interface
65 66 67 68 69 70 71 72 73 74 75
	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)
76 77 78 79 80 81 82
}

// 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.
83
func session(ctx context.Context, net tn.Network, p testutil.Identity) Instance {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
84 85
	bsdelay := delay.Fixed(0)
	const kWriteCacheElems = 100
86

87
	adapter := net.Adapter(p)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
88
	dstore := ds_sync.MutexWrap(datastore2.WithDelay(ds.NewMapDatastore(), bsdelay))
89

Brian Tiger Chow's avatar
Brian Tiger Chow committed
90
	bstore, err := blockstore.WriteCached(blockstore.NewBlockstore(ds_sync.MutexWrap(dstore)), kWriteCacheElems)
91
	if err != nil {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
92
		panic(err.Error()) // FIXME perhaps change signature and return error.
93
	}
94 95 96

	const alwaysSendToPeer = true

Brian Tiger Chow's avatar
Brian Tiger Chow committed
97
	bs := New(ctx, p.ID(), adapter, bstore, alwaysSendToPeer)
98 99

	return Instance{
Brian Tiger Chow's avatar
Brian Tiger Chow committed
100
		Peer:            p.ID(),
101 102 103
		Exchange:        bs,
		blockstore:      bstore,
		blockstoreDelay: bsdelay,
104 105
	}
}