diff --git a/.github/actions/go-test-setup/action.yml b/.github/actions/go-test-setup/action.yml index 1836ae8dc3..b85c81334d 100644 --- a/.github/actions/go-test-setup/action.yml +++ b/.github/actions/go-test-setup/action.yml @@ -9,10 +9,10 @@ runs: shell: bash # This matches only tests with "NoCover" in their test name to avoid running all tests again. run: go test -tags nocover -run NoCover -v ./... - - name: Run synctests tests. These are tests that require go 1.24 and the experimental testing/synctest package + - name: Run synctests tests. These are tests that require go 1.25 and the experimental testing/synctest package shell: bash - if: ${{ contains(matrix.go, '1.24') }} - run: go test -tags goexperiment.synctest -run "_synctest$" -v ./... + if: ${{ contains(matrix.go, '1.25') }} + run: go test -run "_synctest$" -v ./... - name: Install testing tools shell: bash run: cd scripts/test_analysis && go install ./cmd/gotest2sql diff --git a/go.mod b/go.mod index 1557cfb590..055f3fc275 100644 --- a/go.mod +++ b/go.mod @@ -29,7 +29,7 @@ require ( github.com/libp2p/go-reuseport v0.4.0 github.com/libp2p/go-yamux/v5 v5.0.1 github.com/libp2p/zeroconf/v2 v2.2.0 - github.com/marcopolo/simnet v0.0.1 + github.com/marcopolo/simnet v0.0.4 github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b github.com/mr-tron/base58 v1.2.0 diff --git a/go.sum b/go.sum index 1f3fe63c90..9d97d537f8 100644 --- a/go.sum +++ b/go.sum @@ -66,8 +66,8 @@ github.com/libp2p/go-yamux/v5 v5.0.1 h1:f0WoX/bEF2E8SbE4c/k1Mo+/9z0O4oC/hWEA+nfY github.com/libp2p/go-yamux/v5 v5.0.1/go.mod h1:en+3cdX51U0ZslwRdRLrvQsdayFt3TSUKvBGErzpWbU= github.com/libp2p/zeroconf/v2 v2.2.0 h1:Cup06Jv6u81HLhIj1KasuNM/RHHrJ8T7wOTS4+Tv53Q= github.com/libp2p/zeroconf/v2 v2.2.0/go.mod h1:fuJqLnUwZTshS3U/bMRJ3+ow/v9oid1n0DmyYyNO1Xs= -github.com/marcopolo/simnet v0.0.1 h1:rSMslhPz6q9IvJeFWDoMGxMIrlsbXau3NkuIXHGJxfg= -github.com/marcopolo/simnet v0.0.1/go.mod h1:WDaQkgLAjqDUEBAOXz22+1j6wXKfGlC5sD5XWt3ddOs= +github.com/marcopolo/simnet v0.0.4 h1:50Kx4hS9kFGSRIbrt9xUS3NJX33EyPqHVmpXvaKLqrY= +github.com/marcopolo/simnet v0.0.4/go.mod h1:tfQF1u2DmaB6WHODMtQaLtClEf3a296CKQLq5gAsIS0= github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd h1:br0buuQ854V8u83wA0rVZ8ttrq5CpaPZdvrK0LP2lOk= github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd/go.mod h1:QuCEs1Nt24+FYQEqAAncTDPJIuGs+LxK1MCiFL25pMU= github.com/miekg/dns v1.1.43/go.mod h1:+evo5L0630/F6ca/Z9+GAqzhjGyn8/c+TBaOyfEl0V4= diff --git a/p2p/protocol/holepunch/holepunch_test.go b/p2p/protocol/holepunch/holepunch_test.go index 47a7ce1f10..0d246fe343 100644 --- a/p2p/protocol/holepunch/holepunch_test.go +++ b/p2p/protocol/holepunch/holepunch_test.go @@ -637,7 +637,8 @@ func quicSimnet(isPubliclyReachably bool, router *simnet.SimpleFirewallRouter) l if isPubliclyReachably { router.SetAddrPubliclyReachable(address) } - c := simnet.NewSimConn(address, router) + c := simnet.NewSimConn(address) + router.AddNode(address, c) return c, nil })) } diff --git a/x/simlibp2p/libp2p.go b/x/simlibp2p/libp2p.go index 47e745bb3d..873526effc 100644 --- a/x/simlibp2p/libp2p.go +++ b/x/simlibp2p/libp2p.go @@ -1,6 +1,7 @@ -package simconnlibp2p +package simlibp2p import ( + "context" "crypto/rand" "fmt" "net" @@ -206,8 +207,12 @@ type NetworkSettings struct { BlankHostOptsForHostIdx func(idx int) BlankHostOpts } -func SimpleLibp2pNetwork(linkSettings []NodeLinkSettingsAndCount, networkSettings NetworkSettings) (*simnet.Simnet, *SimpleLibp2pNetworkMeta, error) { - nw := &simnet.Simnet{} +type LatencyFunc func(*simnet.Packet) time.Duration + +func SimpleLibp2pNetwork(linkSettings []NodeLinkSettingsAndCount, latencyFunc LatencyFunc, networkSettings NetworkSettings) (*simnet.Simnet, *SimpleLibp2pNetworkMeta, error) { + nw := &simnet.Simnet{ + LatencyFunc: latencyFunc, + } meta := &SimpleLibp2pNetworkMeta{ AddrToNode: make(map[string]HostAndIdx), } @@ -256,3 +261,28 @@ func SimpleLibp2pNetwork(linkSettings []NodeLinkSettingsAndCount, networkSetting return nw, meta, nil } + +// GetBasicHostPair gets a new pair of hosts. +// The first host initiates the connection to the second host. +func GetBasicHostPair(t *testing.T) (host.Host, host.Host) { + network, meta, err := SimpleLibp2pNetwork([]NodeLinkSettingsAndCount{{ + LinkSettings: simnet.NodeBiDiLinkSettings{ + Downlink: simnet.LinkSettings{BitsPerSecond: 20 * OneMbps}, + Uplink: simnet.LinkSettings{BitsPerSecond: 20 * OneMbps}, + }, Count: 2}, + }, simnet.StaticLatency(100/2*time.Millisecond), NetworkSettings{}) + require.NoError(t, err) + network.Start() + t.Cleanup(func() { + network.Close() + }) + + h1 := meta.Nodes[0] + h2 := meta.Nodes[1] + + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + h2pi := h2.Peerstore().PeerInfo(h2.ID()) + require.NoError(t, h1.Connect(ctx, h2pi)) + return h1, h2 +} diff --git a/x/simlibp2p/synctest_test.go b/x/simlibp2p/synctest_test.go index 691c517dc8..cb0ffa340b 100644 --- a/x/simlibp2p/synctest_test.go +++ b/x/simlibp2p/synctest_test.go @@ -1,6 +1,6 @@ -//go:build goexperiment.synctest +//go:build go1.25 -package simconnlibp2p_test +package simlibp2p_test import ( "context" @@ -13,20 +13,20 @@ import ( "github.com/libp2p/go-libp2p" "github.com/libp2p/go-libp2p/core/peer" "github.com/libp2p/go-libp2p/p2p/protocol/ping" - simlibp2p "github.com/libp2p/go-libp2p/x/simlibp2p" + "github.com/libp2p/go-libp2p/x/simlibp2p" "github.com/marcopolo/simnet" "github.com/stretchr/testify/require" ) func TestSimpleLibp2pNetwork_synctest(t *testing.T) { - synctest.Run(func() { + synctest.Test(t, func(t *testing.T) { latency := 10 * time.Millisecond network, meta, err := simlibp2p.SimpleLibp2pNetwork([]simlibp2p.NodeLinkSettingsAndCount{ {LinkSettings: simnet.NodeBiDiLinkSettings{ - Downlink: simnet.LinkSettings{BitsPerSecond: 20 * simlibp2p.OneMbps, Latency: latency / 2}, // Divide by two since this is latency for each direction - Uplink: simnet.LinkSettings{BitsPerSecond: 20 * simlibp2p.OneMbps, Latency: latency / 2}, + Downlink: simnet.LinkSettings{BitsPerSecond: 20 * simlibp2p.OneMbps}, // Divide by two since this is latency for each direction + Uplink: simnet.LinkSettings{BitsPerSecond: 20 * simlibp2p.OneMbps}, }, Count: 100}, - }, simlibp2p.NetworkSettings{}) + }, simnet.StaticLatency(latency/2), simlibp2p.NetworkSettings{}) require.NoError(t, err) network.Start() defer network.Close() @@ -74,19 +74,17 @@ func TestSimpleLibp2pNetwork_synctest(t *testing.T) { } func TestSimpleSimNetPing_synctest(t *testing.T) { - synctest.Run(func() { - router := &simnet.Simnet{} + synctest.Test(t, func(t *testing.T) { + const latency = 10 * time.Millisecond + router := &simnet.Simnet{LatencyFunc: simnet.StaticLatency(latency / 2)} const bandwidth = 10 * simlibp2p.OneMbps - const latency = 10 * time.Millisecond linkSettings := simnet.NodeBiDiLinkSettings{ Downlink: simnet.LinkSettings{ BitsPerSecond: bandwidth, - Latency: latency / 2, }, Uplink: simnet.LinkSettings{ BitsPerSecond: bandwidth, - Latency: latency / 2, }, } @@ -101,14 +99,13 @@ func TestSimpleSimNetPing_synctest(t *testing.T) { simlibp2p.QUICSimnet(router, linkSettings), ) - err := router.Start() - require.NoError(t, err) + router.Start() defer router.Close() defer hostA.Close() defer hostB.Close() - err = hostA.Connect(context.Background(), peer.AddrInfo{ + err := hostA.Connect(context.Background(), peer.AddrInfo{ ID: hostB.ID(), Addrs: hostB.Addrs(), })