Skip to content

Commit

Permalink
p2p/discover: fix flaky tests writing to test.log after completion (#…
Browse files Browse the repository at this point in the history
…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 #30505
  • Loading branch information
holiman committed Sep 26, 2024
1 parent 80b529e commit b5a88da
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
26 changes: 22 additions & 4 deletions p2p/discover/v4_lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"net/netip"
"slices"
"sync"
"testing"

"github.com/ethereum/go-ethereum/crypto"
Expand Down Expand Up @@ -67,15 +68,23 @@ func TestUDPv4_Lookup(t *testing.T) {
func TestUDPv4_LookupIterator(t *testing.T) {
t.Parallel()
test := newUDPTest(t)
defer test.close()
var wg sync.WaitGroup
defer func() {
test.close()
wg.Wait()
}()

// Seed table with initial nodes.
bootnodes := make([]*enode.Node, len(lookupTestnet.dists[256]))
for i := range lookupTestnet.dists[256] {
bootnodes[i] = lookupTestnet.node(256, i)
}
fillTable(test.table, bootnodes, true)
go serveTestnet(test, lookupTestnet)
wg.Add(1)
go func() {
serveTestnet(test, lookupTestnet)
wg.Done()
}()

// Create the iterator and collect the nodes it yields.
iter := test.udp.RandomNodes()
Expand All @@ -102,15 +111,24 @@ func TestUDPv4_LookupIterator(t *testing.T) {
func TestUDPv4_LookupIteratorClose(t *testing.T) {
t.Parallel()
test := newUDPTest(t)
defer test.close()
var wg sync.WaitGroup
defer func() {
test.close()
wg.Wait()
}()

// Seed table with initial nodes.
bootnodes := make([]*enode.Node, len(lookupTestnet.dists[256]))
for i := range lookupTestnet.dists[256] {
bootnodes[i] = lookupTestnet.node(256, i)
}
fillTable(test.table, bootnodes, true)
go serveTestnet(test, lookupTestnet)

wg.Add(1)
go func() {
serveTestnet(test, lookupTestnet)
wg.Done()
}()

it := test.udp.RandomNodes()
if ok := it.Next(); !ok || it.Node() == nil {
Expand Down
6 changes: 5 additions & 1 deletion p2p/enode/nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,10 @@ func nextNode(it iterator.Iterator) *Node {

// Close flushes and closes the database files.
func (db *DB) Close() {
close(db.quit)
select {
case <-db.quit: // already closed
default:
close(db.quit)
}
db.lvl.Close()
}

0 comments on commit b5a88da

Please sign in to comment.