Skip to content

Commit 6c70c2f

Browse files
committed
test: fix query test
Also, fail if the routing tables aren't well-formed. It turns out there were a couple of issues with this test: 1. It used too many file descriptors. 2. It tried to query the DHT without bootstrapping any nodes. That's never going to work.
1 parent c2d0d8c commit 6c70c2f

File tree

1 file changed

+46
-43
lines changed

1 file changed

+46
-43
lines changed

dht_test.go

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
kb "github.com/libp2p/go-libp2p-kbucket"
3333
record "github.com/libp2p/go-libp2p-record"
3434
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
35-
"github.com/libp2p/go-libp2p-testing/ci"
3635
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
3736
ma "github.com/multiformats/go-multiaddr"
3837

@@ -129,7 +128,7 @@ func setupDHT(ctx context.Context, t *testing.T, client bool, options ...Option)
129128
return d
130129
}
131130

132-
func setupDHTS(t *testing.T, ctx context.Context, n int) []*IpfsDHT {
131+
func setupDHTS(t *testing.T, ctx context.Context, n int, options ...Option) []*IpfsDHT {
133132
addrs := make([]ma.Multiaddr, n)
134133
dhts := make([]*IpfsDHT, n)
135134
peers := make([]peer.ID, n)
@@ -138,7 +137,7 @@ func setupDHTS(t *testing.T, ctx context.Context, n int) []*IpfsDHT {
138137
sanityPeersMap := make(map[string]struct{})
139138

140139
for i := 0; i < n; i++ {
141-
dhts[i] = setupDHT(ctx, t, false)
140+
dhts[i] = setupDHT(ctx, t, false, options...)
142141
peers[i] = dhts[i].PeerID()
143142
addrs[i] = dhts[i].host.Addrs()[0]
144143

@@ -673,8 +672,9 @@ func TestLocalProvides(t *testing.T) {
673672
}
674673

675674
// if minPeers or avgPeers is 0, dont test for it.
676-
func waitForWellFormedTables(t *testing.T, dhts []*IpfsDHT, minPeers, avgPeers int, timeout time.Duration) bool {
675+
func waitForWellFormedTables(t *testing.T, dhts []*IpfsDHT, minPeers, avgPeers int, timeout time.Duration) {
677676
// test "well-formed-ness" (>= minPeers peers in every routing table)
677+
t.Helper()
678678

679679
checkTables := func() bool {
680680
totalPeers := 0
@@ -699,11 +699,12 @@ func waitForWellFormedTables(t *testing.T, dhts []*IpfsDHT, minPeers, avgPeers i
699699
for {
700700
select {
701701
case <-timeoutA:
702-
logger.Debugf("did not reach well-formed routing tables by %s", timeout)
703-
return false // failed
702+
t.Errorf("failed to reach well-formed routing tables after %s", timeout)
703+
return
704704
case <-time.After(5 * time.Millisecond):
705705
if checkTables() {
706-
return true // succeeded
706+
// succeeded
707+
return
707708
}
708709
}
709710
}
@@ -760,6 +761,7 @@ func TestRefresh(t *testing.T) {
760761
}
761762

762763
waitForWellFormedTables(t, dhts, 7, 10, 10*time.Second)
764+
763765
cancelT()
764766

765767
if u.Debug {
@@ -830,9 +832,6 @@ func TestRefreshBelowMinRTThreshold(t *testing.T) {
830832
}
831833

832834
func TestPeriodicRefresh(t *testing.T) {
833-
if ci.IsRunning() {
834-
t.Skip("skipping on CI. highly timing dependent")
835-
}
836835
if testing.Short() {
837836
t.SkipNow()
838837
}
@@ -894,7 +893,6 @@ func TestPeriodicRefresh(t *testing.T) {
894893
}
895894

896895
func TestProvidesMany(t *testing.T) {
897-
t.Skip("this test doesn't work")
898896
ctx, cancel := context.WithCancel(context.Background())
899897
defer cancel()
900898

@@ -1149,9 +1147,6 @@ func TestConnectCollision(t *testing.T) {
11491147
if testing.Short() {
11501148
t.SkipNow()
11511149
}
1152-
if ci.IsRunning() {
1153-
t.Skip("Skipping on CI.")
1154-
}
11551150

11561151
runTimes := 10
11571152

@@ -1337,7 +1332,7 @@ func minInt(a, b int) int {
13371332
}
13381333

13391334
func TestFindPeerQueryMinimal(t *testing.T) {
1340-
testFindPeerQuery(t, 2, 22, 11)
1335+
testFindPeerQuery(t, 2, 22, 1)
13411336
}
13421337

13431338
func TestFindPeerQuery(t *testing.T) {
@@ -1348,62 +1343,70 @@ func TestFindPeerQuery(t *testing.T) {
13481343
if testing.Short() {
13491344
t.Skip("skipping test in short mode")
13501345
}
1351-
if curFileLimit() < 1024 {
1352-
t.Skip("insufficient file descriptors available")
1353-
}
1354-
testFindPeerQuery(t, 20, 80, 16)
1346+
testFindPeerQuery(t, 5, 40, 3)
13551347
}
13561348

13571349
// NOTE: You must have ATLEAST (minRTRefreshThreshold+1) test peers before using this.
13581350
func testFindPeerQuery(t *testing.T,
13591351
bootstrappers, // Number of nodes connected to the querying node
13601352
leafs, // Number of nodes that might be connected to from the bootstrappers
1361-
bootstrapperLeafConns int, // Number of connections each bootstrapper has to the leaf nodes
1353+
bootstrapConns int, // Number of bootstrappers each leaf should connect to.
13621354
) {
13631355
ctx, cancel := context.WithCancel(context.Background())
13641356
defer cancel()
13651357

1366-
dhts := setupDHTS(t, ctx, 1+bootstrappers+leafs)
1358+
dhts := setupDHTS(t, ctx, 1+bootstrappers+leafs, BucketSize(4))
13671359
defer func() {
13681360
for _, d := range dhts {
13691361
d.Close()
13701362
d.host.Close()
13711363
}
13721364
}()
13731365

1366+
t.Log("connecting")
1367+
13741368
mrand := rand.New(rand.NewSource(42))
13751369
guy := dhts[0]
13761370
others := dhts[1:]
1377-
for i := 0; i < bootstrappers; i++ {
1378-
for j := 0; j < bootstrapperLeafConns; j++ {
1379-
v := mrand.Intn(leafs)
1380-
connectNoSync(t, ctx, others[i], others[bootstrappers+v])
1371+
for i := 0; i < leafs; i++ {
1372+
for _, v := range mrand.Perm(bootstrappers)[:bootstrapConns] {
1373+
connectNoSync(t, ctx, others[v], others[bootstrappers+i])
13811374
}
13821375
}
13831376

13841377
for i := 0; i < bootstrappers; i++ {
13851378
connectNoSync(t, ctx, guy, others[i])
13861379
}
13871380

1381+
t.Log("waiting for routing tables")
1382+
13881383
// give some time for things to settle down
1389-
waitForWellFormedTables(t, dhts, minRTRefreshThreshold, minRTRefreshThreshold, 5*time.Second)
1384+
waitForWellFormedTables(t, dhts, bootstrapConns, bootstrapConns, 5*time.Second)
13901385

1391-
for _, d := range dhts {
1392-
if len(d.RoutingTable().ListPeers()) > 0 {
1393-
if err := <-d.RefreshRoutingTable(); err != nil {
1394-
t.Fatal(err)
1395-
}
1396-
}
1386+
t.Log("refreshing")
1387+
1388+
var wg sync.WaitGroup
1389+
for _, dht := range dhts {
1390+
wg.Add(1)
1391+
go func(d *IpfsDHT) {
1392+
<-d.RefreshRoutingTable()
1393+
wg.Done()
1394+
}(dht)
13971395
}
13981396

1399-
var reachableIds []peer.ID
1400-
for i, d := range dhts {
1401-
lp := len(d.host.Network().Peers())
1402-
if i != 0 && lp > 0 {
1403-
reachableIds = append(reachableIds, d.PeerID())
1404-
}
1397+
wg.Wait()
1398+
1399+
t.Log("waiting for routing tables again")
1400+
1401+
// Wait for refresh to work. At least one bucket should be full.
1402+
waitForWellFormedTables(t, dhts, 4, 0, 5*time.Second)
1403+
1404+
var peers []peer.ID
1405+
for _, d := range others {
1406+
peers = append(peers, d.PeerID())
14051407
}
1406-
t.Logf("%d reachable ids", len(reachableIds))
1408+
1409+
t.Log("querying")
14071410

14081411
val := "foobar"
14091412
rtval := kb.ConvertKey(val)
@@ -1418,7 +1421,7 @@ func testFindPeerQuery(t *testing.T,
14181421

14191422
sort.Sort(peer.IDSlice(outpeers))
14201423

1421-
exp := kb.SortClosestPeers(reachableIds, rtval)[:minInt(guy.bucketSize, len(reachableIds))]
1424+
exp := kb.SortClosestPeers(peers, rtval)[:minInt(guy.bucketSize, len(peers))]
14221425
t.Logf("got %d peers", len(outpeers))
14231426
got := kb.SortClosestPeers(outpeers, rtval)
14241427

@@ -1588,19 +1591,19 @@ func TestHandleRemotePeerProtocolChanges(t *testing.T) {
15881591
connect(t, ctx, dhtA, dhtB)
15891592

15901593
// now assert both have each other in their RT
1591-
require.True(t, waitForWellFormedTables(t, []*IpfsDHT{dhtA, dhtB}, 1, 1, 10*time.Second), "both RT should have one peer each")
1594+
waitForWellFormedTables(t, []*IpfsDHT{dhtA, dhtB}, 1, 1, 10*time.Second)
15921595

15931596
// dhtB becomes a client
15941597
require.NoError(t, dhtB.setMode(modeClient))
15951598

15961599
// which means that dhtA should evict it from it's RT
1597-
require.True(t, waitForWellFormedTables(t, []*IpfsDHT{dhtA}, 0, 0, 10*time.Second), "dHTA routing table should have 0 peers")
1600+
waitForWellFormedTables(t, []*IpfsDHT{dhtA}, 0, 0, 10*time.Second)
15981601

15991602
// dhtB becomes a server
16001603
require.NoError(t, dhtB.setMode(modeServer))
16011604

16021605
// which means dhtA should have it in the RT again because of fixLowPeers
1603-
require.True(t, waitForWellFormedTables(t, []*IpfsDHT{dhtA}, 1, 1, 10*time.Second), "dHTA routing table should have 1 peers")
1606+
waitForWellFormedTables(t, []*IpfsDHT{dhtA}, 1, 1, 10*time.Second)
16041607
}
16051608

16061609
func TestGetSetPluggedProtocol(t *testing.T) {

0 commit comments

Comments
 (0)