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

This commit is contained in:
Martin Holst Swende 2024-09-25 13:33:27 +02:00
parent 80b529ea71
commit 9e56ade561
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
2 changed files with 23 additions and 3 deletions

View file

@ -21,6 +21,7 @@ import (
"fmt"
"net/netip"
"slices"
"sync"
"testing"
"github.com/ethereum/go-ethereum/crypto"
@ -75,7 +76,12 @@ func TestUDPv4_LookupIterator(t *testing.T) {
bootnodes[i] = lookupTestnet.node(256, i)
}
fillTable(test.table, bootnodes, true)
go serveTestnet(test, lookupTestnet)
var wg sync.WaitGroup
wg.Add(1)
go func() {
serveTestnet(test, lookupTestnet)
wg.Done()
}()
// Create the iterator and collect the nodes it yields.
iter := test.udp.RandomNodes()
@ -95,6 +101,8 @@ func TestUDPv4_LookupIterator(t *testing.T) {
if err := checkNodesEqual(results, want); err != nil {
t.Fatal(err)
}
test.close()
wg.Wait()
}
// TestUDPv4_LookupIteratorClose checks that lookupIterator ends when its Close
@ -110,7 +118,13 @@ func TestUDPv4_LookupIteratorClose(t *testing.T) {
bootnodes[i] = lookupTestnet.node(256, i)
}
fillTable(test.table, bootnodes, true)
go serveTestnet(test, lookupTestnet)
var wg sync.WaitGroup
wg.Add(1)
go func() {
serveTestnet(test, lookupTestnet)
wg.Done()
}()
it := test.udp.RandomNodes()
if ok := it.Next(); !ok || it.Node() == nil {
@ -132,6 +146,8 @@ func TestUDPv4_LookupIteratorClose(t *testing.T) {
if n := it.Node(); n != nil {
t.Errorf("iterator returned non-nil node after close and %d more calls", ncalls)
}
test.close()
wg.Wait()
}
func serveTestnet(test *udpTest, testnet *preminedTestnet) {

View file

@ -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()
}