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

import (
4
	"context"
5 6
	"time"

7 8
	blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
	tn "github.com/ipfs/go-ipfs/exchange/bitswap/testnet"
9
	datastore2 "github.com/ipfs/go-ipfs/thirdparty/datastore2"
10
	delay "github.com/ipfs/go-ipfs/thirdparty/delay"
Steven Allen's avatar
Steven Allen committed
11
	testutil "gx/ipfs/QmeDA8gNhvRTsbrjEieay5wezupJDiky8xvCzDABbsGzmp/go-testutil"
12

Steven Allen's avatar
Steven Allen committed
13 14
	peer "gx/ipfs/QmWNY7dV54ZDYmTA1ykVdwNCqC11mpU4zSUp6XDpLTH9eG/go-libp2p-peer"
	p2ptestutil "gx/ipfs/QmZTcPxK6VqrwY94JpKZPvEqAZ6tEr1rLrpcqJbbRZbg2V/go-libp2p-netutil"
15 16
	ds "gx/ipfs/QmdHG8MAuARdGHxx4rPQASLcvhz24fzjSQq7AJRAQEorq5/go-datastore"
	ds_sync "gx/ipfs/QmdHG8MAuARdGHxx4rPQASLcvhz24fzjSQq7AJRAQEorq5/go-datastore/sync"
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.Background())
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
	if err != nil {
		panic("FIXME") // TODO change signature
	}
Jeromy's avatar
Jeromy committed
50
	return MkSession(g.ctx, g.net, p)
51 52 53
}

func (g *SessionGenerator) Instances(n int) []Instance {
rht's avatar
rht committed
54
	var instances []Instance
55 56 57 58
	for j := 0; j < n; j++ {
		inst := g.Next()
		instances = append(instances, inst)
	}
59 60 61
	for i, inst := range instances {
		for j := i + 1; j < len(instances); j++ {
			oinst := instances[j]
62
			inst.Exchange.network.ConnectTo(context.Background(), oinst.Peer)
63 64
		}
	}
65 66 67 68
	return instances
}

type Instance struct {
69
	Peer       peer.ID
70
	Exchange   *Bitswap
71 72 73 74 75 76 77 78 79 80 81
	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)
82 83 84 85 86 87 88
}

// 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.
Jeromy's avatar
Jeromy committed
89
func MkSession(ctx context.Context, net tn.Network, p testutil.Identity) Instance {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
90
	bsdelay := delay.Fixed(0)
91

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

95 96 97
	bstore, err := blockstore.CachedBlockstore(ctx,
		blockstore.NewBlockstore(ds_sync.MutexWrap(dstore)),
		blockstore.DefaultCacheOpts())
98
	if err != nil {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
99
		panic(err.Error()) // FIXME perhaps change signature and return error.
100
	}
101 102 103

	const alwaysSendToPeer = true

104
	bs := New(ctx, p.ID(), adapter, bstore, alwaysSendToPeer).(*Bitswap)
105 106

	return Instance{
Brian Tiger Chow's avatar
Brian Tiger Chow committed
107
		Peer:            p.ID(),
108 109 110
		Exchange:        bs,
		blockstore:      bstore,
		blockstoreDelay: bsdelay,
111 112
	}
}