Skip to content

Commit d9c74a4

Browse files
committed
p2p/discover: fix TestUDPv4_findnode data race with revalidation ping
1 parent 0413af4 commit d9c74a4

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

p2p/discover/v4_udp_test.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,20 +282,46 @@ func TestUDPv4_findnode(t *testing.T) {
282282
expected := test.table.findnodeByID(testTarget.ID(), bucketSize, true)
283283
test.packetIn(nil, &v4wire.Findnode{Target: testTarget, Expiration: futureExp})
284284
waitNeighbors := func(want []*enode.Node) {
285-
test.waitPacketOut(func(p *v4wire.Neighbors, to netip.AddrPort, hash []byte) {
286-
if len(p.Nodes) != len(want) {
287-
t.Errorf("wrong number of results: got %d, want %d", len(p.Nodes), len(want))
285+
timeout := time.After(1 * time.Minute)
286+
for {
287+
select {
288+
case <-timeout:
289+
t.Fatal("timeout waiting for neighbors response")
290+
default:
291+
}
292+
dgram, err := test.pipe.receive()
293+
if err == errClosed {
294+
t.Fatal("socket closed before receiving neighbors")
295+
} else if err != nil {
296+
t.Fatal("packet receive error:", err)
297+
}
298+
p, _, _, err := v4wire.Decode(dgram.data)
299+
if err != nil {
300+
t.Fatalf("sent packet decode error: %v", err)
301+
}
302+
// Skip any PING packets from revalidation
303+
if _, ok := p.(*v4wire.Ping); ok {
304+
continue
305+
}
306+
// Check we got NEIGHBORS
307+
neighbors, ok := p.(*v4wire.Neighbors)
308+
if !ok {
309+
t.Fatalf("sent packet type mismatch, got: %T, want: *v4wire.Neighbors", p)
310+
}
311+
if len(neighbors.Nodes) != len(want) {
312+
t.Errorf("wrong number of results: got %d, want %d", len(neighbors.Nodes), len(want))
288313
return
289314
}
290-
for i, n := range p.Nodes {
315+
for i, n := range neighbors.Nodes {
291316
if n.ID.ID() != want[i].ID() {
292317
t.Errorf("result mismatch at %d:\n got: %v\n want: %v", i, n, expected.entries[i])
293318
}
294319
if !live[n.ID.ID()] {
295320
t.Errorf("result includes dead node %v", n.ID.ID())
296321
}
297322
}
298-
})
323+
return
324+
}
299325
}
300326
// Receive replies.
301327
want := expected.entries

0 commit comments

Comments
 (0)