Skip to content
Draft
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
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,17 @@ endif
.PHONY: test-ci
test-ci:
ifdef cover
$(GO) test -run "[^FLAKY]$$" -coverprofile=cover.out ./...
$(GO) test -run "[^FLAKY]$$" -coverprofile=cover.out ./pkg/topology/kademlia
else
$(GO) test -run "[^FLAKY]$$" ./...
$(GO) test -run "[^FLAKY]$$" ./pkg/topology/kademlia
endif

.PHONY: test-ci-race
test-ci-race:
ifdef cover
$(GO) test -race -run "[^FLAKY]$$" -coverprofile=cover.out ./...
$(GO) test -race -run "[^FLAKY]$$" -coverprofile=cover.out ./pkg/topology/kademlia
else
$(GO) test -race -run "[^FLAKY]$$" ./...
$(GO) test -race -run "[^FLAKY]$$" ./pkg/topology/kademlia
endif

.PHONY: test-ci-flaky
Expand Down
71 changes: 36 additions & 35 deletions pkg/topology/kademlia/kademlia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"sync"
"sync/atomic"
"testing"
"testing/synctest"
"time"

ma "github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -885,47 +886,47 @@ func TestAddressBookPrune(t *testing.T) {
}

// test pruning addressbook after successive failed connect attempts
func TestAddressBookQuickPrune_FLAKY(t *testing.T) {
t.Parallel()

var (
conns, failedConns int32 // how many connect calls were made to the p2p mock
base, kad, ab, _, signer = newTestKademlia(t, &conns, &failedConns, kademlia.Options{
TimeToRetry: ptrDuration(time.Millisecond),
})
)
kad.SetStorageRadius(2)
func TestAddressBookQuickPrune(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
var (
conns, failedConns int32 // how many connect calls were made to the p2p mock
base, kad, ab, _, signer = newTestKademlia(t, &conns, &failedConns, kademlia.Options{
TimeToRetry: ptrDuration(time.Millisecond),
})
)
kad.SetStorageRadius(2)

if err := kad.Start(context.Background()); err != nil {
t.Fatal(err)
}
testutil.CleanupCloser(t, kad)
if err := kad.Start(context.Background()); err != nil {
t.Fatal(err)
}
testutil.CleanupCloser(t, kad)

time.Sleep(100 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using synctest.Test, real time.Sleep calls should typically be avoided as they defeat the purpose of deterministic time simulation. Consider removing this sleep or verifying if it's necessary with synctest's time control mechanisms.

Suggested change
time.Sleep(100 * time.Millisecond)
synctest.Advance(t, 100 * time.Millisecond)

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no synctest.Advance in the docs 🙂


nonConnPeer, err := bzz.NewAddress(signer, nonConnectableAddress, swarm.RandAddressAt(t, base, 1), 0, nil)
if err != nil {
t.Fatal(err)
}
if err := ab.Put(nonConnPeer.Overlay, *nonConnPeer); err != nil {
t.Fatal(err)
}
nonConnPeer, err := bzz.NewAddress(signer, nonConnectableAddress, swarm.RandAddressAt(t, base, 1), 0, nil)
if err != nil {
t.Fatal(err)
}
if err := ab.Put(nonConnPeer.Overlay, *nonConnPeer); err != nil {
t.Fatal(err)
}

addr := swarm.RandAddressAt(t, base, 1)
// add one valid peer
addOne(t, signer, kad, ab, addr)
waitCounter(t, &conns, 1)
waitCounter(t, &failedConns, 0)
addr := swarm.RandAddressAt(t, base, 1)
// add one valid peer
addOne(t, signer, kad, ab, addr)
waitCounter(t, &conns, 1)
waitCounter(t, &failedConns, 0)

// add non connectable peer, check connection and failed connection counters
kad.AddPeers(nonConnPeer.Overlay)
waitCounter(t, &conns, 0)
waitCounter(t, &failedConns, 1)
// add non connectable peer, check connection and failed connection counters
kad.AddPeers(nonConnPeer.Overlay)
waitCounter(t, &conns, 0)
waitCounter(t, &failedConns, 1)

_, err = ab.Get(nonConnPeer.Overlay)
if !errors.Is(err, addressbook.ErrNotFound) {
t.Fatal(err)
}
_, err = ab.Get(nonConnPeer.Overlay)
if !errors.Is(err, addressbook.ErrNotFound) {
t.Fatal(err)
}
})
}

func TestClosestPeer(t *testing.T) {
Expand Down
Loading