testinstance.go 3.58 KB
Newer Older
1
package testsession
2 3

import (
4
	"context"
5 6
	"time"

7 8
	bitswap "github.com/ipfs/go-bitswap"
	bsnet "github.com/ipfs/go-bitswap/network"
Jeromy's avatar
Jeromy committed
9 10 11 12 13 14
	tn "github.com/ipfs/go-bitswap/testnet"
	ds "github.com/ipfs/go-datastore"
	delayed "github.com/ipfs/go-datastore/delayed"
	ds_sync "github.com/ipfs/go-datastore/sync"
	blockstore "github.com/ipfs/go-ipfs-blockstore"
	delay "github.com/ipfs/go-ipfs-delay"
Raúl Kripalani's avatar
Raúl Kripalani committed
15
	peer "github.com/libp2p/go-libp2p-core/peer"
Jeromy's avatar
Jeromy committed
16
	p2ptestutil "github.com/libp2p/go-libp2p-netutil"
17
	tnet "github.com/libp2p/go-libp2p-testing/net"
18 19
)

20
// NewTestInstanceGenerator generates a new InstanceGenerator for the given
21
// testnet
22
func NewTestInstanceGenerator(net tn.Network, bsOptions ...bitswap.Option) InstanceGenerator {
23
	ctx, cancel := context.WithCancel(context.Background())
24
	return InstanceGenerator{
25 26 27 28 29
		net:       net,
		seq:       0,
		ctx:       ctx, // TODO take ctx as param to Next, Instances
		cancel:    cancel,
		bsOptions: bsOptions,
30 31 32
	}
}

33 34
// InstanceGenerator generates new test instances of bitswap+dependencies
type InstanceGenerator struct {
35 36 37 38 39
	seq       int
	net       tn.Network
	ctx       context.Context
	cancel    context.CancelFunc
	bsOptions []bitswap.Option
40 41
}

42
// Close closes the clobal context, shutting down all test instances
43
func (g *InstanceGenerator) Close() error {
44
	g.cancel()
45
	return nil // for Closer interface
46 47
}

48
// Next generates a new instance of bitswap + dependencies
49
func (g *InstanceGenerator) Next() Instance {
50
	g.seq++
51
	p, err := p2ptestutil.RandTestBogusIdentity()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
52 53 54
	if err != nil {
		panic("FIXME") // TODO change signature
	}
55
	return NewInstance(g.ctx, g.net, p, g.bsOptions...)
56 57
}

58
// Instances creates N test instances of bitswap + dependencies
59
func (g *InstanceGenerator) Instances(n int) []Instance {
rht's avatar
rht committed
60
	var instances []Instance
61 62 63 64
	for j := 0; j < n; j++ {
		inst := g.Next()
		instances = append(instances, inst)
	}
65 66 67
	for i, inst := range instances {
		for j := i + 1; j < len(instances); j++ {
			oinst := instances[j]
Steven Allen's avatar
Steven Allen committed
68 69 70 71
			err := inst.Adapter.ConnectTo(context.Background(), oinst.Peer)
			if err != nil {
				panic(err.Error())
			}
72 73
		}
	}
74 75 76
	return instances
}

77
// Instance is a test instance of bitswap + dependencies for integration testing
78
type Instance struct {
79 80 81 82
	Peer            peer.ID
	Exchange        *bitswap.Bitswap
	blockstore      blockstore.Blockstore
	Adapter         bsnet.BitSwapNetwork
83 84 85
	blockstoreDelay delay.D
}

86
// Blockstore returns the block store for this test instance
87 88 89 90
func (i *Instance) Blockstore() blockstore.Blockstore {
	return i.blockstore
}

91 92
// SetBlockstoreLatency customizes the artificial delay on receiving blocks
// from a blockstore test instance.
93 94
func (i *Instance) SetBlockstoreLatency(t time.Duration) time.Duration {
	return i.blockstoreDelay.Set(t)
95 96
}

97
// NewInstance creates a test bitswap instance.
98 99
//
// NB: It's easy make mistakes by providing the same peer ID to two different
100
// instances. To safeguard, use the InstanceGenerator to generate instances. It's
101
// just a much better idea.
102
func NewInstance(ctx context.Context, net tn.Network, p tnet.Identity, options ...bitswap.Option) Instance {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
103
	bsdelay := delay.Fixed(0)
104

105
	adapter := net.Adapter(p)
106
	dstore := ds_sync.MutexWrap(delayed.New(ds.NewMapDatastore(), bsdelay))
107

108 109 110
	bstore, err := blockstore.CachedBlockstore(ctx,
		blockstore.NewBlockstore(ds_sync.MutexWrap(dstore)),
		blockstore.DefaultCacheOpts())
111
	if err != nil {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
112
		panic(err.Error()) // FIXME perhaps change signature and return error.
113
	}
114

115
	bs := bitswap.New(ctx, adapter, bstore, options...).(*bitswap.Bitswap)
116 117

	return Instance{
118
		Adapter:         adapter,
Brian Tiger Chow's avatar
Brian Tiger Chow committed
119
		Peer:            p.ID(),
120 121 122
		Exchange:        bs,
		blockstore:      bstore,
		blockstoreDelay: bsdelay,
123 124
	}
}