Skip to content

Commit

Permalink
Clean up Agents in tests more aggressively
Browse files Browse the repository at this point in the history
  • Loading branch information
edaniels committed Jul 25, 2024
1 parent a0385ee commit def1670
Show file tree
Hide file tree
Showing 17 changed files with 361 additions and 176 deletions.
22 changes: 15 additions & 7 deletions active_tcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ func TestActiveTCP(t *testing.T) {
networkTypes: []NetworkType{NetworkTypeTCP6},
listenIPAddress: getLocalIPAddress(t, NetworkTypeTCP6),
selectedPairNetworkType: tcp,
// if we don't use mDNS, we will very liekly be filtering out location tracked ips.
// if we don't use mDNS, we will very likely be filtering out location tracked ips.
useMDNS: true,
},
testCase{
name: "UDP is preferred over TCP6",
networkTypes: supportedNetworkTypes(),
listenIPAddress: getLocalIPAddress(t, NetworkTypeTCP6),
selectedPairNetworkType: udp,
// if we don't use mDNS, we will very liekly be filtering out location tracked ips.
// if we don't use mDNS, we will very likely be filtering out location tracked ips.
useMDNS: true,
},
)
Expand Down Expand Up @@ -143,6 +143,11 @@ func TestActiveTCP(t *testing.T) {
r.NotNil(passiveAgentConn)
r.NotNil(activeAgenConn)

defer func() {
r.NoError(activeAgenConn.Close())
r.NoError(passiveAgentConn.Close())
}()

pair := passiveAgent.getSelectedPair()
r.NotNil(pair)
r.Equal(testCase.selectedPairNetworkType, pair.Local.NetworkType().NetworkShort())
Expand All @@ -163,9 +168,6 @@ func TestActiveTCP(t *testing.T) {
n, err = passiveAgentConn.Read(buffer)
r.NoError(err)
r.Equal(bar, buffer[:n])

r.NoError(activeAgenConn.Close())
r.NoError(passiveAgentConn.Close())
})
}
}
Expand All @@ -185,9 +187,17 @@ func TestActiveTCP_NonBlocking(t *testing.T) {
aAgent, err := NewAgent(cfg)
require.NoError(t, err)

defer func() {
require.NoError(t, aAgent.Close())
}()

bAgent, err := NewAgent(cfg)
require.NoError(t, err)

defer func() {
require.NoError(t, bAgent.Close())
}()

isConnected := make(chan interface{})
err = aAgent.OnConnectionStateChange(func(c ConnectionState) {
if c == ConnectionStateConnected {
Expand All @@ -205,6 +215,4 @@ func TestActiveTCP_NonBlocking(t *testing.T) {
connect(aAgent, bAgent)

<-isConnected
require.NoError(t, aAgent.Close())
require.NoError(t, bAgent.Close())
}
17 changes: 5 additions & 12 deletions agent_get_best_available_candidate_pair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,12 @@ import (
)

func TestNoBestAvailableCandidatePairAfterAgentConstruction(t *testing.T) {
agent := setupTest(t)

require.Nil(t, agent.getBestAvailableCandidatePair())

tearDownTest(t, agent)
}

func setupTest(t *testing.T) *Agent {
agent, err := NewAgent(&AgentConfig{})
require.NoError(t, err)
return agent
}

func tearDownTest(t *testing.T, agent *Agent) {
require.NoError(t, agent.Close())
defer func() {
require.NoError(t, agent.Close())
}()

require.Nil(t, agent.getBestAvailableCandidatePair())
}
5 changes: 3 additions & 2 deletions agent_get_best_valid_candidate_pair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (

func TestAgentGetBestValidCandidatePair(t *testing.T) {
f := setupTestAgentGetBestValidCandidatePair(t)
defer func() {
require.NoError(t, f.sut.Close())
}()

remoteCandidatesFromLowestPriorityToHighest := []Candidate{f.relayRemote, f.srflxRemote, f.prflxRemote, f.hostRemote}

Expand All @@ -26,8 +29,6 @@ func TestAgentGetBestValidCandidatePair(t *testing.T) {

require.Equal(t, actualBestPair.String(), expectedBestPair.String())
}

require.NoError(t, f.sut.Close())
}

func setupTestAgentGetBestValidCandidatePair(t *testing.T) *TestAgentGetBestValidCandidatePairFixture {
Expand Down
4 changes: 3 additions & 1 deletion agent_on_selected_candidate_pair_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (

func TestOnSelectedCandidatePairChange(t *testing.T) {
agent, candidatePair := fixtureTestOnSelectedCandidatePairChange(t)
defer func() {
require.NoError(t, agent.Close())
}()

callbackCalled := make(chan struct{}, 1)
err := agent.OnSelectedCandidatePairChange(func(_, _ Candidate) {
Expand All @@ -28,7 +31,6 @@ func TestOnSelectedCandidatePairChange(t *testing.T) {
require.NoError(t, err)

<-callbackCalled
require.NoError(t, agent.Close())
}

func fixtureTestOnSelectedCandidatePairChange(t *testing.T) (*Agent, *CandidatePair) {
Expand Down
Loading

0 comments on commit def1670

Please sign in to comment.