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 {