Skip to content

Commit

Permalink
Simplify usage of test.TimeOut()
Browse files Browse the repository at this point in the history
Execute directly instead of allocating function
  • Loading branch information
Sean-Der committed Mar 24, 2024
1 parent 01c3535 commit 85a3a7f
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 88 deletions.
6 changes: 2 additions & 4 deletions active_tcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
Expand Down
1 change: 0 additions & 1 deletion agent_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 13 additions & 26 deletions agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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{
Expand Down
3 changes: 1 addition & 2 deletions agent_udpmux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions candidate_relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)()

Expand Down
3 changes: 1 addition & 2 deletions candidate_server_reflexive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 2 additions & 4 deletions connectivity_vnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand Down
37 changes: 12 additions & 25 deletions gather_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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 {
Expand All @@ -475,8 +470,6 @@ func TestSTUNTURNConcurrency(t *testing.T) {
require.NoError(t, a.GatherCandidates())

<-candidateGathered.Done()

gatherLim.Stop()
}

require.NoError(t, a.Close())
Expand All @@ -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))
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 3 additions & 6 deletions mdns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down

0 comments on commit 85a3a7f

Please sign in to comment.