Skip to content
Open
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/p2p/libp2p/internal/reacher
Copy link

Copilot AI Oct 29, 2025

Choose a reason for hiding this comment

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

The test-ci target has been narrowed to run only the reacher package tests. This breaks the CI testing for the rest of the codebase. If this is intended for testing purposes only, it should not be committed. Otherwise, revert to ./... to ensure comprehensive test coverage in CI.

Copilot uses AI. Check for mistakes.
else
$(GO) test -run "[^FLAKY]$$" ./...
$(GO) test -run "[^FLAKY]$$" ./pkg/p2p/libp2p/internal/reacher
Copy link

Copilot AI Oct 29, 2025

Choose a reason for hiding this comment

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

The test-ci target has been narrowed to run only the reacher package tests. This breaks the CI testing for the rest of the codebase. If this is intended for testing purposes only, it should not be committed. Otherwise, revert to ./... to ensure comprehensive test coverage in CI.

Copilot uses AI. Check for mistakes.
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
Copy link

Copilot AI Oct 29, 2025

Choose a reason for hiding this comment

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

The test-ci-race target has been narrowed to run only the reacher package tests. This breaks the CI race detection testing for the rest of the codebase. If this is intended for testing purposes only, it should not be committed. Otherwise, revert to ./... to ensure comprehensive test coverage in CI.

Copilot uses AI. Check for mistakes.
else
$(GO) test -race -run "[^FLAKY]$$" ./...
$(GO) test -race -run "[^FLAKY]$$" ./pkg/p2p/libp2p/internal/reacher
Copy link

Copilot AI Oct 29, 2025

Choose a reason for hiding this comment

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

The test-ci-race target has been narrowed to run only the reacher package tests. This breaks the CI race detection testing for the rest of the codebase. If this is intended for testing purposes only, it should not be committed. Otherwise, revert to ./... to ensure comprehensive test coverage in CI.

Copilot uses AI. Check for mistakes.
endif

.PHONY: test-ci-flaky
Expand Down
86 changes: 44 additions & 42 deletions pkg/p2p/libp2p/internal/reacher/reacher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
Loading