Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transport integration tests: transport suite #2295

Closed
wants to merge 2 commits into from
Closed
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
45 changes: 45 additions & 0 deletions p2p/test/transport/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package transport_integration

import (
"regexp"
"testing"

"github.com/libp2p/go-libp2p/core/transport"
ttransport "github.com/libp2p/go-libp2p/p2p/transport/testsuite"
libp2pwebtransport "github.com/libp2p/go-libp2p/p2p/transport/webtransport"
ma "github.com/multiformats/go-multiaddr"
)

func TestWithSuite(t *testing.T) {
portMatcher := regexp.MustCompile(`(tcp|udp)\/\d+`)

for _, tc := range transportsToTest {
t.Run(tc.Name, func(t *testing.T) {
// We use this host just to get a valid multiaddr for this transport.
hostToGetAddr := tc.HostGenerator(t, TransportTestCaseOpts{NoRcmgr: true})
a := ma.StringCast(portMatcher.ReplaceAllString(hostToGetAddr.Addrs()[0].String(), "$1/0"))
hostToGetAddr.Close()

if tc.Name == "WebTransport" {
// Remove certhash components
_, n := libp2pwebtransport.IsWebtransportMultiaddr(a)
for i := 0; i < n; i++ {
a, _ = ma.SplitLast(a)
}
}

listener := tc.HostGenerator(t, TransportTestCaseOpts{NoRcmgr: true, NoListen: true, DisableQuicReuseport: false}) // Transport suite will listen on its own
defer listener.Close()
dialer := tc.HostGenerator(t, TransportTestCaseOpts{NoRcmgr: true, NoListen: true, DisableQuicReuseport: true})
defer dialer.Close()

type transportForListeninger interface {
TransportForListening(a ma.Multiaddr) transport.Transport
}

listenerTransport := listener.Network().(transportForListeninger).TransportForListening(a)
dialerTransport := dialer.Network().(transportForListeninger).TransportForListening(a)
ttransport.SubtestTransport(t, listenerTransport, dialerTransport, a.String(), listener.ID())
})
}
}
12 changes: 9 additions & 3 deletions p2p/test/transport/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
"github.com/libp2p/go-libp2p/p2p/security/noise"
tls "github.com/libp2p/go-libp2p/p2p/security/tls"
"github.com/libp2p/go-libp2p/p2p/transport/quicreuse"
"github.com/stretchr/testify/require"
)

Expand All @@ -31,9 +32,10 @@ type TransportTestCase struct {
}

type TransportTestCaseOpts struct {
NoListen bool
NoRcmgr bool
ConnGater connmgr.ConnectionGater
NoListen bool
DisableQuicReuseport bool
NoRcmgr bool
ConnGater connmgr.ConnectionGater
}

func transformOpts(opts TransportTestCaseOpts) []config.Option {
Expand All @@ -45,6 +47,10 @@ func transformOpts(opts TransportTestCaseOpts) []config.Option {
if opts.ConnGater != nil {
libp2pOpts = append(libp2pOpts, libp2p.ConnectionGater(opts.ConnGater))
}

if opts.DisableQuicReuseport {
libp2pOpts = append(libp2pOpts, libp2p.QUICReuse(quicreuse.NewConnManager, quicreuse.DisableReuseport()))
}
return libp2pOpts
}

Expand Down
30 changes: 19 additions & 11 deletions p2p/transport/testsuite/stream_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,22 +180,30 @@ func SubtestStress(t *testing.T, ta, tb transport.Transport, maddr ma.Multiaddr,
fullClose(t, s)
}

var l transport.Listener
startListening := &sync.Once{}
defer func() {
if l != nil {
l.Close()
}
}()

openConnAndRW := func() {
var wg sync.WaitGroup
defer wg.Wait()

l, err := ta.Listen(maddr)
if err != nil {
t.Error(err)
return
}
defer l.Close()
startListening.Do(func() {
var err error
l, err = ta.Listen(maddr)
if err != nil {
t.Error(err)
return
}

wg.Add(1)
go func() {
defer wg.Done()
serve(t, l)
}()
go func() {
serve(t, l)
}()
})

c, err := tb.Dial(context.Background(), l.Multiaddr(), peerA)
if err != nil {
Expand Down