Skip to content

Commit

Permalink
test: fix test problem
Browse files Browse the repository at this point in the history
  • Loading branch information
Shawn-Huang-Tron committed Aug 23, 2024
1 parent c39fa8d commit 2057f88
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 41 deletions.
6 changes: 3 additions & 3 deletions core/corehttp/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func TestPeersTotal(t *testing.T) {
ctx := context.Background()

hosts := make([]*bhost.BasicHost, 4)
var err error
for i := 0; i < 4; i++ {
var err error
hosts[i], err = bhost.NewHost(swarmt.GenSwarm(t), nil)
if err != nil {
t.Fatalf("Failed to dial: %s", err)
t.Fatal(err)
}
}

Expand All @@ -49,7 +49,7 @@ func TestPeersTotal(t *testing.T) {
t.Fatalf("expected at most 2 peers transport (tcp and upd/quic), got %d, transport map %v",
len(peersTransport), peersTransport)
}
totalPeers := peersTransport["/ip4/tcp"] + peersTransport["/ip4/udp/quic"]
totalPeers := peersTransport["/ip4/tcp"] + peersTransport["/ip4/udp/quic-v1"]
if totalPeers != 3 {
t.Fatalf("expected 3 peers in either tcp or upd/quic transport, got %f", totalPeers)
}
Expand Down
78 changes: 40 additions & 38 deletions test/integration/wan_lan_dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ func TestDHTConnectivitySlowRouting(t *testing.T) {
}

// wan prefix must have a real corresponding ASN for the peer diversity filter to work.
var wanPrefix = net.ParseIP("2001:218:3004::")
var lanPrefix = net.ParseIP("fe80::")
var (
wanPrefix = net.ParseIP("2001:218:3004::")
lanPrefix = net.ParseIP("fe80::")
)

func makeAddr(n uint32, wan bool) ma.Multiaddr {
var ip net.IP
Expand Down Expand Up @@ -150,13 +152,12 @@ StartupWait:
for {
select {
case err := <-testPeer.DHT.LAN.RefreshRoutingTable():
// if err != nil {
// fmt.Printf("Error refreshing routing table: %v\n", err)
// }
if err != nil {
fmt.Printf("Error refreshing routing table: %v\n", err)
}
if testPeer.DHT.LAN.RoutingTable() == nil ||
testPeer.DHT.LAN.RoutingTable().Size() == 0 ||
err != nil {
//delay the sleep time so that the LAN can find all each other
time.Sleep(100 * time.Millisecond)
continue
}
Expand All @@ -169,29 +170,29 @@ StartupWait:
startupCancel()

// choose a lan peer and validate lan DHT is functioning.
// i := rand.Intn(len(lanPeers))
// if testPeer.PeerHost.Network().Connectedness(lanPeers[i].Identity) == corenet.Connected {
// i = (i + 1) % len(lanPeers)
// if testPeer.PeerHost.Network().Connectedness(lanPeers[i].Identity) == corenet.Connected {
// _ = testPeer.PeerHost.Network().ClosePeer(lanPeers[i].Identity)
// testPeer.PeerHost.Peerstore().ClearAddrs(lanPeers[i].Identity)
// }
// }
// // That peer will provide a new CID, and we'll validate the test node can find it.
i := rand.Intn(len(lanPeers))
if testPeer.PeerHost.Network().Connectedness(lanPeers[i].Identity) == corenet.Connected {
i = (i + 1) % len(lanPeers)
if testPeer.PeerHost.Network().Connectedness(lanPeers[i].Identity) == corenet.Connected {
_ = testPeer.PeerHost.Network().ClosePeer(lanPeers[i].Identity)
testPeer.PeerHost.Peerstore().ClearAddrs(lanPeers[i].Identity)
}
}
// That peer will provide a new CID, and we'll validate the test node can find it.
provideCid := cid.NewCidV1(cid.Raw, []byte("Lan Provide Record"))
// provideCtx, cancel := context.WithTimeout(ctx, time.Second)
// defer cancel()
// if err := lanPeers[i].DHT.Provide(provideCtx, provideCid, true); err != nil {
// return err
// }
// provChan := testPeer.DHT.FindProvidersAsync(provideCtx, provideCid, 0)
// prov, ok := <-provChan
// if !ok || prov.ID == "" {
// return fmt.Errorf("Expected provider. stream closed early, ok is: %t, prov is %+v", ok, prov)
// }
// if prov.ID != lanPeers[i].Identity {
// return fmt.Errorf("Unexpected lan peer provided record")
// }
provideCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
if err := lanPeers[i].DHT.Provide(provideCtx, provideCid, true); err != nil {
return err
}
provChan := testPeer.DHT.FindProvidersAsync(provideCtx, provideCid, 0)
prov, ok := <-provChan
if !ok || prov.ID == "" {
return fmt.Errorf("Expected provider. stream closed early")
}
if prov.ID != lanPeers[i].Identity {
return fmt.Errorf("Unexpected lan peer provided record")
}

// Now, connect with a wan peer.
for _, p := range wanPeers {
Expand All @@ -210,12 +211,13 @@ WanStartupWait:
for {
select {
case err := <-testPeer.DHT.WAN.RefreshRoutingTable():
//if err != nil {
// if err != nil {
// fmt.Printf("Error refreshing routing table: %v\n", err)
//}
// }
if testPeer.DHT.WAN.RoutingTable() == nil ||
testPeer.DHT.WAN.RoutingTable().Size() == 0 ||
err != nil {
time.Sleep(100 * time.Millisecond)
continue
}
break WanStartupWait
Expand All @@ -227,7 +229,7 @@ WanStartupWait:
startupCancel()

// choose a wan peer and validate wan DHT is functioning.
i := rand.Intn(len(wanPeers))
i = rand.Intn(len(wanPeers))
if testPeer.PeerHost.Network().Connectedness(wanPeers[i].Identity) == corenet.Connected {
i = (i + 1) % len(wanPeers)
if testPeer.PeerHost.Network().Connectedness(wanPeers[i].Identity) == corenet.Connected {
Expand All @@ -243,8 +245,8 @@ WanStartupWait:
if err := wanPeers[i].DHT.Provide(wanProvideCtx, wanCid, true); err != nil {
return err
}
provChan := testPeer.DHT.FindProvidersAsync(wanProvideCtx, wanCid, 0)
prov, ok := <-provChan
provChan = testPeer.DHT.FindProvidersAsync(wanProvideCtx, wanCid, 0)
prov, ok = <-provChan
if !ok || prov.ID == "" {
return fmt.Errorf("Expected one provider, closed early")
}
Expand All @@ -259,7 +261,7 @@ WanStartupWait:
testPeer.PeerHost.Peerstore().ClearAddrs(wanPeers[i].Identity)
}

provideCtx, cancel := context.WithTimeout(ctx, time.Second)
provideCtx, cancel = context.WithTimeout(ctx, time.Second)
defer cancel()
if err := wanPeers[i].DHT.Provide(provideCtx, provideCid, true); err != nil {
return err
Expand All @@ -269,10 +271,10 @@ WanStartupWait:
if !ok {
return fmt.Errorf("Expected two providers, got 0")
}
// prov, ok = <-provChan
// if !ok {
// return fmt.Errorf("Expected two providers, got 1")
// }
prov, ok = <-provChan
if !ok {
return fmt.Errorf("Expected two providers, got 1")
}

return nil
}

0 comments on commit 2057f88

Please sign in to comment.