This repository has been archived by the owner on Mar 16, 2022. It is now read-only.
forked from pion/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
datachannel_test.go
413 lines (339 loc) · 10.8 KB
/
datachannel_test.go
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package webrtc
import (
"io"
"sync"
"testing"
"time"
"github.com/pion/transport/test"
"github.com/stretchr/testify/assert"
)
// expectedLabel represents the label of the data channel we are trying to test.
// Some other channels may have been created during initialization (in the Wasm
// bindings this is a requirement).
const expectedLabel = "data"
func closePairNow(t *testing.T, pc1, pc2 io.Closer) {
var fail bool
if err := pc1.Close(); err != nil {
t.Errorf("Failed to close PeerConnection: %v", err)
fail = true
}
if err := pc2.Close(); err != nil {
t.Errorf("Failed to close PeerConnection: %v", err)
fail = true
}
if fail {
t.FailNow()
}
}
func closePair(t *testing.T, pc1, pc2 io.Closer, done <-chan bool) {
select {
case <-time.After(10 * time.Second):
t.Fatalf("closePair timed out waiting for done signal")
case <-done:
closePairNow(t, pc1, pc2)
}
}
func setUpDataChannelParametersTest(t *testing.T, options *DataChannelInit) (*PeerConnection, *PeerConnection, *DataChannel, chan bool) {
offerPC, answerPC, err := newPair()
if err != nil {
t.Fatalf("Failed to create a PC pair for testing")
}
done := make(chan bool)
dc, err := offerPC.CreateDataChannel(expectedLabel, options)
if err != nil {
t.Fatalf("Failed to create a PC pair for testing")
}
return offerPC, answerPC, dc, done
}
func closeReliabilityParamTest(t *testing.T, pc1, pc2 *PeerConnection, done chan bool) {
err := signalPair(pc1, pc2)
if err != nil {
t.Fatalf("Failed to signal our PC pair for testing")
}
closePair(t, pc1, pc2, done)
}
func TestDataChannel_Open(t *testing.T) {
t.Run("handler should be called once", func(t *testing.T) {
report := test.CheckRoutines(t)
defer report()
offerPC, answerPC, err := newPair()
if err != nil {
t.Fatalf("Failed to create a PC pair for testing")
}
done := make(chan bool)
openCalls := make(chan bool, 2)
answerPC.OnDataChannel(func(d *DataChannel) {
if d.Label() != expectedLabel {
return
}
d.OnOpen(func() {
openCalls <- true
})
d.OnMessage(func(msg DataChannelMessage) {
go func() {
// Wait a little bit to ensure all messages are processed.
time.Sleep(100 * time.Millisecond)
done <- true
}()
})
})
dc, err := offerPC.CreateDataChannel(expectedLabel, nil)
assert.NoError(t, err)
dc.OnOpen(func() {
e := dc.SendText("Ping")
if e != nil {
t.Fatalf("Failed to send string on data channel")
}
})
assert.NoError(t, signalPair(offerPC, answerPC))
closePair(t, offerPC, answerPC, done)
assert.Len(t, openCalls, 1)
})
}
func TestDataChannel_Send(t *testing.T) {
t.Run("before signaling", func(t *testing.T) {
report := test.CheckRoutines(t)
defer report()
offerPC, answerPC, err := newPair()
if err != nil {
t.Fatalf("Failed to create a PC pair for testing")
}
done := make(chan bool)
answerPC.OnDataChannel(func(d *DataChannel) {
// Make sure this is the data channel we were looking for. (Not the one
// created in signalPair).
if d.Label() != expectedLabel {
return
}
d.OnMessage(func(msg DataChannelMessage) {
e := d.Send([]byte("Pong"))
if e != nil {
t.Fatalf("Failed to send string on data channel")
}
})
assert.True(t, d.Ordered(), "Ordered should be set to true")
})
dc, err := offerPC.CreateDataChannel(expectedLabel, nil)
if err != nil {
t.Fatalf("Failed to create a PC pair for testing")
}
assert.True(t, dc.Ordered(), "Ordered should be set to true")
dc.OnOpen(func() {
e := dc.SendText("Ping")
if e != nil {
t.Fatalf("Failed to send string on data channel")
}
})
dc.OnMessage(func(msg DataChannelMessage) {
done <- true
})
err = signalPair(offerPC, answerPC)
if err != nil {
t.Fatalf("Failed to signal our PC pair for testing: %+v", err)
}
closePair(t, offerPC, answerPC, done)
})
t.Run("after connected", func(t *testing.T) {
report := test.CheckRoutines(t)
defer report()
offerPC, answerPC, err := newPair()
if err != nil {
t.Fatalf("Failed to create a PC pair for testing")
}
done := make(chan bool)
answerPC.OnDataChannel(func(d *DataChannel) {
// Make sure this is the data channel we were looking for. (Not the one
// created in signalPair).
if d.Label() != expectedLabel {
return
}
d.OnMessage(func(msg DataChannelMessage) {
e := d.Send([]byte("Pong"))
if e != nil {
t.Fatalf("Failed to send string on data channel")
}
})
assert.True(t, d.Ordered(), "Ordered should be set to true")
})
once := &sync.Once{}
offerPC.OnICEConnectionStateChange(func(state ICEConnectionState) {
if state == ICEConnectionStateConnected || state == ICEConnectionStateCompleted {
// wasm fires completed state multiple times
once.Do(func() {
dc, createErr := offerPC.CreateDataChannel(expectedLabel, nil)
if createErr != nil {
t.Fatalf("Failed to create a PC pair for testing")
}
assert.True(t, dc.Ordered(), "Ordered should be set to true")
dc.OnMessage(func(msg DataChannelMessage) {
done <- true
})
if e := dc.SendText("Ping"); e != nil {
// wasm binding doesn't fire OnOpen (we probably already missed it)
dc.OnOpen(func() {
e = dc.SendText("Ping")
if e != nil {
t.Fatalf("Failed to send string on data channel")
}
})
}
})
}
})
err = signalPair(offerPC, answerPC)
if err != nil {
t.Fatalf("Failed to signal our PC pair for testing")
}
closePair(t, offerPC, answerPC, done)
})
}
func TestDataChannel_Close(t *testing.T) {
report := test.CheckRoutines(t)
defer report()
t.Run("Close after PeerConnection Closed", func(t *testing.T) {
offerPC, answerPC, err := newPair()
assert.NoError(t, err)
dc, err := offerPC.CreateDataChannel(expectedLabel, nil)
assert.NoError(t, err)
assert.NoError(t, offerPC.Close())
assert.NoError(t, answerPC.Close())
assert.NoError(t, dc.Close())
})
t.Run("Close before connected", func(t *testing.T) {
offerPC, answerPC, err := newPair()
assert.NoError(t, err)
dc, err := offerPC.CreateDataChannel(expectedLabel, nil)
assert.NoError(t, err)
assert.NoError(t, dc.Close())
assert.NoError(t, offerPC.Close())
assert.NoError(t, answerPC.Close())
})
}
func TestDataChannelParameters(t *testing.T) {
report := test.CheckRoutines(t)
defer report()
t.Run("MaxPacketLifeTime exchange", func(t *testing.T) {
ordered := true
maxPacketLifeTime := uint16(3)
options := &DataChannelInit{
Ordered: &ordered,
MaxPacketLifeTime: &maxPacketLifeTime,
}
offerPC, answerPC, dc, done := setUpDataChannelParametersTest(t, options)
// Check if parameters are correctly set
assert.Equal(t, dc.Ordered(), ordered, "Ordered should be same value as set in DataChannelInit")
if assert.NotNil(t, dc.MaxPacketLifeTime(), "should not be nil") {
assert.Equal(t, maxPacketLifeTime, *dc.MaxPacketLifeTime(), "should match")
}
answerPC.OnDataChannel(func(d *DataChannel) {
if d.Label() != expectedLabel {
return
}
// Check if parameters are correctly set
assert.Equal(t, d.Ordered(), ordered, "Ordered should be same value as set in DataChannelInit")
if assert.NotNil(t, d.MaxPacketLifeTime(), "should not be nil") {
assert.Equal(t, maxPacketLifeTime, *d.MaxPacketLifeTime(), "should match")
}
done <- true
})
closeReliabilityParamTest(t, offerPC, answerPC, done)
})
t.Run("MaxRetransmits exchange", func(t *testing.T) {
ordered := false
maxRetransmits := uint16(3000)
options := &DataChannelInit{
Ordered: &ordered,
MaxRetransmits: &maxRetransmits,
}
offerPC, answerPC, dc, done := setUpDataChannelParametersTest(t, options)
// Check if parameters are correctly set
assert.False(t, dc.Ordered(), "Ordered should be set to false")
if assert.NotNil(t, dc.MaxRetransmits(), "should not be nil") {
assert.Equal(t, maxRetransmits, *dc.MaxRetransmits(), "should match")
}
answerPC.OnDataChannel(func(d *DataChannel) {
// Make sure this is the data channel we were looking for. (Not the one
// created in signalPair).
if d.Label() != expectedLabel {
return
}
// Check if parameters are correctly set
assert.False(t, d.Ordered(), "Ordered should be set to false")
if assert.NotNil(t, d.MaxRetransmits(), "should not be nil") {
assert.Equal(t, maxRetransmits, *d.MaxRetransmits(), "should match")
}
done <- true
})
closeReliabilityParamTest(t, offerPC, answerPC, done)
})
t.Run("Protocol exchange", func(t *testing.T) {
protocol := "json"
options := &DataChannelInit{
Protocol: &protocol,
}
offerPC, answerPC, dc, done := setUpDataChannelParametersTest(t, options)
// Check if parameters are correctly set
assert.Equal(t, protocol, dc.Protocol(), "Protocol should match DataChannelInit")
answerPC.OnDataChannel(func(d *DataChannel) {
// Make sure this is the data channel we were looking for. (Not the one
// created in signalPair).
if d.Label() != expectedLabel {
return
}
// Check if parameters are correctly set
assert.Equal(t, protocol, d.Protocol(), "Protocol should match what channel creator declared")
done <- true
})
closeReliabilityParamTest(t, offerPC, answerPC, done)
})
t.Run("Negotiated exchange", func(t *testing.T) {
const expectedMessage = "Hello World"
negotiated := true
var id uint16 = 500
options := &DataChannelInit{
Negotiated: &negotiated,
ID: &id,
}
offerPC, answerPC, offerDatachannel, done := setUpDataChannelParametersTest(t, options)
answerDatachannel, err := answerPC.CreateDataChannel(expectedLabel, options)
assert.NoError(t, err)
answerPC.OnDataChannel(func(d *DataChannel) {
// Ignore our default channel, exists to force ICE candidates. See signalPair for more info
if d.Label() == "initial_data_channel" {
return
}
t.Fatal("OnDataChannel must not be fired when negotiated == true")
})
offerPC.OnDataChannel(func(d *DataChannel) {
t.Fatal("OnDataChannel must not be fired when negotiated == true")
})
seenAnswerMessage := &atomicBool{}
seenOfferMessage := &atomicBool{}
answerDatachannel.OnMessage(func(msg DataChannelMessage) {
if msg.IsString && string(msg.Data) == expectedMessage {
seenAnswerMessage.set(true)
}
})
offerDatachannel.OnMessage(func(msg DataChannelMessage) {
if msg.IsString && string(msg.Data) == expectedMessage {
seenOfferMessage.set(true)
}
})
go func() {
for {
if seenAnswerMessage.get() && seenOfferMessage.get() {
break
}
if offerDatachannel.ReadyState() == DataChannelStateOpen {
assert.NoError(t, offerDatachannel.SendText(expectedMessage))
}
if answerDatachannel.ReadyState() == DataChannelStateOpen {
assert.NoError(t, answerDatachannel.SendText(expectedMessage))
}
time.Sleep(500 * time.Millisecond)
}
done <- true
}()
closeReliabilityParamTest(t, offerPC, answerPC, done)
})
}