From 77441be2fa3920bf45ded531961428614762e5f7 Mon Sep 17 00:00:00 2001 From: akrem-chabchoub Date: Wed, 29 Oct 2025 16:25:49 +0100 Subject: [PATCH 1/3] test(libp2p): update reacher tests to use synctest --- Makefile | 8 +- .../libp2p/internal/reacher/reacher_test.go | 86 ++++++++++--------- 2 files changed, 48 insertions(+), 46 deletions(-) diff --git a/Makefile b/Makefile index 047f5d4f8be..ce2a78a9f0f 100644 --- a/Makefile +++ b/Makefile @@ -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/p2p/libp2p/internal/reacher else - $(GO) test -run "[^FLAKY]$$" ./... + $(GO) test -run "[^FLAKY]$$" ./pkg/p2p/libp2p/internal/reacher 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/p2p/libp2p/internal/reacher else - $(GO) test -race -run "[^FLAKY]$$" ./... + $(GO) test -race -run "[^FLAKY]$$" ./pkg/p2p/libp2p/internal/reacher endif .PHONY: test-ci-flaky diff --git a/pkg/p2p/libp2p/internal/reacher/reacher_test.go b/pkg/p2p/libp2p/internal/reacher/reacher_test.go index 73ba1842b2d..35ac647b0cf 100644 --- a/pkg/p2p/libp2p/internal/reacher/reacher_test.go +++ b/pkg/p2p/libp2p/internal/reacher/reacher_test.go @@ -10,6 +10,8 @@ import ( "testing" "time" + "testing/synctest" + "github.com/ethersphere/bee/v2/pkg/p2p" "github.com/ethersphere/bee/v2/pkg/p2p/libp2p/internal/reacher" "github.com/ethersphere/bee/v2/pkg/swarm" @@ -62,63 +64,63 @@ func TestPingSuccess(t *testing.T) { }, } { t.Run(tc.name, func(t *testing.T) { - t.Parallel() - - done := make(chan struct{}) - mock := newMock(tc.pingFunc, tc.reachableFunc(done)) + synctest.Test(t, func(t *testing.T) { + done := make(chan struct{}) + mock := newMock(tc.pingFunc, tc.reachableFunc(done)) - r := reacher.New(mock, mock, &defaultOptions) - testutil.CleanupCloser(t, r) + r := reacher.New(mock, mock, &defaultOptions) + testutil.CleanupCloser(t, r) - overlay := swarm.RandAddress(t) + overlay := swarm.RandAddress(t) - r.Connected(overlay, nil) + r.Connected(overlay, nil) - select { - case <-time.After(time.Second * 5): - t.Fatalf("test timed out") - case <-done: - } + select { + case <-time.After(time.Second * 5): + t.Fatalf("test timed out") + case <-done: + } + }) }) } } func TestDisconnected(t *testing.T) { - t.Parallel() - - var ( - disconnectedOverlay = swarm.RandAddress(t) - disconnectedMa, _ = ma.NewMultiaddr("/ip4/127.0.0.1/tcp/7071/p2p/16Uiu2HAmTBuJT9LvNmBiQiNoTsxE5mtNy6YG3paw79m94CRa9sRb") - ) - - /* - Because the Disconnected is called after Connected, it may be that one of the workers - have picked up the peer already. So to test that the Disconnected really works, - if the ping function pings the peer we are trying to disconnect, we return an error - which triggers another attempt in the future, which by the, the peer should already be removed. - */ - var errs atomic.Int64 - pingFunc := func(_ context.Context, a ma.Multiaddr) (time.Duration, error) { - if a != nil && a.Equal(disconnectedMa) { - errs.Inc() - if errs.Load() > 1 { - t.Fatalf("overlay should be disconnected already") + synctest.Test(t, func(t *testing.T) { + var ( + disconnectedOverlay = swarm.RandAddress(t) + disconnectedMa, _ = ma.NewMultiaddr("/ip4/127.0.0.1/tcp/7071/p2p/16Uiu2HAmTBuJT9LvNmBiQiNoTsxE5mtNy6YG3paw79m94CRa9sRb") + ) + + /* + Because the Disconnected is called after Connected, it may be that one of the workers + have picked up the peer already. So to test that the Disconnected really works, + if the ping function pings the peer we are trying to disconnect, we return an error + which triggers another attempt in the future, which by the, the peer should already be removed. + */ + var errs atomic.Int64 + pingFunc := func(_ context.Context, a ma.Multiaddr) (time.Duration, error) { + if a != nil && a.Equal(disconnectedMa) { + errs.Inc() + if errs.Load() > 1 { + t.Fatalf("overlay should be disconnected already") + } + return 0, errors.New("test error") } - return 0, errors.New("test error") + return 0, nil } - return 0, nil - } - reachableFunc := func(addr swarm.Address, b p2p.ReachabilityStatus) {} + reachableFunc := func(addr swarm.Address, b p2p.ReachabilityStatus) {} - mock := newMock(pingFunc, reachableFunc) + mock := newMock(pingFunc, reachableFunc) - r := reacher.New(mock, mock, &defaultOptions) - testutil.CleanupCloser(t, r) + r := reacher.New(mock, mock, &defaultOptions) + testutil.CleanupCloser(t, r) - r.Connected(swarm.RandAddress(t), nil) - r.Connected(disconnectedOverlay, disconnectedMa) - r.Disconnected(disconnectedOverlay) + r.Connected(swarm.RandAddress(t), nil) + r.Connected(disconnectedOverlay, disconnectedMa) + r.Disconnected(disconnectedOverlay) + }) } type mock struct { From e20a1ab4417aecc666d9332a99eb2aab1033242c Mon Sep 17 00:00:00 2001 From: akrem-chabchoub Date: Thu, 20 Nov 2025 17:11:05 +0100 Subject: [PATCH 2/3] refactor(Makefile): update test commands to run all packages instead of specific path --- Makefile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index ce2a78a9f0f..047f5d4f8be 100644 --- a/Makefile +++ b/Makefile @@ -121,17 +121,17 @@ endif .PHONY: test-ci test-ci: ifdef cover - $(GO) test -run "[^FLAKY]$$" -coverprofile=cover.out ./pkg/p2p/libp2p/internal/reacher + $(GO) test -run "[^FLAKY]$$" -coverprofile=cover.out ./... else - $(GO) test -run "[^FLAKY]$$" ./pkg/p2p/libp2p/internal/reacher + $(GO) test -run "[^FLAKY]$$" ./... endif .PHONY: test-ci-race test-ci-race: ifdef cover - $(GO) test -race -run "[^FLAKY]$$" -coverprofile=cover.out ./pkg/p2p/libp2p/internal/reacher + $(GO) test -race -run "[^FLAKY]$$" -coverprofile=cover.out ./... else - $(GO) test -race -run "[^FLAKY]$$" ./pkg/p2p/libp2p/internal/reacher + $(GO) test -race -run "[^FLAKY]$$" ./... endif .PHONY: test-ci-flaky From 3e18e9599456cdbe8e25b3e3a438baaaf101004e Mon Sep 17 00:00:00 2001 From: akrem-chabchoub Date: Tue, 2 Dec 2025 00:07:03 +0100 Subject: [PATCH 3/3] fix: update multiaddr peer ID in reacher test --- pkg/p2p/libp2p/internal/reacher/reacher_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/p2p/libp2p/internal/reacher/reacher_test.go b/pkg/p2p/libp2p/internal/reacher/reacher_test.go index 49cd07e82a9..515e05eab88 100644 --- a/pkg/p2p/libp2p/internal/reacher/reacher_test.go +++ b/pkg/p2p/libp2p/internal/reacher/reacher_test.go @@ -73,7 +73,7 @@ func TestPingSuccess(t *testing.T) { testutil.CleanupCloser(t, r) overlay := swarm.RandAddress(t) - addr, _ := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/7071/p2p/16Uiu2HAmTBuJT9LvNmQiNoTsxE5mtNy6YG3paw79m94CRa9sRb") + addr, _ := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/7071/p2p/16Uiu2HAmTBuJT9LvNmBiQiNoTsxE5mtNy6YG3paw79m94CRa9sRb") r.Connected(overlay, addr)