From dc03ebfe186fcaa1853725ccf07ca7ed7ff6b91e Mon Sep 17 00:00:00 2001 From: hannahhoward Date: Thu, 9 May 2019 17:05:29 -0700 Subject: [PATCH] refactor(testinstance): rename instance generator Instance generator was previously named session generator, which created confusion with bitswap sessions fix #101 --- benchmarks_test.go | 12 +++--- bitswap_test.go | 80 +++++++++++++++++------------------ bitswap_with_sessions_test.go | 50 +++++++++++----------- testinstance/testinstance.go | 26 ++++++------ 4 files changed, 84 insertions(+), 84 deletions(-) diff --git a/benchmarks_test.go b/benchmarks_test.go index 29198274..dbe05889 100644 --- a/benchmarks_test.go +++ b/benchmarks_test.go @@ -148,12 +148,12 @@ func subtestDistributeAndFetch(b *testing.B, numnodes, numblks int, d delay.D, d start := time.Now() net := tn.VirtualNetwork(mockrouting.NewServer(), d) - sg := testinstance.NewTestSessionGenerator(net) - defer sg.Close() + ig := testinstance.NewTestInstanceGenerator(net) + defer ig.Close() bg := blocksutil.NewBlockGenerator() - instances := sg.Instances(numnodes) + instances := ig.Instances(numnodes) blocks := bg.Blocks(numblks) runDistribution(b, instances, blocks, df, ff, start) } @@ -162,10 +162,10 @@ func subtestDistributeAndFetchRateLimited(b *testing.B, numnodes, numblks int, d start := time.Now() net := tn.RateLimitedVirtualNetwork(mockrouting.NewServer(), d, rateLimitGenerator) - sg := testinstance.NewTestSessionGenerator(net) - defer sg.Close() + ig := testinstance.NewTestInstanceGenerator(net) + defer ig.Close() - instances := sg.Instances(numnodes) + instances := ig.Instances(numnodes) blocks := testutil.GenerateBlocksOfSize(numblks, blockSize) runDistribution(b, instances, blocks, df, ff, start) diff --git a/bitswap_test.go b/bitswap_test.go index 55690a73..c1d059b4 100644 --- a/bitswap_test.go +++ b/bitswap_test.go @@ -36,12 +36,12 @@ func getVirtualNetwork() tn.Network { func TestClose(t *testing.T) { vnet := getVirtualNetwork() - sesgen := testinstance.NewTestSessionGenerator(vnet) - defer sesgen.Close() + ig := testinstance.NewTestInstanceGenerator(vnet) + defer ig.Close() bgen := blocksutil.NewBlockGenerator() block := bgen.Next() - bitswap := sesgen.Next() + bitswap := ig.Next() bitswap.Exchange.Close() bitswap.Exchange.GetBlock(context.Background(), block.Cid()) @@ -51,14 +51,14 @@ func TestProviderForKeyButNetworkCannotFind(t *testing.T) { // TODO revisit this rs := mockrouting.NewServer() net := tn.VirtualNetwork(rs, delay.Fixed(kNetworkDelay)) - g := testinstance.NewTestSessionGenerator(net) - defer g.Close() + ig := testinstance.NewTestInstanceGenerator(net) + defer ig.Close() block := blocks.NewBlock([]byte("block")) pinfo := p2ptestutil.RandTestBogusIdentityOrFatal(t) rs.Client(pinfo).Provide(context.Background(), block.Cid(), true) // but not on network - solo := g.Next() + solo := ig.Next() defer solo.Exchange.Close() ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond) @@ -74,10 +74,10 @@ func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) { net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay)) block := blocks.NewBlock([]byte("block")) - g := testinstance.NewTestSessionGenerator(net) - defer g.Close() + ig := testinstance.NewTestInstanceGenerator(net) + defer ig.Close() - peers := g.Instances(2) + peers := ig.Instances(2) hasBlock := peers[0] defer hasBlock.Exchange.Close() @@ -107,10 +107,10 @@ func TestDoesNotProvideWhenConfiguredNotTo(t *testing.T) { net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay)) block := blocks.NewBlock([]byte("block")) - g := testinstance.NewTestSessionGenerator(net) - defer g.Close() + ig := testinstance.NewTestInstanceGenerator(net) + defer ig.Close() - hasBlock := g.Next() + hasBlock := ig.Next() defer hasBlock.Exchange.Close() if err := hasBlock.Exchange.HasBlock(block); err != nil { @@ -120,7 +120,7 @@ func TestDoesNotProvideWhenConfiguredNotTo(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() - wantsBlock := g.Next() + wantsBlock := ig.Next() defer wantsBlock.Exchange.Close() ns := wantsBlock.Exchange.NewSession(ctx).(*bssession.Session) @@ -144,10 +144,10 @@ func TestUnwantedBlockNotAdded(t *testing.T) { bsMessage := message.New(true) bsMessage.AddBlock(block) - g := testinstance.NewTestSessionGenerator(net) - defer g.Close() + ig := testinstance.NewTestInstanceGenerator(net) + defer ig.Close() - peers := g.Instances(2) + peers := ig.Instances(2) hasBlock := peers[0] defer hasBlock.Exchange.Close() @@ -216,11 +216,11 @@ func PerformDistributionTest(t *testing.T, numInstances, numBlocks int) { t.SkipNow() } net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay)) - sg := testinstance.NewTestSessionGenerator(net) - defer sg.Close() + ig := testinstance.NewTestInstanceGenerator(net) + defer ig.Close() bg := blocksutil.NewBlockGenerator() - instances := sg.Instances(numInstances) + instances := ig.Instances(numInstances) blocks := bg.Blocks(numBlocks) t.Log("Give the blocks to the first instance") @@ -279,11 +279,11 @@ func TestSendToWantingPeer(t *testing.T) { } net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay)) - sg := testinstance.NewTestSessionGenerator(net) - defer sg.Close() + ig := testinstance.NewTestInstanceGenerator(net) + defer ig.Close() bg := blocksutil.NewBlockGenerator() - peers := sg.Instances(2) + peers := ig.Instances(2) peerA := peers[0] peerB := peers[1] @@ -321,9 +321,9 @@ func TestSendToWantingPeer(t *testing.T) { func TestEmptyKey(t *testing.T) { net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay)) - sg := testinstance.NewTestSessionGenerator(net) - defer sg.Close() - bs := sg.Instances(1)[0].Exchange + ig := testinstance.NewTestInstanceGenerator(net) + defer ig.Close() + bs := ig.Instances(1)[0].Exchange ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) defer cancel() @@ -354,13 +354,13 @@ func assertStat(t *testing.T, st *bitswap.Stat, sblks, rblks, sdata, rdata uint6 func TestBasicBitswap(t *testing.T) { net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay)) - sg := testinstance.NewTestSessionGenerator(net) - defer sg.Close() + ig := testinstance.NewTestInstanceGenerator(net) + defer ig.Close() bg := blocksutil.NewBlockGenerator() t.Log("Test a one node trying to get one block from another") - instances := sg.Instances(3) + instances := ig.Instances(3) blocks := bg.Blocks(1) err := instances[0].Exchange.HasBlock(blocks[0]) if err != nil { @@ -423,13 +423,13 @@ func TestBasicBitswap(t *testing.T) { func TestDoubleGet(t *testing.T) { net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay)) - sg := testinstance.NewTestSessionGenerator(net) - defer sg.Close() + ig := testinstance.NewTestInstanceGenerator(net) + defer ig.Close() bg := blocksutil.NewBlockGenerator() t.Log("Test a one node trying to get one block from another") - instances := sg.Instances(2) + instances := ig.Instances(2) blocks := bg.Blocks(1) // NOTE: A race condition can happen here where these GetBlocks requests go @@ -491,11 +491,11 @@ func TestDoubleGet(t *testing.T) { func TestWantlistCleanup(t *testing.T) { net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay)) - sg := testinstance.NewTestSessionGenerator(net) - defer sg.Close() + ig := testinstance.NewTestInstanceGenerator(net) + defer ig.Close() bg := blocksutil.NewBlockGenerator() - instances := sg.Instances(1)[0] + instances := ig.Instances(1)[0] bswap := instances.Exchange blocks := bg.Blocks(20) @@ -602,13 +602,13 @@ func newReceipt(sent, recv, exchanged uint64) *decision.Receipt { func TestBitswapLedgerOneWay(t *testing.T) { net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay)) - sg := testinstance.NewTestSessionGenerator(net) - defer sg.Close() + ig := testinstance.NewTestInstanceGenerator(net) + defer ig.Close() bg := blocksutil.NewBlockGenerator() t.Log("Test ledgers match when one peer sends block to another") - instances := sg.Instances(2) + instances := ig.Instances(2) blocks := bg.Blocks(1) err := instances[0].Exchange.HasBlock(blocks[0]) if err != nil { @@ -654,13 +654,13 @@ func TestBitswapLedgerOneWay(t *testing.T) { func TestBitswapLedgerTwoWay(t *testing.T) { net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay)) - sg := testinstance.NewTestSessionGenerator(net) - defer sg.Close() + ig := testinstance.NewTestInstanceGenerator(net) + defer ig.Close() bg := blocksutil.NewBlockGenerator() t.Log("Test ledgers match when two peers send one block to each other") - instances := sg.Instances(2) + instances := ig.Instances(2) blocks := bg.Blocks(2) err := instances[0].Exchange.HasBlock(blocks[0]) if err != nil { diff --git a/bitswap_with_sessions_test.go b/bitswap_with_sessions_test.go index dd26a30c..50be52ca 100644 --- a/bitswap_with_sessions_test.go +++ b/bitswap_with_sessions_test.go @@ -19,12 +19,12 @@ func TestBasicSessions(t *testing.T) { defer cancel() vnet := getVirtualNetwork() - sesgen := testinstance.NewTestSessionGenerator(vnet) - defer sesgen.Close() + ig := testinstance.NewTestInstanceGenerator(vnet) + defer ig.Close() bgen := blocksutil.NewBlockGenerator() block := bgen.Next() - inst := sesgen.Instances(2) + inst := ig.Instances(2) a := inst[0] b := inst[1] @@ -67,11 +67,11 @@ func TestSessionBetweenPeers(t *testing.T) { defer cancel() vnet := getVirtualNetwork() - sesgen := testinstance.NewTestSessionGenerator(vnet) - defer sesgen.Close() + ig := testinstance.NewTestInstanceGenerator(vnet) + defer ig.Close() bgen := blocksutil.NewBlockGenerator() - inst := sesgen.Instances(10) + inst := ig.Instances(10) blks := bgen.Blocks(101) if err := inst[0].Blockstore().PutMany(blks); err != nil { @@ -120,11 +120,11 @@ func TestSessionSplitFetch(t *testing.T) { defer cancel() vnet := getVirtualNetwork() - sesgen := testinstance.NewTestSessionGenerator(vnet) - defer sesgen.Close() + ig := testinstance.NewTestInstanceGenerator(vnet) + defer ig.Close() bgen := blocksutil.NewBlockGenerator() - inst := sesgen.Instances(11) + inst := ig.Instances(11) blks := bgen.Blocks(100) for i := 0; i < 10; i++ { @@ -163,11 +163,11 @@ func TestFetchNotConnected(t *testing.T) { bssession.SetProviderSearchDelay(10 * time.Millisecond) vnet := getVirtualNetwork() - sesgen := testinstance.NewTestSessionGenerator(vnet) - defer sesgen.Close() + ig := testinstance.NewTestInstanceGenerator(vnet) + defer ig.Close() bgen := blocksutil.NewBlockGenerator() - other := sesgen.Next() + other := ig.Next() blks := bgen.Blocks(10) for _, block := range blks { @@ -181,7 +181,7 @@ func TestFetchNotConnected(t *testing.T) { cids = append(cids, blk.Cid()) } - thisNode := sesgen.Next() + thisNode := ig.Next() ses := thisNode.Exchange.NewSession(ctx).(*bssession.Session) ses.SetBaseTickDelay(time.Millisecond * 10) @@ -203,12 +203,12 @@ func TestInterestCacheOverflow(t *testing.T) { defer cancel() vnet := getVirtualNetwork() - sesgen := testinstance.NewTestSessionGenerator(vnet) - defer sesgen.Close() + ig := testinstance.NewTestInstanceGenerator(vnet) + defer ig.Close() bgen := blocksutil.NewBlockGenerator() blks := bgen.Blocks(2049) - inst := sesgen.Instances(2) + inst := ig.Instances(2) a := inst[0] b := inst[1] @@ -255,12 +255,12 @@ func TestPutAfterSessionCacheEvict(t *testing.T) { defer cancel() vnet := getVirtualNetwork() - sesgen := testinstance.NewTestSessionGenerator(vnet) - defer sesgen.Close() + ig := testinstance.NewTestInstanceGenerator(vnet) + defer ig.Close() bgen := blocksutil.NewBlockGenerator() blks := bgen.Blocks(2500) - inst := sesgen.Instances(1) + inst := ig.Instances(1) a := inst[0] @@ -295,12 +295,12 @@ func TestMultipleSessions(t *testing.T) { defer cancel() vnet := getVirtualNetwork() - sesgen := testinstance.NewTestSessionGenerator(vnet) - defer sesgen.Close() + ig := testinstance.NewTestInstanceGenerator(vnet) + defer ig.Close() bgen := blocksutil.NewBlockGenerator() blk := bgen.Blocks(1)[0] - inst := sesgen.Instances(2) + inst := ig.Instances(2) a := inst[0] b := inst[1] @@ -338,8 +338,8 @@ func TestWantlistClearsOnCancel(t *testing.T) { defer cancel() vnet := getVirtualNetwork() - sesgen := testinstance.NewTestSessionGenerator(vnet) - defer sesgen.Close() + ig := testinstance.NewTestInstanceGenerator(vnet) + defer ig.Close() bgen := blocksutil.NewBlockGenerator() blks := bgen.Blocks(10) @@ -348,7 +348,7 @@ func TestWantlistClearsOnCancel(t *testing.T) { cids = append(cids, blk.Cid()) } - inst := sesgen.Instances(1) + inst := ig.Instances(1) a := inst[0] diff --git a/testinstance/testinstance.go b/testinstance/testinstance.go index f677c949..f459065f 100644 --- a/testinstance/testinstance.go +++ b/testinstance/testinstance.go @@ -17,12 +17,12 @@ import ( testutil "github.com/libp2p/go-testutil" ) -// NewTestSessionGenerator generates a new SessionGenerator for the given +// NewTestInstanceGenerator generates a new InstanceGenerator for the given // testnet -func NewTestSessionGenerator( - net tn.Network) SessionGenerator { +func NewTestInstanceGenerator( + net tn.Network) InstanceGenerator { ctx, cancel := context.WithCancel(context.Background()) - return SessionGenerator{ + return InstanceGenerator{ net: net, seq: 0, ctx: ctx, // TODO take ctx as param to Next, Instances @@ -30,8 +30,8 @@ func NewTestSessionGenerator( } } -// SessionGenerator generates new test instances of bitswap+dependencies -type SessionGenerator struct { +// InstanceGenerator generates new test instances of bitswap+dependencies +type InstanceGenerator struct { seq int net tn.Network ctx context.Context @@ -39,23 +39,23 @@ type SessionGenerator struct { } // Close closes the clobal context, shutting down all test instances -func (g *SessionGenerator) Close() error { +func (g *InstanceGenerator) Close() error { g.cancel() return nil // for Closer interface } // Next generates a new instance of bitswap + dependencies -func (g *SessionGenerator) Next() Instance { +func (g *InstanceGenerator) Next() Instance { g.seq++ p, err := p2ptestutil.RandTestBogusIdentity() if err != nil { panic("FIXME") // TODO change signature } - return MkSession(g.ctx, g.net, p) + return NewInstance(g.ctx, g.net, p) } // Instances creates N test instances of bitswap + dependencies -func (g *SessionGenerator) Instances(n int) []Instance { +func (g *InstanceGenerator) Instances(n int) []Instance { var instances []Instance for j := 0; j < n; j++ { inst := g.Next() @@ -90,12 +90,12 @@ func (i *Instance) SetBlockstoreLatency(t time.Duration) time.Duration { return i.blockstoreDelay.Set(t) } -// MkSession creates a test bitswap instance. +// NewInstance creates a test bitswap instance. // // 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 +// instances. To safeguard, use the InstanceGenerator to generate instances. It's // just a much better idea. -func MkSession(ctx context.Context, net tn.Network, p testutil.Identity) Instance { +func NewInstance(ctx context.Context, net tn.Network, p testutil.Identity) Instance { bsdelay := delay.Fixed(0) adapter := net.Adapter(p)