forked from pion/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icegatherer_test.go
146 lines (116 loc) · 2.92 KB
/
icegatherer_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
// +build !js
package webrtc
import (
"context"
"strings"
"testing"
"time"
"github.com/pion/transport/test"
"github.com/stretchr/testify/assert"
)
func TestNewICEGatherer_Success(t *testing.T) {
// Limit runtime in case of deadlocks
lim := test.TimeOut(time.Second * 20)
defer lim.Stop()
report := test.CheckRoutines(t)
defer report()
opts := ICEGatherOptions{
ICEServers: []ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}},
}
gatherer, err := NewAPI().NewICEGatherer(opts)
if err != nil {
t.Error(err)
}
if gatherer.State() != ICEGathererStateNew {
t.Fatalf("Expected gathering state new")
}
err = gatherer.Gather()
if err != nil {
t.Error(err)
}
params, err := gatherer.GetLocalParameters()
if err != nil {
t.Error(err)
}
if len(params.UsernameFragment) == 0 ||
len(params.Password) == 0 {
t.Fatalf("Empty local username or password frag")
}
candidates, err := gatherer.GetLocalCandidates()
if err != nil {
t.Error(err)
}
if len(candidates) == 0 {
t.Fatalf("No candidates gathered")
}
assert.NoError(t, gatherer.Close())
}
func TestICEGather_LocalCandidateOrder(t *testing.T) {
// Limit runtime in case of deadlocks
lim := test.TimeOut(time.Second * 20)
defer lim.Stop()
report := test.CheckRoutines(t)
defer report()
opts := ICEGatherOptions{
ICEServers: []ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}},
}
gatherer, err := NewAPI().NewICEGatherer(opts)
if err != nil {
t.Error(err)
}
if gatherer.State() != ICEGathererStateNew {
t.Fatalf("Expected gathering state new")
}
for i := 0; i < 10; i++ {
candidate := make(chan *ICECandidate)
gatherer.OnLocalCandidate(func(c *ICECandidate) {
candidate <- c
})
if err := gatherer.SignalCandidates(); err != nil {
t.Error(err)
}
endGathering := false
L:
for {
select {
case c := <-candidate:
if c == nil {
endGathering = true
} else if endGathering {
t.Error("Received a candidate after the last candidate")
break L
}
case <-time.After(100 * time.Millisecond):
if !endGathering {
t.Error("Timed out before receiving the last candidate")
}
break L
}
}
}
assert.NoError(t, gatherer.Close())
}
func TestICEGather_mDNSCandidateGathering(t *testing.T) {
// Limit runtime in case of deadlocks
lim := test.TimeOut(time.Second * 20)
defer lim.Stop()
report := test.CheckRoutines(t)
defer report()
s := SettingEngine{}
s.GenerateMulticastDNSCandidates(true)
gatherer, err := NewAPI(WithSettingEngine(s)).NewICEGatherer(ICEGatherOptions{})
if err != nil {
t.Error(err)
}
gotMulticastDNSCandidate, resolveFunc := context.WithCancel(context.Background())
gatherer.OnLocalCandidate(func(c *ICECandidate) {
if c != nil && strings.HasSuffix(c.Address, ".local") {
resolveFunc()
}
})
if err := gatherer.SignalCandidates(); err != nil {
t.Error(err)
}
<-gotMulticastDNSCandidate.Done()
assert.NoError(t, gatherer.Close())
}