Skip to content

Commit b64a8ae

Browse files
committed
bitswap: testnet does no longer need to do routing itself for tests. Remove.
1 parent 4848435 commit b64a8ae

File tree

6 files changed

+30
-64
lines changed

6 files changed

+30
-64
lines changed

bitswap/benchmarks_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
bsnet "github.com/ipfs/boxo/bitswap/network"
2121
testinstance "github.com/ipfs/boxo/bitswap/testinstance"
2222
tn "github.com/ipfs/boxo/bitswap/testnet"
23-
mockrouting "github.com/ipfs/boxo/routing/mock"
2423
cid "github.com/ipfs/go-cid"
2524
delay "github.com/ipfs/go-ipfs-delay"
2625
)
@@ -142,7 +141,7 @@ func BenchmarkFetchFromOldBitswap(b *testing.B) {
142141
oldSeedCount := bch.oldSeedCount
143142
newSeedCount := bch.nodeCount - (fetcherCount + oldSeedCount)
144143

145-
net := tn.VirtualNetwork(mockrouting.NewServer(), fixedDelay)
144+
net := tn.VirtualNetwork(fixedDelay)
146145

147146
// Simulate an older Bitswap node (old protocol ID) that doesn't
148147
// send DONT_HAVE responses
@@ -294,7 +293,7 @@ func BenchmarkDatacenterMultiLeechMultiSeed(b *testing.B) {
294293
numblks := 1000
295294

296295
for i := 0; i < b.N; i++ {
297-
net := tn.RateLimitedVirtualNetwork(mockrouting.NewServer(), d, rateLimitGenerator)
296+
net := tn.RateLimitedVirtualNetwork(d, rateLimitGenerator)
298297

299298
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
300299
defer ig.Close()
@@ -312,7 +311,7 @@ func BenchmarkDatacenterMultiLeechMultiSeed(b *testing.B) {
312311

313312
func subtestDistributeAndFetch(b *testing.B, numnodes, numblks int, d delay.D, bstoreLatency time.Duration, df distFunc, ff fetchFunc) {
314313
for i := 0; i < b.N; i++ {
315-
net := tn.VirtualNetwork(mockrouting.NewServer(), d)
314+
net := tn.VirtualNetwork(d)
316315

317316
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
318317

@@ -327,7 +326,7 @@ func subtestDistributeAndFetch(b *testing.B, numnodes, numblks int, d delay.D, b
327326

328327
func subtestDistributeAndFetchRateLimited(b *testing.B, numnodes, numblks int, d delay.D, rateLimitGenerator tn.RateLimitGenerator, blockSize int64, bstoreLatency time.Duration, df distFunc, ff fetchFunc) {
329328
for i := 0; i < b.N; i++ {
330-
net := tn.RateLimitedVirtualNetwork(mockrouting.NewServer(), d, rateLimitGenerator)
329+
net := tn.RateLimitedVirtualNetwork(d, rateLimitGenerator)
331330

332331
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
333332
defer ig.Close()

bitswap/bitswap_test.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/ipfs/boxo/bitswap/server"
1616
testinstance "github.com/ipfs/boxo/bitswap/testinstance"
1717
tn "github.com/ipfs/boxo/bitswap/testnet"
18-
mockrouting "github.com/ipfs/boxo/routing/mock"
1918
blocks "github.com/ipfs/go-block-format"
2019
cid "github.com/ipfs/go-cid"
2120
detectrace "github.com/ipfs/go-detect-race"
@@ -51,7 +50,7 @@ func addBlock(t *testing.T, ctx context.Context, inst testinstance.Instance, blk
5150
const kNetworkDelay = 0 * time.Millisecond
5251

5352
func TestClose(t *testing.T) {
54-
vnet := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
53+
vnet := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
5554
ig := testinstance.NewTestInstanceGenerator(vnet, nil, nil)
5655
defer ig.Close()
5756
block := random.BlocksOfSize(1, blockSize)[0]
@@ -65,14 +64,13 @@ func TestClose(t *testing.T) {
6564
}
6665

6766
func TestProviderForKeyButNetworkCannotFind(t *testing.T) { // TODO revisit this
68-
rs := mockrouting.NewServer()
69-
net := tn.VirtualNetwork(rs, delay.Fixed(kNetworkDelay))
67+
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
7068
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
7169
defer ig.Close()
7270

7371
block := blocks.NewBlock([]byte("block"))
7472
pinfo := p2ptestutil.RandTestBogusIdentityOrFatal(t)
75-
err := rs.Client(pinfo).Provide(context.Background(), block.Cid(), true) // but not on network
73+
err := ig.Routing().Client(pinfo).Provide(context.Background(), block.Cid(), true) // but not on network
7674
if err != nil {
7775
t.Fatal(err)
7876
}
@@ -90,7 +88,7 @@ func TestProviderForKeyButNetworkCannotFind(t *testing.T) { // TODO revisit this
9088
}
9189

9290
func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {
93-
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
91+
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
9492
block := blocks.NewBlock([]byte("block"))
9593
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
9694
defer ig.Close()
@@ -118,7 +116,7 @@ func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {
118116
}
119117

120118
func TestDoesNotProvideWhenConfiguredNotTo(t *testing.T) {
121-
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
119+
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
122120
block := blocks.NewBlock([]byte("block"))
123121
bsOpts := []bitswap.Option{bitswap.ProviderSearchDelay(50 * time.Millisecond)}
124122
ig := testinstance.NewTestInstanceGenerator(net, nil, bsOpts)
@@ -150,7 +148,7 @@ func TestDoesNotProvideWhenConfiguredNotTo(t *testing.T) {
150148
// Tests that a received block is not stored in the blockstore if the block was
151149
// not requested by the client
152150
func TestUnwantedBlockNotAdded(t *testing.T) {
153-
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
151+
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
154152
block := blocks.NewBlock([]byte("block"))
155153
bsMessage := bsmsg.New(true)
156154
bsMessage.AddBlock(block)
@@ -186,7 +184,7 @@ func TestUnwantedBlockNotAdded(t *testing.T) {
186184
// (because the live request queue is full)
187185
func TestPendingBlockAdded(t *testing.T) {
188186
ctx := context.Background()
189-
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
187+
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
190188
sessionBroadcastWantCapacity := 4
191189

192190
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
@@ -277,7 +275,7 @@ func PerformDistributionTest(t *testing.T, numInstances, numBlocks int) {
277275
if testing.Short() {
278276
t.SkipNow()
279277
}
280-
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
278+
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
281279
ig := testinstance.NewTestInstanceGenerator(net, nil, []bitswap.Option{
282280
bitswap.TaskWorkerCount(5),
283281
bitswap.EngineTaskWorkerCount(5),
@@ -333,7 +331,7 @@ func TestSendToWantingPeer(t *testing.T) {
333331
t.SkipNow()
334332
}
335333

336-
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
334+
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
337335
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
338336
defer ig.Close()
339337

@@ -370,7 +368,7 @@ func TestSendToWantingPeer(t *testing.T) {
370368
}
371369

372370
func TestEmptyKey(t *testing.T) {
373-
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
371+
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
374372
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
375373
defer ig.Close()
376374
bs := ig.Instances(1)[0].Exchange
@@ -403,7 +401,7 @@ func assertStat(t *testing.T, st *bitswap.Stat, sblks, rblks, sdata, rdata uint6
403401
}
404402

405403
func TestBasicBitswap(t *testing.T) {
406-
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
404+
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
407405
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
408406
defer ig.Close()
409407

@@ -474,7 +472,7 @@ func TestBasicBitswap(t *testing.T) {
474472
}
475473

476474
func TestDoubleGet(t *testing.T) {
477-
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
475+
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
478476
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
479477
defer ig.Close()
480478

@@ -538,7 +536,7 @@ func TestDoubleGet(t *testing.T) {
538536
}
539537

540538
func TestWantlistCleanup(t *testing.T) {
541-
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
539+
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
542540
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
543541
defer ig.Close()
544542

@@ -659,7 +657,7 @@ func newReceipt(sent, recv, exchanged uint64) *server.Receipt {
659657
}
660658

661659
func TestBitswapLedgerOneWay(t *testing.T) {
662-
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
660+
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
663661
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
664662
defer ig.Close()
665663

@@ -707,7 +705,7 @@ func TestBitswapLedgerOneWay(t *testing.T) {
707705
}
708706

709707
func TestBitswapLedgerTwoWay(t *testing.T) {
710-
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
708+
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
711709
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
712710
defer ig.Close()
713711

@@ -795,7 +793,7 @@ func (tsl *testingScoreLedger) Stop() {
795793
// Tests start and stop of a custom decision logic
796794
func TestWithScoreLedger(t *testing.T) {
797795
tsl := newTestingScoreLedger()
798-
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
796+
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
799797
bsOpts := []bitswap.Option{bitswap.WithScoreLedger(tsl)}
800798
ig := testinstance.NewTestInstanceGenerator(net, nil, bsOpts)
801799
defer ig.Close()

bitswap/client/bitswap_with_sessions_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/ipfs/boxo/bitswap/client/traceability"
1313
testinstance "github.com/ipfs/boxo/bitswap/testinstance"
1414
tn "github.com/ipfs/boxo/bitswap/testnet"
15-
mockrouting "github.com/ipfs/boxo/routing/mock"
1615
blocks "github.com/ipfs/go-block-format"
1716
cid "github.com/ipfs/go-cid"
1817
delay "github.com/ipfs/go-ipfs-delay"
@@ -26,7 +25,7 @@ const blockSize = 4
2625
func getVirtualNetwork() tn.Network {
2726
// FIXME: the tests are really sensitive to the network delay. fix them to work
2827
// well under varying conditions
29-
return tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(0))
28+
return tn.VirtualNetwork(delay.Fixed(0))
3029
}
3130

3231
func addBlock(t *testing.T, ctx context.Context, inst testinstance.Instance, blk blocks.Block) {
@@ -115,7 +114,7 @@ func TestSessionBetweenPeers(t *testing.T) {
115114
ctx, cancel := context.WithCancel(context.Background())
116115
defer cancel()
117116

118-
vnet := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(time.Millisecond))
117+
vnet := tn.VirtualNetwork(delay.Fixed(time.Millisecond))
119118
ig := testinstance.NewTestInstanceGenerator(vnet, nil, []bitswap.Option{bitswap.SetSimulateDontHavesOnTimeout(false)})
120119
defer ig.Close()
121120

bitswap/testinstance/testinstance.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ func ConnectInstances(instances []Instance) {
8686
}
8787
}
8888

89+
// Routing returns the mock routing server
90+
func (g *InstanceGenerator) Routing() mockrouting.Server {
91+
return g.routing
92+
}
93+
8994
// Instance is a test instance of bitswap + dependencies for integration testing
9095
type Instance struct {
9196
Peer peer.ID

bitswap/testnet/network_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
bsmsg "github.com/ipfs/boxo/bitswap/message"
99
bsnet "github.com/ipfs/boxo/bitswap/network"
1010

11-
mockrouting "github.com/ipfs/boxo/routing/mock"
1211
blocks "github.com/ipfs/go-block-format"
1312
delay "github.com/ipfs/go-ipfs-delay"
1413

@@ -17,7 +16,7 @@ import (
1716
)
1817

1918
func TestSendMessageAsyncButWaitForResponse(t *testing.T) {
20-
net := VirtualNetwork(mockrouting.NewServer(), delay.Fixed(0))
19+
net := VirtualNetwork(delay.Fixed(0))
2120
responderPeer := tnet.RandIdentityOrFatal(t)
2221
waiter := net.Adapter(tnet.RandIdentityOrFatal(t))
2322
responder := net.Adapter(responderPeer)

bitswap/testnet/virtual.go

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,23 @@ import (
1111
bsmsg "github.com/ipfs/boxo/bitswap/message"
1212
bsnet "github.com/ipfs/boxo/bitswap/network"
1313

14-
mockrouting "github.com/ipfs/boxo/routing/mock"
15-
cid "github.com/ipfs/go-cid"
1614
delay "github.com/ipfs/go-ipfs-delay"
1715

1816
tnet "github.com/libp2p/go-libp2p-testing/net"
1917
"github.com/libp2p/go-libp2p/core/connmgr"
2018
"github.com/libp2p/go-libp2p/core/peer"
2119
protocol "github.com/libp2p/go-libp2p/core/protocol"
22-
"github.com/libp2p/go-libp2p/core/routing"
2320
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
2421
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
2522
)
2623

2724
// VirtualNetwork generates a new testnet instance - a fake network that
2825
// is used to simulate sending messages.
29-
func VirtualNetwork(rs mockrouting.Server, d delay.D) Network {
26+
func VirtualNetwork(d delay.D) Network {
3027
return &network{
3128
latencies: make(map[peer.ID]map[peer.ID]time.Duration),
3229
clients: make(map[peer.ID]*receiverQueue),
3330
delay: d,
34-
routingserver: rs,
3531
isRateLimited: false,
3632
rateLimitGenerator: nil,
3733
conns: make(map[string]struct{}),
@@ -45,13 +41,12 @@ type RateLimitGenerator interface {
4541

4642
// RateLimitedVirtualNetwork generates a testnet instance where nodes are rate
4743
// limited in the upload/download speed.
48-
func RateLimitedVirtualNetwork(rs mockrouting.Server, d delay.D, rateLimitGenerator RateLimitGenerator) Network {
44+
func RateLimitedVirtualNetwork(d delay.D, rateLimitGenerator RateLimitGenerator) Network {
4945
return &network{
5046
latencies: make(map[peer.ID]map[peer.ID]time.Duration),
5147
rateLimiters: make(map[peer.ID]map[peer.ID]*mocknet.RateLimiter),
5248
clients: make(map[peer.ID]*receiverQueue),
5349
delay: d,
54-
routingserver: rs,
5550
isRateLimited: true,
5651
rateLimitGenerator: rateLimitGenerator,
5752
conns: make(map[string]struct{}),
@@ -63,7 +58,6 @@ type network struct {
6358
latencies map[peer.ID]map[peer.ID]time.Duration
6459
rateLimiters map[peer.ID]map[peer.ID]*mocknet.RateLimiter
6560
clients map[peer.ID]*receiverQueue
66-
routingserver mockrouting.Server
6761
delay delay.D
6862
isRateLimited bool
6963
rateLimitGenerator RateLimitGenerator
@@ -105,7 +99,6 @@ func (n *network) Adapter(p tnet.Identity, opts ...bsnet.NetOpt) bsnet.BitSwapNe
10599
client := &networkClient{
106100
local: p.ID(),
107101
network: n,
108-
routing: n.routingserver.Client(p),
109102
supportedProtocols: s.SupportedProtocols,
110103
}
111104
n.clients[p.ID()] = &receiverQueue{receiver: client}
@@ -192,7 +185,6 @@ type networkClient struct {
192185
local peer.ID
193186
receivers []bsnet.Receiver
194187
network *network
195-
routing routing.Routing
196188
supportedProtocols []protocol.ID
197189
}
198190

@@ -253,27 +245,6 @@ func (nc *networkClient) Stats() bsnet.Stats {
253245
}
254246
}
255247

256-
// FindProvidersAsync returns a channel of providers for the given key.
257-
func (nc *networkClient) FindProvidersAsync(ctx context.Context, k cid.Cid, max int) <-chan peer.AddrInfo {
258-
// NB: this function duplicates the AddrInfo -> ID transformation in the
259-
// bitswap network adapter. Not to worry. This network client will be
260-
// deprecated once the ipfsnet.Mock is added. The code below is only
261-
// temporary.
262-
263-
out := make(chan peer.AddrInfo)
264-
go func() {
265-
defer close(out)
266-
providers := nc.routing.FindProvidersAsync(ctx, k, max)
267-
for info := range providers {
268-
select {
269-
case <-ctx.Done():
270-
case out <- info:
271-
}
272-
}
273-
}()
274-
return out
275-
}
276-
277248
func (nc *networkClient) ConnectionManager() connmgr.ConnManager {
278249
return &connmgr.NullConnMgr{}
279250
}
@@ -322,11 +293,6 @@ func (nc *networkClient) NewMessageSender(ctx context.Context, p peer.ID, opts *
322293
}, nil
323294
}
324295

325-
// Provide provides the key to the network.
326-
func (nc *networkClient) Provide(ctx context.Context, k cid.Cid) error {
327-
return nc.routing.Provide(ctx, k, true)
328-
}
329-
330296
func (nc *networkClient) Start(r ...bsnet.Receiver) {
331297
nc.receivers = r
332298
}

0 commit comments

Comments
 (0)