Skip to content

Commit ccfdb14

Browse files
holimanjakub-freebit
authored andcommitted
p2p/discover: fix flaky tests writing to test.log after completion (ethereum#30506)
This PR fixes two tests, which had a tendency to sometimes write to the `*testing.T` `log` facility after the test function had completed, which is not allowed. This PR fixes it by using waitgroups to ensure that the handler/logwriter terminates before the test exits. closes ethereum#30505
1 parent 5aac5eb commit ccfdb14

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

p2p/discover/v4_lookup_test.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"crypto/ecdsa"
2121
"fmt"
2222
"net/netip"
23+
"sync"
2324
"testing"
2425

2526
"github.com/ethereum/go-ethereum/crypto"
@@ -67,15 +68,23 @@ func TestUDPv4_Lookup(t *testing.T) {
6768
func TestUDPv4_LookupIterator(t *testing.T) {
6869
t.Parallel()
6970
test := newUDPTest(t)
70-
defer test.close()
71+
var wg sync.WaitGroup
72+
defer func() {
73+
test.close()
74+
wg.Wait()
75+
}()
7176

7277
// Seed table with initial nodes.
7378
bootnodes := make([]*enode.Node, len(lookupTestnet.dists[256]))
7479
for i := range lookupTestnet.dists[256] {
7580
bootnodes[i] = lookupTestnet.node(256, i)
7681
}
7782
fillTable(test.table, bootnodes, true)
78-
go serveTestnet(test, lookupTestnet)
83+
wg.Add(1)
84+
go func() {
85+
serveTestnet(test, lookupTestnet)
86+
wg.Done()
87+
}()
7988

8089
// Create the iterator and collect the nodes it yields.
8190
iter := test.udp.RandomNodes()
@@ -102,15 +111,24 @@ func TestUDPv4_LookupIterator(t *testing.T) {
102111
func TestUDPv4_LookupIteratorClose(t *testing.T) {
103112
t.Parallel()
104113
test := newUDPTest(t)
105-
defer test.close()
114+
var wg sync.WaitGroup
115+
defer func() {
116+
test.close()
117+
wg.Wait()
118+
}()
106119

107120
// Seed table with initial nodes.
108121
bootnodes := make([]*enode.Node, len(lookupTestnet.dists[256]))
109122
for i := range lookupTestnet.dists[256] {
110123
bootnodes[i] = lookupTestnet.node(256, i)
111124
}
112125
fillTable(test.table, bootnodes, true)
113-
go serveTestnet(test, lookupTestnet)
126+
127+
wg.Add(1)
128+
go func() {
129+
serveTestnet(test, lookupTestnet)
130+
wg.Done()
131+
}()
114132

115133
it := test.udp.RandomNodes()
116134
if ok := it.Next(); !ok || it.Node() == nil {

p2p/enode/nodedb.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,10 @@ func nextNode(it iterator.Iterator) *Node {
500500

501501
// Close flushes and closes the database files.
502502
func (db *DB) Close() {
503-
close(db.quit)
503+
select {
504+
case <-db.quit: // already closed
505+
default:
506+
close(db.quit)
507+
}
504508
db.lvl.Close()
505509
}

0 commit comments

Comments
 (0)