From 85a3a7f52407586fea6957187c461aaa08241611 Mon Sep 17 00:00:00 2001 From: Sean DuBois Date: Sat, 23 Mar 2024 20:28:47 -0400 Subject: [PATCH] Simplify usage of test.TimeOut() Execute directly instead of allocating function --- active_tcp_test.go | 6 ++--- agent_handlers_test.go | 1 - agent_test.go | 39 ++++++++++-------------------- agent_udpmux_test.go | 3 +-- candidate_relay_test.go | 3 +-- candidate_server_reflexive_test.go | 3 +-- connectivity_vnet_test.go | 6 ++--- gather_test.go | 37 +++++++++------------------- mdns_test.go | 9 +++---- transport_test.go | 12 +++------ transport_vnet_test.go | 3 +-- udp_mux_multi_test.go | 6 ++--- udp_mux_test.go | 3 +-- 13 files changed, 43 insertions(+), 88 deletions(-) diff --git a/active_tcp_test.go b/active_tcp_test.go index 39b9a35..3df2c5e 100644 --- a/active_tcp_test.go +++ b/active_tcp_test.go @@ -37,8 +37,7 @@ func ipv6Available(t *testing.T) bool { func TestActiveTCP(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 5) - defer lim.Stop() + defer test.TimeOut(time.Second * 5).Stop() const listenPort = 7686 type testCase struct { @@ -164,8 +163,7 @@ func TestActiveTCP(t *testing.T) { func TestActiveTCP_NonBlocking(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 5) - defer lim.Stop() + defer test.TimeOut(time.Second * 5).Stop() cfg := &AgentConfig{ NetworkTypes: supportedNetworkTypes(), diff --git a/agent_handlers_test.go b/agent_handlers_test.go index 025d473..35680ee 100644 --- a/agent_handlers_test.go +++ b/agent_handlers_test.go @@ -12,7 +12,6 @@ import ( func TestConnectionStateNotifier(t *testing.T) { t.Run("TestManyUpdates", func(t *testing.T) { - defer test.CheckRoutines(t)() updates := make(chan struct{}, 1) diff --git a/agent_test.go b/agent_test.go index dfb83f0..0207476 100644 --- a/agent_test.go +++ b/agent_test.go @@ -37,8 +37,7 @@ func TestHandlePeerReflexive(t *testing.T) { defer test.CheckRoutines(t)() // Limit runtime in case of deadlocks - lim := test.TimeOut(time.Second * 2) - defer lim.Stop() + defer test.TimeOut(time.Second * 2).Stop() t.Run("UDP prflx candidate from handleInbound()", func(t *testing.T) { a, err := NewAgent(&AgentConfig{}) @@ -184,8 +183,7 @@ func TestHandlePeerReflexive(t *testing.T) { func TestConnectivityOnStartup(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() // Create a network with two interfaces wan, err := vnet.NewRouter(&vnet.RouterConfig{ @@ -292,8 +290,7 @@ func TestConnectivityOnStartup(t *testing.T) { func TestConnectivityLite(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() stunServerURL := &stun.URI{ Scheme: SchemeTypeSTUN, @@ -535,8 +532,7 @@ func TestInvalidAgentStarts(t *testing.T) { func TestConnectionStateCallback(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 5) - defer lim.Stop() + defer test.TimeOut(time.Second * 5).Stop() disconnectedDuration := time.Second failedDuration := time.Second @@ -1074,8 +1070,7 @@ func TestAgentCredentials(t *testing.T) { func TestConnectionStateFailedDeleteAllCandidates(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 5) - defer lim.Stop() + defer test.TimeOut(time.Second * 5).Stop() oneSecond := time.Second KeepaliveInterval := time.Duration(0) @@ -1119,8 +1114,7 @@ func TestConnectionStateFailedDeleteAllCandidates(t *testing.T) { func TestConnectionStateConnectingToFailed(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 5) - defer lim.Stop() + defer test.TimeOut(time.Second * 5).Stop() oneSecond := time.Second KeepaliveInterval := time.Duration(0) @@ -1178,8 +1172,7 @@ func TestConnectionStateConnectingToFailed(t *testing.T) { func TestAgentRestart(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() oneSecond := time.Second @@ -1375,8 +1368,7 @@ func TestGetLocalCandidates(t *testing.T) { func TestCloseInConnectionStateCallback(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 5) - defer lim.Stop() + defer test.TimeOut(time.Second * 5).Stop() disconnectedDuration := time.Second failedDuration := time.Second @@ -1428,8 +1420,7 @@ func TestCloseInConnectionStateCallback(t *testing.T) { func TestRunTaskInConnectionStateCallback(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 5) - defer lim.Stop() + defer test.TimeOut(time.Second * 5).Stop() oneSecond := time.Second KeepaliveInterval := time.Duration(0) @@ -1472,8 +1463,7 @@ func TestRunTaskInConnectionStateCallback(t *testing.T) { func TestRunTaskInSelectedCandidatePairChangeCallback(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 5) - defer lim.Stop() + defer test.TimeOut(time.Second * 5).Stop() oneSecond := time.Second KeepaliveInterval := time.Duration(0) @@ -1524,8 +1514,7 @@ func TestRunTaskInSelectedCandidatePairChangeCallback(t *testing.T) { func TestLiteLifecycle(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() aNotifier, aConnected := onConnected() @@ -1598,8 +1587,7 @@ func TestNilCandidatePair(t *testing.T) { func TestGetSelectedCandidatePair(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() wan, err := vnet.NewRouter(&vnet.RouterConfig{ CIDR: "0.0.0.0/0", @@ -1655,8 +1643,7 @@ func TestGetSelectedCandidatePair(t *testing.T) { func TestAcceptAggressiveNomination(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() // Create a network with two interfaces wan, err := vnet.NewRouter(&vnet.RouterConfig{ diff --git a/agent_udpmux_test.go b/agent_udpmux_test.go index 8899da7..70e7d25 100644 --- a/agent_udpmux_test.go +++ b/agent_udpmux_test.go @@ -20,8 +20,7 @@ import ( func TestMuxAgent(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() const muxPort = 7686 diff --git a/candidate_relay_test.go b/candidate_relay_test.go index e4a7352..5a98a5b 100644 --- a/candidate_relay_test.go +++ b/candidate_relay_test.go @@ -24,8 +24,7 @@ func optimisticAuthHandler(string, string, net.Addr) (key []byte, ok bool) { func TestRelayOnlyConnection(t *testing.T) { // Limit runtime in case of deadlocks - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() defer test.CheckRoutines(t)() diff --git a/candidate_server_reflexive_test.go b/candidate_server_reflexive_test.go index 3f4361d..3d8244a 100644 --- a/candidate_server_reflexive_test.go +++ b/candidate_server_reflexive_test.go @@ -22,8 +22,7 @@ func TestServerReflexiveOnlyConnection(t *testing.T) { defer test.CheckRoutines(t)() // Limit runtime in case of deadlocks - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() serverPort := randomPort(t) serverListener, err := net.ListenPacket("udp4", "127.0.0.1:"+strconv.Itoa(serverPort)) diff --git a/connectivity_vnet_test.go b/connectivity_vnet_test.go index 2aa48f4..f85ac54 100644 --- a/connectivity_vnet_test.go +++ b/connectivity_vnet_test.go @@ -450,8 +450,7 @@ func TestConnectivityVNet(t *testing.T) { func TestDisconnectedToConnected(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 10) - defer lim.Stop() + defer test.TimeOut(time.Second * 10).Stop() loggerFactory := logging.NewDefaultLoggerFactory() @@ -547,8 +546,7 @@ func TestDisconnectedToConnected(t *testing.T) { func TestWriteUseValidPair(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 10) - defer lim.Stop() + defer test.TimeOut(time.Second * 10).Stop() loggerFactory := logging.NewDefaultLoggerFactory() diff --git a/gather_test.go b/gather_test.go index 554d515..1c14fe7 100644 --- a/gather_test.go +++ b/gather_test.go @@ -92,8 +92,7 @@ func TestListenUDP(t *testing.T) { func TestGatherConcurrency(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() a, err := NewAgent(&AgentConfig{ NetworkTypes: []NetworkType{NetworkTypeUDP4, NetworkTypeUDP6}, @@ -119,8 +118,7 @@ func TestGatherConcurrency(t *testing.T) { func TestLoopbackCandidate(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() type testCase struct { name string agentConfig *AgentConfig @@ -229,8 +227,7 @@ func TestLoopbackCandidate(t *testing.T) { func TestSTUNConcurrency(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() serverPort := randomPort(t) serverListener, err := net.ListenPacket("udp4", localhostIPStr+":"+strconv.Itoa(serverPort)) @@ -304,8 +301,7 @@ func TestSTUNConcurrency(t *testing.T) { func TestTURNConcurrency(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() runTest := func(protocol stun.ProtoType, scheme stun.SchemeType, packetConn net.PacketConn, listener net.Listener, serverPort int) { packetConnConfigs := []turn.PacketConnConfig{} @@ -421,8 +417,7 @@ func TestTURNConcurrency(t *testing.T) { func TestSTUNTURNConcurrency(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 8) - defer lim.Stop() + defer test.TimeOut(time.Second * 8).Stop() serverPort := randomPort(t) serverListener, err := net.ListenPacket("udp4", localhostIPStr+":"+strconv.Itoa(serverPort)) @@ -465,7 +460,7 @@ func TestSTUNTURNConcurrency(t *testing.T) { require.NoError(t, err) { - gatherLim := test.TimeOut(time.Second * 3) // As TURN and STUN should be checked in parallel, this should complete before the default STUN timeout (5s) + defer test.TimeOut(time.Second * 3).Stop() // As TURN and STUN should be checked in parallel, this should complete before the default STUN timeout (5s) candidateGathered, candidateGatheredFunc := context.WithCancel(context.Background()) require.NoError(t, a.OnCandidate(func(c Candidate) { if c != nil { @@ -475,8 +470,6 @@ func TestSTUNTURNConcurrency(t *testing.T) { require.NoError(t, a.GatherCandidates()) <-candidateGathered.Done() - - gatherLim.Stop() } require.NoError(t, a.Close()) @@ -492,8 +485,7 @@ func TestSTUNTURNConcurrency(t *testing.T) { func TestTURNSrflx(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() serverPort := randomPort(t) serverListener, err := net.ListenPacket("udp4", localhostIPStr+":"+strconv.Itoa(serverPort)) @@ -577,8 +569,7 @@ func (m *mockProxy) Dial(string, string) (net.Conn, error) { func TestTURNProxyDialer(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() proxyWasDialed, proxyWasDialedFunc := context.WithCancel(context.Background()) proxy.RegisterDialerType("tcp", func(*url.URL, proxy.Dialer) (proxy.Dialer, error) { @@ -627,8 +618,7 @@ func TestTURNProxyDialer(t *testing.T) { func TestUDPMuxDefaultWithNAT1To1IPsUsage(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() conn, err := net.ListenPacket("udp4", ":0") require.NoError(t, err) @@ -670,8 +660,7 @@ func TestUDPMuxDefaultWithNAT1To1IPsUsage(t *testing.T) { func TestMultiUDPMuxUsage(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() var expectedPorts []int var udpMuxInstances []UDPMux @@ -726,8 +715,7 @@ func TestMultiUDPMuxUsage(t *testing.T) { func TestMultiTCPMuxUsage(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() var expectedPorts []int var tcpMuxInstances []TCPMux @@ -785,8 +773,7 @@ func TestMultiTCPMuxUsage(t *testing.T) { func TestUniversalUDPMuxUsage(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() conn, err := net.ListenUDP("udp4", &net.UDPAddr{IP: net.IP{127, 0, 0, 1}, Port: randomPort(t)}) require.NoError(t, err) diff --git a/mdns_test.go b/mdns_test.go index 48eeb59..8c9a5d7 100644 --- a/mdns_test.go +++ b/mdns_test.go @@ -20,8 +20,7 @@ func TestMulticastDNSOnlyConnection(t *testing.T) { defer test.CheckRoutines(t)() // Limit runtime in case of deadlocks - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() cfg := &AgentConfig{ NetworkTypes: []NetworkType{NetworkTypeUDP4}, @@ -61,8 +60,7 @@ func TestMulticastDNSMixedConnection(t *testing.T) { defer test.CheckRoutines(t)() // Limit runtime in case of deadlocks - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() aAgent, err := NewAgent(&AgentConfig{ NetworkTypes: []NetworkType{NetworkTypeUDP4}, @@ -103,8 +101,7 @@ func TestMulticastDNSMixedConnection(t *testing.T) { func TestMulticastDNSStaticHostName(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() _, err := NewAgent(&AgentConfig{ NetworkTypes: []NetworkType{NetworkTypeUDP4}, diff --git a/transport_test.go b/transport_test.go index 80a6bef..ef79773 100644 --- a/transport_test.go +++ b/transport_test.go @@ -22,8 +22,7 @@ func TestStressDuplex(t *testing.T) { defer test.CheckRoutines(t)() // Limit runtime in case of deadlocks - lim := test.TimeOut(time.Second * 20) - defer lim.Stop() + defer test.TimeOut(time.Second * 20).Stop() // Run the test stressDuplex(t) @@ -78,8 +77,7 @@ func TestTimeout(t *testing.T) { defer test.CheckRoutines(t)() // Limit runtime in case of deadlocks - lim := test.TimeOut(time.Second * 20) - defer lim.Stop() + defer test.TimeOut(time.Second * 20).Stop() t.Run("WithoutDisconnectTimeout", func(t *testing.T) { ca, cb := pipe(nil) @@ -109,8 +107,7 @@ func TestReadClosed(t *testing.T) { defer test.CheckRoutines(t)() // Limit runtime in case of deadlocks - lim := test.TimeOut(time.Second * 20) - defer lim.Stop() + defer test.TimeOut(time.Second * 20).Stop() ca, cb := pipe(nil) @@ -321,8 +318,7 @@ func TestConnStats(t *testing.T) { defer test.CheckRoutines(t)() // Limit runtime in case of deadlocks - lim := test.TimeOut(time.Second * 20) - defer lim.Stop() + defer test.TimeOut(time.Second * 20).Stop() ca, cb := pipe(nil) if _, err := ca.Write(make([]byte, 10)); err != nil { diff --git a/transport_vnet_test.go b/transport_vnet_test.go index ff27ec6..36a22f5 100644 --- a/transport_vnet_test.go +++ b/transport_vnet_test.go @@ -23,8 +23,7 @@ func TestRemoteLocalAddr(t *testing.T) { defer test.CheckRoutines(t)() // Limit runtime in case of deadlocks - lim := test.TimeOut(time.Second * 20) - defer lim.Stop() + defer test.TimeOut(time.Second * 20).Stop() // Agent0 is behind 1:1 NAT natType0 := &vnet.NATType{Mode: vnet.NATModeNAT1To1} diff --git a/udp_mux_multi_test.go b/udp_mux_multi_test.go index 2f250f9..bb12022 100644 --- a/udp_mux_multi_test.go +++ b/udp_mux_multi_test.go @@ -20,8 +20,7 @@ import ( func TestMultiUDPMux(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() conn1, err := net.ListenUDP(udp, &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1)}) require.NoError(t, err) @@ -112,8 +111,7 @@ func testMultiUDPMuxConnections(t *testing.T, udpMuxMulti *MultiUDPMuxDefault, u func TestUnspecifiedUDPMux(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() muxPort := 7778 udpMuxMulti, err := NewMultiUDPMuxFromPort(muxPort, UDPMuxFromPortWithInterfaceFilter(func(s string) bool { diff --git a/udp_mux_test.go b/udp_mux_test.go index 253d38e..5f8b1e0 100644 --- a/udp_mux_test.go +++ b/udp_mux_test.go @@ -23,8 +23,7 @@ import ( func TestUDPMux(t *testing.T) { defer test.CheckRoutines(t)() - lim := test.TimeOut(time.Second * 30) - defer lim.Stop() + defer test.TimeOut(time.Second * 30).Stop() conn4, err := net.ListenUDP(udp, &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1)}) require.NoError(t, err)