Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

p2p/discover: fix flaky test which wrote to test.log after completion #30506

Merged
merged 2 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}