Skip to content

Commit

Permalink
fix: Flaky TestInvalidServer
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumemichel committed Feb 7, 2025
1 parent 08074be commit 175deb9
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,11 @@ func TestInvalidServer(t *testing.T) {
for _, m := range []*IpfsDHT{m0, m1} {
// Hang on every request.
m.host.SetStreamHandler(protocol, func(s network.Stream) {
select {
case <-ctx.Done():
return
default:
}
r := msgio.NewVarintReaderSize(s, network.MessageSizeMax)
msgbytes, err := r.ReadMsg()
if err != nil {
Expand All @@ -1505,7 +1510,7 @@ func TestInvalidServer(t *testing.T) {
}

// answer with an empty response message
resp := pb.NewMessage(req.GetType(), nil, req.GetClusterLevel())
resp := pb.NewMessage(req.GetType(), make([]byte, 32), req.GetClusterLevel())

// send out response msg
err = net.WriteMsg(s, resp)
Expand All @@ -1526,9 +1531,14 @@ func TestInvalidServer(t *testing.T) {
time.Sleep(time.Millisecond * 5) // just in case...

// find the provider for k from m0
provs, err := m0.FindProviders(ctx, k)
if err != nil {
t.Fatal(err)
maxRetries := 3
var provs []peer.AddrInfo
var err error
for i := 0; i < maxRetries && len(provs) == 0; i++ {
provs, err = m0.FindProviders(ctx, k)
if err != nil {
t.Fatal(err)
}
}
if len(provs) == 0 {
t.Fatal("Expected to get a provider back")
Expand All @@ -1555,7 +1565,10 @@ func TestInvalidServer(t *testing.T) {
// contains more than bucketSize (2) entries, lookupCheck is enabled and m1
// shouldn't be added, because it fails the lookupCheck (hang on all requests).
if s0.routingTable.Find(s1.self) == "" {
t.Fatal("Well behaving DHT server should have been added to the server routing table")
time.Sleep(time.Millisecond * 5) // just in case...
if s0.routingTable.Find(s1.self) == "" {
t.Fatal("Well behaving DHT server should have been added to the server routing table")
}
}
if s0.routingTable.Find(m1.self) != "" {
t.Fatal("Misbehaving DHT servers should not be added to routing table if well populated")
Expand Down

0 comments on commit 175deb9

Please sign in to comment.