Rename TestPairPriority and refactor it

- Use a fixture to simplify the test.
- Remove call to findPair as it has no effect for the test.
- Replace assert with require, remove redundant error messages.
- Better naming.
This commit is contained in:
Artur Shellunts
2023-02-09 18:52:05 +01:00
committed by Arthur Shellunts
parent b7897a6b72
commit 19ffbe7e61
2 changed files with 120 additions and 95 deletions
+120
View File
@@ -0,0 +1,120 @@
//go:build !js
// +build !js
package ice
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAgentGetBestValidCandidatePair(t *testing.T) {
f := setupTestAgentGetBestValidCandidatePair(t)
remoteCandidatesFromLowestPriorityToHighest := []Candidate{f.relayRemote, f.srflxRemote, f.prflxRemote, f.hostRemote}
for _, remoteCandidate := range remoteCandidatesFromLowestPriorityToHighest {
candidatePair := f.sut.addPair(f.hostLocal, remoteCandidate)
candidatePair.state = CandidatePairStateSucceeded
actualBestPair := f.sut.getBestValidCandidatePair()
expectedBestPair := &CandidatePair{Remote: remoteCandidate, Local: f.hostLocal}
require.Equal(t, actualBestPair.String(), expectedBestPair.String())
}
assert.NoError(t, f.sut.Close())
}
func setupTestAgentGetBestValidCandidatePair(t *testing.T) *TestAgentGetBestValidCandidatePairFixture {
fixture := new(TestAgentGetBestValidCandidatePairFixture)
fixture.hostLocal = newHostLocal(t)
fixture.relayRemote = newRelayRemote(t)
fixture.srflxRemote = newSrflxRemote(t)
fixture.prflxRemote = newPrflxRemote(t)
fixture.hostRemote = newHostRemote(t)
agent, err := NewAgent(&AgentConfig{})
require.NoError(t, err)
fixture.sut = agent
return fixture
}
type TestAgentGetBestValidCandidatePairFixture struct {
sut *Agent
hostLocal Candidate
relayRemote Candidate
srflxRemote Candidate
prflxRemote Candidate
hostRemote Candidate
}
func newHostRemote(t *testing.T) *CandidateHost {
remoteHostConfig := &CandidateHostConfig{
Network: "udp",
Address: "1.2.3.5",
Port: 12350,
Component: 1,
}
hostRemote, err := NewCandidateHost(remoteHostConfig)
require.NoError(t, err)
return hostRemote
}
func newPrflxRemote(t *testing.T) *CandidatePeerReflexive {
prflxConfig := &CandidatePeerReflexiveConfig{
Network: "udp",
Address: "10.10.10.2",
Port: 19217,
Component: 1,
RelAddr: "4.3.2.1",
RelPort: 43211,
}
prflxRemote, err := NewCandidatePeerReflexive(prflxConfig)
require.NoError(t, err)
return prflxRemote
}
func newSrflxRemote(t *testing.T) *CandidateServerReflexive {
srflxConfig := &CandidateServerReflexiveConfig{
Network: "udp",
Address: "10.10.10.2",
Port: 19218,
Component: 1,
RelAddr: "4.3.2.1",
RelPort: 43212,
}
srflxRemote, err := NewCandidateServerReflexive(srflxConfig)
require.NoError(t, err)
return srflxRemote
}
func newRelayRemote(t *testing.T) *CandidateRelay {
relayConfig := &CandidateRelayConfig{
Network: "udp",
Address: "1.2.3.4",
Port: 12340,
Component: 1,
RelAddr: "4.3.2.1",
RelPort: 43210,
}
relayRemote, err := NewCandidateRelay(relayConfig)
require.NoError(t, err)
return relayRemote
}
func newHostLocal(t *testing.T) *CandidateHost {
localHostConfig := &CandidateHostConfig{
Network: "udp",
Address: "192.168.1.1",
Port: 19216,
Component: 1,
}
hostLocal, err := NewCandidateHost(localHostConfig)
require.NoError(t, err)
return hostLocal
}
-95
View File
@@ -1,95 +0,0 @@
//go:build !js
// +build !js
package ice
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestPairPriority(t *testing.T) {
a, err := NewAgent(&AgentConfig{})
if err != nil {
t.Fatalf("Failed to create agent: %s", err)
}
hostConfig := &CandidateHostConfig{
Network: "udp",
Address: "192.168.1.1",
Port: 19216,
Component: 1,
}
hostLocal, err := NewCandidateHost(hostConfig)
if err != nil {
t.Fatalf("Failed to construct local host candidate: %s", err)
}
relayConfig := &CandidateRelayConfig{
Network: "udp",
Address: "1.2.3.4",
Port: 12340,
Component: 1,
RelAddr: "4.3.2.1",
RelPort: 43210,
}
relayRemote, err := NewCandidateRelay(relayConfig)
if err != nil {
t.Fatalf("Failed to construct remote relay candidate: %s", err)
}
srflxConfig := &CandidateServerReflexiveConfig{
Network: "udp",
Address: "10.10.10.2",
Port: 19218,
Component: 1,
RelAddr: "4.3.2.1",
RelPort: 43212,
}
srflxRemote, err := NewCandidateServerReflexive(srflxConfig)
if err != nil {
t.Fatalf("Failed to construct remote srflx candidate: %s", err)
}
prflxConfig := &CandidatePeerReflexiveConfig{
Network: "udp",
Address: "10.10.10.2",
Port: 19217,
Component: 1,
RelAddr: "4.3.2.1",
RelPort: 43211,
}
prflxRemote, err := NewCandidatePeerReflexive(prflxConfig)
if err != nil {
t.Fatalf("Failed to construct remote prflx candidate: %s", err)
}
hostConfig = &CandidateHostConfig{
Network: "udp",
Address: "1.2.3.5",
Port: 12350,
Component: 1,
}
hostRemote, err := NewCandidateHost(hostConfig)
if err != nil {
t.Fatalf("Failed to construct remote host candidate: %s", err)
}
for _, remote := range []Candidate{relayRemote, srflxRemote, prflxRemote, hostRemote} {
p := a.findPair(hostLocal, remote)
if p == nil {
p = a.addPair(hostLocal, remote)
}
p.state = CandidatePairStateSucceeded
bestPair := a.getBestValidCandidatePair()
if bestPair.String() != (&CandidatePair{Remote: remote, Local: hostLocal}).String() {
t.Fatalf("Unexpected bestPair %s (expected remote: %s)", bestPair, remote)
}
}
assert.NoError(t, a.Close())
}