Skip to content

Commit 66519d7

Browse files
test(libp2p): update reacher tests to use synctest (#5267)
1 parent 37ab9c7 commit 66519d7

File tree

1 file changed

+47
-44
lines changed

1 file changed

+47
-44
lines changed

pkg/p2p/libp2p/internal/reacher/reacher_test.go

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"testing"
1111
"time"
1212

13+
"testing/synctest"
14+
1315
"github.com/ethersphere/bee/v2/pkg/log"
1416
"github.com/ethersphere/bee/v2/pkg/p2p"
1517
"github.com/ethersphere/bee/v2/pkg/p2p/libp2p/internal/reacher"
@@ -63,65 +65,66 @@ func TestPingSuccess(t *testing.T) {
6365
},
6466
} {
6567
t.Run(tc.name, func(t *testing.T) {
66-
t.Parallel()
67-
68-
done := make(chan struct{})
69-
mock := newMock(tc.pingFunc, tc.reachableFunc(done))
68+
synctest.Test(t, func(t *testing.T) {
69+
done := make(chan struct{})
70+
mock := newMock(tc.pingFunc, tc.reachableFunc(done))
7071

71-
r := reacher.New(mock, mock, &defaultOptions, log.Noop)
72-
testutil.CleanupCloser(t, r)
72+
r := reacher.New(mock, mock, &defaultOptions, log.Noop)
73+
testutil.CleanupCloser(t, r)
7374

74-
overlay := swarm.RandAddress(t)
75-
addr, _ := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/7071/p2p/16Uiu2HAmTBuJT9LvNmBiQiNoTsxE5mtNy6YG3paw79m94CRa9sRb")
75+
overlay := swarm.RandAddress(t)
76+
addr, _ := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/7071/p2p/16Uiu2HAmTBuJT9LvNmBiQiNoTsxE5mtNy6YG3paw79m94CRa9sRb")
7677

77-
r.Connected(overlay, addr)
78+
r.Connected(overlay, addr)
7879

79-
select {
80-
case <-time.After(time.Second * 5):
81-
t.Fatalf("test timed out")
82-
case <-done:
83-
}
80+
select {
81+
case <-time.After(time.Second * 5):
82+
t.Fatalf("test timed out")
83+
case <-done:
84+
}
85+
})
8486
})
8587
}
8688
}
8789

8890
func TestDisconnected(t *testing.T) {
89-
t.Parallel()
90-
91-
var (
92-
disconnectedOverlay = swarm.RandAddress(t)
93-
disconnectedMa, _ = ma.NewMultiaddr("/ip4/127.0.0.1/tcp/7071/p2p/16Uiu2HAmTBuJT9LvNmBiQiNoTsxE5mtNy6YG3paw79m94CRa9sRb")
94-
)
95-
96-
/*
97-
Because the Disconnected is called after Connected, it may be that one of the workers
98-
have picked up the peer already. So to test that the Disconnected really works,
99-
if the ping function pings the peer we are trying to disconnect, we return an error
100-
which triggers another attempt in the future, which by the, the peer should already be removed.
101-
*/
102-
var errs atomic.Int64
103-
pingFunc := func(_ context.Context, a ma.Multiaddr) (time.Duration, error) {
104-
if a != nil && a.Equal(disconnectedMa) {
105-
errs.Inc()
106-
if errs.Load() > 1 {
107-
t.Fatalf("overlay should be disconnected already")
91+
synctest.Test(t, func(t *testing.T) {
92+
var (
93+
disconnectedOverlay = swarm.RandAddress(t)
94+
disconnectedMa, _ = ma.NewMultiaddr("/ip4/127.0.0.1/tcp/7071/p2p/16Uiu2HAmTBuJT9LvNmBiQiNoTsxE5mtNy6YG3paw79m94CRa9sRb")
95+
)
96+
97+
/*
98+
Because the Disconnected is called after Connected, it may be that one of the workers
99+
have picked up the peer already. So to test that the Disconnected really works,
100+
if the ping function pings the peer we are trying to disconnect, we return an error
101+
which triggers another attempt in the future, which by the, the peer should already be removed.
102+
*/
103+
104+
var errs atomic.Int64
105+
pingFunc := func(_ context.Context, a ma.Multiaddr) (time.Duration, error) {
106+
if a != nil && a.Equal(disconnectedMa) {
107+
errs.Inc()
108+
if errs.Load() > 1 {
109+
t.Fatalf("overlay should be disconnected already")
110+
}
111+
return 0, errors.New("test error")
108112
}
109-
return 0, errors.New("test error")
113+
return 0, nil
110114
}
111-
return 0, nil
112-
}
113115

114-
reachableFunc := func(addr swarm.Address, b p2p.ReachabilityStatus) {}
116+
reachableFunc := func(addr swarm.Address, b p2p.ReachabilityStatus) {}
115117

116-
mock := newMock(pingFunc, reachableFunc)
118+
mock := newMock(pingFunc, reachableFunc)
117119

118-
r := reacher.New(mock, mock, &defaultOptions, log.Noop)
119-
testutil.CleanupCloser(t, r)
120+
r := reacher.New(mock, mock, &defaultOptions, log.Noop)
121+
testutil.CleanupCloser(t, r)
120122

121-
addr, _ := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/7072/p2p/16Uiu2HAmTBuJT9LvNmBiQiNoTsxE5mtNy6YG3paw79m94CRa9sRb")
122-
r.Connected(swarm.RandAddress(t), addr)
123-
r.Connected(disconnectedOverlay, disconnectedMa)
124-
r.Disconnected(disconnectedOverlay)
123+
addr, _ := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/7072/p2p/16Uiu2HAmTBuJT9LvNmBiQiNoTsxE5mtNy6YG3paw79m94CRa9sRb")
124+
r.Connected(swarm.RandAddress(t), addr)
125+
r.Connected(disconnectedOverlay, disconnectedMa)
126+
r.Disconnected(disconnectedOverlay)
127+
})
125128
}
126129

127130
type mock struct {

0 commit comments

Comments
 (0)