Skip to content

Commit

Permalink
fix flaky test
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Jun 6, 2023
1 parent b2e8657 commit f893840
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions p2p/net/swarm/swarm_dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@ import (
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
"github.com/libp2p/go-libp2p/core/sec"
"github.com/libp2p/go-libp2p/core/sec/insecure"
"github.com/libp2p/go-libp2p/core/test"
"github.com/libp2p/go-libp2p/p2p/host/eventbus"
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem"
"github.com/libp2p/go-libp2p/p2p/muxer/yamux"
tptu "github.com/libp2p/go-libp2p/p2p/net/upgrader"
quic "github.com/libp2p/go-libp2p/p2p/transport/quic"
"github.com/libp2p/go-libp2p/p2p/transport/quicreuse"

"github.com/libp2p/go-libp2p/p2p/transport/tcp"
"github.com/libp2p/go-libp2p/p2p/transport/websocket"
webtransport "github.com/libp2p/go-libp2p/p2p/transport/webtransport"

ma "github.com/multiformats/go-multiaddr"
madns "github.com/multiformats/go-multiaddr-dns"
manet "github.com/multiformats/go-multiaddr/net"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -130,8 +136,11 @@ func newTestSwarmWithResolver(t *testing.T, resolver *madns.Resolver) *Swarm {
s.Close()
})

st := insecure.NewWithIdentity(insecure.ID, id, priv)
u, err := tptu.New([]sec.SecureTransport{st}, []tptu.StreamMuxer{{ID: yamux.ID, Muxer: yamux.DefaultTransport}}, nil, nil, nil)
require.NoError(t, err)
// Add a tcp transport so that we know we can dial a tcp multiaddr and we don't filter it out.
tpt, err := tcp.NewTCPTransport(nil, &network.NullResourceManager{})
tpt, err := tcp.NewTCPTransport(u, &network.NullResourceManager{})
require.NoError(t, err)
err = s.AddTransport(tpt)
require.NoError(t, err)
Expand Down Expand Up @@ -285,8 +294,22 @@ func TestBlackHoledAddrBlocked(t *testing.T) {
minDials := 5
s.udpBHD = newBlackHoleDetector(func(addr ma.Multiaddr) bool { return true }, "mock", 10, minDials, 5, 0.5)

// reject all dials to the address will be refused as there is no listener
addr := ma.StringCast("/ip4/127.0.0.1/tcp/1234")
// all dials to the address will be rejected
addr := ma.StringCast("/ip4/127.0.0.1/tcp/0")
list, err := manet.Listen(addr)
if err != nil {
t.Error(err)
}
addr = list.Multiaddr()
go func() {
for {
conn, err := list.Accept()
if err != nil {
break
}
conn.Close()
}
}()

p, err := test.RandPeerID()
if err != nil {
Expand All @@ -295,18 +318,14 @@ func TestBlackHoledAddrBlocked(t *testing.T) {
s.Peerstore().AddAddr(p, addr, peerstore.PermanentAddrTTL)
for i := 0; i < minDials; i++ {
s.backf.Clear(p)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
conn, err := s.dialPeer(ctx, p)
cancel()
conn, err := s.dialPeer(context.Background(), p)
if err == nil || conn != nil {
t.Fatalf("expected dial to fail")
}
}

s.backf.Clear(p)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
conn, err := s.dialPeer(ctx, p)
cancel()
conn, err := s.dialPeer(context.Background(), p)
if conn != nil || !errors.Is(err, ErrNoGoodAddresses) {
t.Fatalf("expected dial to be blocked: %s", err)
}
Expand Down

0 comments on commit f893840

Please sign in to comment.