-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathcandidatepair_test.go
More file actions
189 lines (168 loc) · 4.54 KB
/
candidatepair_test.go
File metadata and controls
189 lines (168 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package ice
import (
"testing"
"github.com/stretchr/testify/require"
)
func hostCandidate() *CandidateHost {
return &CandidateHost{
candidateBase: candidateBase{
candidateType: CandidateTypeHost,
component: ComponentRTP,
},
}
}
func prflxCandidate() *CandidatePeerReflexive {
return &CandidatePeerReflexive{
candidateBase: candidateBase{
candidateType: CandidateTypePeerReflexive,
component: ComponentRTP,
},
}
}
func srflxCandidate() *CandidateServerReflexive {
return &CandidateServerReflexive{
candidateBase: candidateBase{
candidateType: CandidateTypeServerReflexive,
component: ComponentRTP,
},
}
}
func relayCandidate() *CandidateRelay {
return &CandidateRelay{
candidateBase: candidateBase{
candidateType: CandidateTypeRelay,
component: ComponentRTP,
relayLocalPreference: relayProtocolPreference(udp),
},
}
}
func TestCandidatePairPriority(t *testing.T) {
for _, test := range []struct {
Pair *CandidatePair
WantPriority uint64
}{
{
Pair: newCandidatePair(
hostCandidate(),
hostCandidate(),
false,
),
WantPriority: 9151314440652587007,
},
{
Pair: newCandidatePair(
hostCandidate(),
hostCandidate(),
true,
),
WantPriority: 9151314440652587007,
},
{
Pair: newCandidatePair(
hostCandidate(),
prflxCandidate(),
true,
),
WantPriority: 7998392936314175488,
},
{
Pair: newCandidatePair(
hostCandidate(),
prflxCandidate(),
false,
),
WantPriority: 7998392936314175487,
},
{
Pair: newCandidatePair(
hostCandidate(),
srflxCandidate(),
true,
),
WantPriority: 7277816996102668288,
},
{
Pair: newCandidatePair(
hostCandidate(),
srflxCandidate(),
false,
),
WantPriority: 7277816996102668287,
},
{
Pair: newCandidatePair(
hostCandidate(),
relayCandidate(),
true,
),
WantPriority: 4398012955648,
},
{
Pair: newCandidatePair(
hostCandidate(),
relayCandidate(),
false,
),
WantPriority: 4398012955647,
},
} {
require.Equal(t, test.Pair.priority(), test.WantPriority)
}
}
func TestCandidatePairEquality(t *testing.T) {
pairA := newCandidatePair(hostCandidate(), srflxCandidate(), true)
pairB := newCandidatePair(hostCandidate(), srflxCandidate(), false)
require.True(t, pairA.equal(pairB))
}
func TestNilCandidatePairString(t *testing.T) {
var nilCandidatePair *CandidatePair
require.Equal(t, nilCandidatePair.String(), "")
}
func TestCandidatePairState_String(t *testing.T) {
tests := []struct {
name string
in CandidatePairState
want string
}{
{"waiting", CandidatePairStateWaiting, "waiting"},
{"in-progress", CandidatePairStateInProgress, "in-progress"},
{"failed", CandidatePairStateFailed, "failed"},
{"succeeded", CandidatePairStateSucceeded, "succeeded"},
{"unknown", CandidatePairState(255), "Unknown candidate pair state"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.want, tt.in.String())
})
}
}
func TestCandidatePairEqual_NilCases(t *testing.T) {
// both nil -> true
var a *CandidatePair
var b *CandidatePair
require.True(t, a.equal(b), "both nil pairs should be equal")
// left non-nil, right nil -> false
a = newCandidatePair(hostCandidate(), srflxCandidate(), true)
require.False(t, a.equal(nil), "non-nil vs nil should be false")
// left nil, right non-nil -> false
require.False(t, (*CandidatePair)(nil).equal(a), "nil vs non-nil should be false")
}
func TestCandidatePair_TimeGetters_DefaultZero(t *testing.T) {
p := newCandidatePair(hostCandidate(), srflxCandidate(), true)
require.True(t, p.FirstRequestSentAt().IsZero(), "FirstRequestSentAt should be zero by default")
require.True(t, p.LastRequestSentAt().IsZero(), "LastRequestSentAt should be zero by default")
require.True(t, p.FirstReponseReceivedAt().IsZero(), "FirstReponseReceivedAt should be zero by default")
require.True(t, p.LastResponseReceivedAt().IsZero(), "LastResponseReceivedAt should be zero by default")
require.True(t, p.FirstRequestReceivedAt().IsZero(), "FirstRequestReceivedAt should be zero by default")
require.True(t, p.LastRequestReceivedAt().IsZero(), "LastRequestReceivedAt should be zero by default")
}
func TestCandidatePair_ID(t *testing.T) {
pair := newCandidatePair(hostCandidate(), srflxCandidate(), true)
// Default ID should be 0
require.Equal(t, uint64(0), pair.ID())
// Set ID
pair.id = 42
require.Equal(t, uint64(42), pair.ID())
}