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

import (
4 5
	"time"

6 7
	blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
	tn "github.com/ipfs/go-ipfs/exchange/bitswap/testnet"
8
	datastore2 "github.com/ipfs/go-ipfs/thirdparty/datastore2"
9
	delay "github.com/ipfs/go-ipfs/thirdparty/delay"
10
	testutil "github.com/ipfs/go-ipfs/thirdparty/testutil"
11
	peer "gx/ipfs/QmRBqJF7hb8ZSpRcMwUt8hNhydWcxGEhtk81HKq6oUwKvs/go-libp2p-peer"
Jeromy's avatar
Jeromy committed
12
	p2ptestutil "gx/ipfs/QmVCe3SNMjkcPgnpFhZs719dheq6xE7gJwjzV7aWcUM4Ms/go-libp2p/p2p/test/util"
Jeromy's avatar
Jeromy committed
13
	context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
Jeromy's avatar
Jeromy committed
14 15
	ds "gx/ipfs/QmfQzVugPq1w5shWRcLWSeiHF4a2meBX7yVD8Vw7GWJM9o/go-datastore"
	ds_sync "gx/ipfs/QmfQzVugPq1w5shWRcLWSeiHF4a2meBX7yVD8Vw7GWJM9o/go-datastore/sync"
16 17
)

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

Brian Tiger Chow's avatar
Brian Tiger Chow committed
30
// TODO move this SessionGenerator to the core package and export it as the core generator
31
type SessionGenerator struct {
32 33 34 35 36 37
	seq    int
	net    tn.Network
	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
	p, err := p2ptestutil.RandTestBogusIdentity()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
46 47 48
	if err != nil {
		panic("FIXME") // TODO change signature
	}
Karthik Bala's avatar
Karthik Bala committed
49
	return Session(g.ctx, g.net, p)
50 51 52
}

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

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

// 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.
Karthik Bala's avatar
Karthik Bala committed
88
func Session(ctx context.Context, net tn.Network, p testutil.Identity) Instance {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
89
	bsdelay := delay.Fixed(0)
90
	const bloomSize = 512
rht's avatar
rht committed
91
	const writeCacheElems = 100
92

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

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

	const alwaysSendToPeer = true

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

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