p2p/discover: fix flaky tests writing to test.log after completion (#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
This commit is contained in:
parent
80b529ea71
commit
b5a88dafae
@ -21,6 +21,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"slices"
|
"slices"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
@ -67,7 +68,11 @@ func TestUDPv4_Lookup(t *testing.T) {
|
|||||||
func TestUDPv4_LookupIterator(t *testing.T) {
|
func TestUDPv4_LookupIterator(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
test := newUDPTest(t)
|
test := newUDPTest(t)
|
||||||
defer test.close()
|
var wg sync.WaitGroup
|
||||||
|
defer func() {
|
||||||
|
test.close()
|
||||||
|
wg.Wait()
|
||||||
|
}()
|
||||||
|
|
||||||
// Seed table with initial nodes.
|
// Seed table with initial nodes.
|
||||||
bootnodes := make([]*enode.Node, len(lookupTestnet.dists[256]))
|
bootnodes := make([]*enode.Node, len(lookupTestnet.dists[256]))
|
||||||
@ -75,7 +80,11 @@ func TestUDPv4_LookupIterator(t *testing.T) {
|
|||||||
bootnodes[i] = lookupTestnet.node(256, i)
|
bootnodes[i] = lookupTestnet.node(256, i)
|
||||||
}
|
}
|
||||||
fillTable(test.table, bootnodes, true)
|
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.
|
// Create the iterator and collect the nodes it yields.
|
||||||
iter := test.udp.RandomNodes()
|
iter := test.udp.RandomNodes()
|
||||||
@ -102,7 +111,11 @@ func TestUDPv4_LookupIterator(t *testing.T) {
|
|||||||
func TestUDPv4_LookupIteratorClose(t *testing.T) {
|
func TestUDPv4_LookupIteratorClose(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
test := newUDPTest(t)
|
test := newUDPTest(t)
|
||||||
defer test.close()
|
var wg sync.WaitGroup
|
||||||
|
defer func() {
|
||||||
|
test.close()
|
||||||
|
wg.Wait()
|
||||||
|
}()
|
||||||
|
|
||||||
// Seed table with initial nodes.
|
// Seed table with initial nodes.
|
||||||
bootnodes := make([]*enode.Node, len(lookupTestnet.dists[256]))
|
bootnodes := make([]*enode.Node, len(lookupTestnet.dists[256]))
|
||||||
@ -110,7 +123,12 @@ func TestUDPv4_LookupIteratorClose(t *testing.T) {
|
|||||||
bootnodes[i] = lookupTestnet.node(256, i)
|
bootnodes[i] = lookupTestnet.node(256, i)
|
||||||
}
|
}
|
||||||
fillTable(test.table, bootnodes, true)
|
fillTable(test.table, bootnodes, true)
|
||||||
go serveTestnet(test, lookupTestnet)
|
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
serveTestnet(test, lookupTestnet)
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
|
||||||
it := test.udp.RandomNodes()
|
it := test.udp.RandomNodes()
|
||||||
if ok := it.Next(); !ok || it.Node() == nil {
|
if ok := it.Next(); !ok || it.Node() == nil {
|
||||||
|
@ -496,6 +496,10 @@ func nextNode(it iterator.Iterator) *Node {
|
|||||||
|
|
||||||
// Close flushes and closes the database files.
|
// Close flushes and closes the database files.
|
||||||
func (db *DB) Close() {
|
func (db *DB) Close() {
|
||||||
close(db.quit)
|
select {
|
||||||
|
case <-db.quit: // already closed
|
||||||
|
default:
|
||||||
|
close(db.quit)
|
||||||
|
}
|
||||||
db.lvl.Close()
|
db.lvl.Close()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user