github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/exchange/bitswap/testutils.go (about)

     1  package bitswap
     2  
     3  import (
     4  	"time"
     5  
     6  	ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
     7  	ds_sync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
     8  	context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
     9  	blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
    10  	tn "github.com/ipfs/go-ipfs/exchange/bitswap/testnet"
    11  	peer "github.com/ipfs/go-ipfs/p2p/peer"
    12  	p2ptestutil "github.com/ipfs/go-ipfs/p2p/test/util"
    13  	delay "github.com/ipfs/go-ipfs/thirdparty/delay"
    14  	datastore2 "github.com/ipfs/go-ipfs/util/datastore2"
    15  	testutil "github.com/ipfs/go-ipfs/util/testutil"
    16  )
    17  
    18  // WARNING: this uses RandTestBogusIdentity DO NOT USE for NON TESTS!
    19  func NewTestSessionGenerator(
    20  	net tn.Network) SessionGenerator {
    21  	ctx, cancel := context.WithCancel(context.Background())
    22  	return SessionGenerator{
    23  		net:    net,
    24  		seq:    0,
    25  		ctx:    ctx, // TODO take ctx as param to Next, Instances
    26  		cancel: cancel,
    27  	}
    28  }
    29  
    30  // TODO move this SessionGenerator to the core package and export it as the core generator
    31  type SessionGenerator struct {
    32  	seq    int
    33  	net    tn.Network
    34  	ctx    context.Context
    35  	cancel context.CancelFunc
    36  }
    37  
    38  func (g *SessionGenerator) Close() error {
    39  	g.cancel()
    40  	return nil // for Closer interface
    41  }
    42  
    43  func (g *SessionGenerator) Next() Instance {
    44  	g.seq++
    45  	p, err := p2ptestutil.RandTestBogusIdentity()
    46  	if err != nil {
    47  		panic("FIXME") // TODO change signature
    48  	}
    49  	return Session(g.ctx, g.net, p)
    50  }
    51  
    52  func (g *SessionGenerator) Instances(n int) []Instance {
    53  	var instances []Instance
    54  	for j := 0; j < n; j++ {
    55  		inst := g.Next()
    56  		instances = append(instances, inst)
    57  	}
    58  	for i, inst := range instances {
    59  		for j := i + 1; j < len(instances); j++ {
    60  			oinst := instances[j]
    61  			inst.Exchange.PeerConnected(oinst.Peer)
    62  		}
    63  	}
    64  	return instances
    65  }
    66  
    67  type Instance struct {
    68  	Peer       peer.ID
    69  	Exchange   *Bitswap
    70  	blockstore blockstore.Blockstore
    71  
    72  	blockstoreDelay delay.D
    73  }
    74  
    75  func (i *Instance) Blockstore() blockstore.Blockstore {
    76  	return i.blockstore
    77  }
    78  
    79  func (i *Instance) SetBlockstoreLatency(t time.Duration) time.Duration {
    80  	return i.blockstoreDelay.Set(t)
    81  }
    82  
    83  // session creates a test bitswap session.
    84  //
    85  // NB: It's easy make mistakes by providing the same peer ID to two different
    86  // sessions. To safeguard, use the SessionGenerator to generate sessions. It's
    87  // just a much better idea.
    88  func Session(ctx context.Context, net tn.Network, p testutil.Identity) Instance {
    89  	bsdelay := delay.Fixed(0)
    90  	const writeCacheElems = 100
    91  
    92  	adapter := net.Adapter(p)
    93  	dstore := ds_sync.MutexWrap(datastore2.WithDelay(ds.NewMapDatastore(), bsdelay))
    94  
    95  	bstore, err := blockstore.WriteCached(blockstore.NewBlockstore(ds_sync.MutexWrap(dstore)), writeCacheElems)
    96  	if err != nil {
    97  		panic(err.Error()) // FIXME perhaps change signature and return error.
    98  	}
    99  
   100  	const alwaysSendToPeer = true
   101  
   102  	bs := New(ctx, p.ID(), adapter, bstore, alwaysSendToPeer).(*Bitswap)
   103  
   104  	return Instance{
   105  		Peer:            p.ID(),
   106  		Exchange:        bs,
   107  		blockstore:      bstore,
   108  		blockstoreDelay: bsdelay,
   109  	}
   110  }