-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathagent_handlers_test.go
More file actions
191 lines (166 loc) · 4.66 KB
/
agent_handlers_test.go
File metadata and controls
191 lines (166 loc) · 4.66 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
190
191
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package ice
import (
"testing"
"time"
"github.com/pion/transport/v4/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConnectionStateNotifier(t *testing.T) {
t.Run("TestManyUpdates", func(t *testing.T) {
defer test.CheckRoutines(t)()
updates := make(chan struct{}, 1)
notifier := &handlerNotifier{
connectionStateFunc: func(_ ConnectionState) {
updates <- struct{}{}
},
done: make(chan struct{}),
}
// Enqueue all updates upfront to ensure that it
// doesn't block
for range 10000 {
notifier.EnqueueConnectionState(ConnectionStateNew)
}
done := make(chan struct{})
go func() {
for range 10000 {
<-updates
}
select {
case <-updates:
t.Errorf("received more updates than expected") // nolint
case <-time.After(1 * time.Second):
}
close(done)
}()
<-done
notifier.Close(true)
})
t.Run("TestUpdateOrdering", func(t *testing.T) {
defer test.CheckRoutines(t)()
updates := make(chan ConnectionState)
notifer := &handlerNotifier{
connectionStateFunc: func(cs ConnectionState) {
updates <- cs
},
done: make(chan struct{}),
}
done := make(chan struct{})
go func() {
for i := range 10000 {
assert.Equal(t, ConnectionState(i), <-updates)
}
select {
case <-updates:
t.Errorf("received more updates than expected") // nolint
case <-time.After(1 * time.Second):
}
close(done)
}()
for i := range 10000 {
notifer.EnqueueConnectionState(ConnectionState(i))
}
<-done
notifer.Close(true)
})
}
func TestHandlerNotifier_Close_AlreadyClosed(t *testing.T) {
defer test.CheckRoutines(t)()
notifier := &handlerNotifier{
connectionStateFunc: func(ConnectionState) {},
candidateFunc: func(Candidate) {},
candidatePairFunc: func(*CandidatePair) {},
done: make(chan struct{}),
}
// first close
notifier.Close(false)
isClosed := func(ch <-chan struct{}) bool {
select {
case <-ch:
return true
default:
return false
}
}
assert.True(t, isClosed(notifier.done), "expected h.done to be closed after first Close")
// second close should hit `case <-h.done` and return immediately
// without blocking on the WaitGroup.
finished := make(chan struct{}, 1)
go func() {
notifier.Close(true)
close(finished)
}()
assert.Eventually(t, func() bool {
select {
case <-finished:
return true
default:
return false
}
}, 250*time.Millisecond, 10*time.Millisecond, "second Close(true) did not return promptly")
// ensure still closed afterwards
assert.True(t, isClosed(notifier.done), "expected h.done to remain closed after second Close")
// sanity: no enqueues should start after close.
require.False(t, notifier.runningConnectionStates)
require.False(t, notifier.runningCandidates)
require.False(t, notifier.runningCandidatePairs)
require.Zero(t, len(notifier.connectionStates))
require.Zero(t, len(notifier.candidates))
require.Zero(t, len(notifier.selectedCandidatePairs))
}
func TestHandlerNotifier_EnqueueConnectionState_AfterClose(t *testing.T) {
defer test.CheckRoutines(t)()
connCh := make(chan struct{}, 1)
notifier := &handlerNotifier{
connectionStateFunc: func(ConnectionState) { connCh <- struct{}{} },
done: make(chan struct{}),
}
notifier.Close(false)
notifier.EnqueueConnectionState(ConnectionStateConnected)
assert.Never(t, func() bool {
select {
case <-connCh:
return true
default:
return false
}
}, 250*time.Millisecond, 10*time.Millisecond, "connectionStateFunc should not be called after close")
}
func TestHandlerNotifier_EnqueueCandidate_AfterClose(t *testing.T) {
defer test.CheckRoutines(t)()
candidateCh := make(chan struct{}, 1)
h := &handlerNotifier{
candidateFunc: func(Candidate) { candidateCh <- struct{}{} },
done: make(chan struct{}),
}
h.Close(false)
h.EnqueueCandidate(nil)
assert.Never(t, func() bool {
select {
case <-candidateCh:
return true
default:
return false
}
}, 250*time.Millisecond, 10*time.Millisecond, "candidateFunc should not be called after close")
}
func TestHandlerNotifier_EnqueueSelectedCandidatePair_AfterClose(t *testing.T) {
defer test.CheckRoutines(t)()
pairCh := make(chan struct{}, 1)
h := &handlerNotifier{
candidatePairFunc: func(*CandidatePair) { pairCh <- struct{}{} },
done: make(chan struct{}),
}
h.Close(false)
h.EnqueueSelectedCandidatePair(nil)
assert.Never(t, func() bool {
select {
case <-pairCh:
return true
default:
return false
}
}, 250*time.Millisecond, 10*time.Millisecond, "candidatePairFunc should not be called after close")
}