forked from pion/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icetransport_test.go
135 lines (102 loc) · 3.66 KB
/
icetransport_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
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
//go:build !js
// +build !js
package webrtc
import (
"sync"
"sync/atomic"
"testing"
"time"
"github.com/pion/transport/v3/test"
"github.com/stretchr/testify/assert"
)
func TestICETransport_OnConnectionStateChange(t *testing.T) {
report := test.CheckRoutines(t)
defer report()
lim := test.TimeOut(time.Second * 30)
defer lim.Stop()
pcOffer, pcAnswer, err := newPair()
assert.NoError(t, err)
var (
iceComplete sync.WaitGroup
peerConnectionConnected sync.WaitGroup
)
iceComplete.Add(2)
peerConnectionConnected.Add(2)
onIceComplete := func(s ICETransportState) {
if s == ICETransportStateConnected {
iceComplete.Done()
}
}
pcOffer.SCTP().Transport().ICETransport().OnConnectionStateChange(onIceComplete)
pcAnswer.SCTP().Transport().ICETransport().OnConnectionStateChange(onIceComplete)
onConnected := func(s PeerConnectionState) {
if s == PeerConnectionStateConnected {
peerConnectionConnected.Done()
}
}
pcOffer.OnConnectionStateChange(onConnected)
pcAnswer.OnConnectionStateChange(onConnected)
assert.NoError(t, signalPair(pcOffer, pcAnswer))
iceComplete.Wait()
peerConnectionConnected.Wait()
closePairNow(t, pcOffer, pcAnswer)
}
func TestICETransport_OnSelectedCandidatePairChange(t *testing.T) {
report := test.CheckRoutines(t)
defer report()
lim := test.TimeOut(time.Second * 30)
defer lim.Stop()
pcOffer, pcAnswer, err := newPair()
assert.NoError(t, err)
iceComplete := make(chan bool)
pcAnswer.OnICEConnectionStateChange(func(iceState ICEConnectionState) {
if iceState == ICEConnectionStateConnected {
time.Sleep(3 * time.Second)
close(iceComplete)
}
})
senderCalledCandidateChange := int32(0)
pcOffer.SCTP().Transport().ICETransport().OnSelectedCandidatePairChange(func(*ICECandidatePair) {
atomic.StoreInt32(&senderCalledCandidateChange, 1)
})
assert.NoError(t, signalPair(pcOffer, pcAnswer))
<-iceComplete
if atomic.LoadInt32(&senderCalledCandidateChange) == 0 {
t.Fatalf("Sender ICETransport OnSelectedCandidateChange was never called")
}
closePairNow(t, pcOffer, pcAnswer)
}
func TestICETransport_GetSelectedCandidatePair(t *testing.T) {
offerer, answerer, err := newPair()
assert.NoError(t, err)
peerConnectionConnected := untilConnectionState(PeerConnectionStateConnected, offerer, answerer)
offererSelectedPair, err := offerer.SCTP().Transport().ICETransport().GetSelectedCandidatePair()
assert.NoError(t, err)
assert.Nil(t, offererSelectedPair)
answererSelectedPair, err := answerer.SCTP().Transport().ICETransport().GetSelectedCandidatePair()
assert.NoError(t, err)
assert.Nil(t, answererSelectedPair)
assert.NoError(t, signalPair(offerer, answerer))
peerConnectionConnected.Wait()
offererSelectedPair, err = offerer.SCTP().Transport().ICETransport().GetSelectedCandidatePair()
assert.NoError(t, err)
assert.NotNil(t, offererSelectedPair)
answererSelectedPair, err = answerer.SCTP().Transport().ICETransport().GetSelectedCandidatePair()
assert.NoError(t, err)
assert.NotNil(t, answererSelectedPair)
closePairNow(t, offerer, answerer)
}
func TestICETransport_GetLocalParameters(t *testing.T) {
offerer, answerer, err := newPair()
assert.NoError(t, err)
peerConnectionConnected := untilConnectionState(PeerConnectionStateConnected, offerer, answerer)
assert.NoError(t, signalPair(offerer, answerer))
peerConnectionConnected.Wait()
localParameters, err := offerer.SCTP().Transport().ICETransport().GetLocalParameters()
assert.NoError(t, err)
assert.NotEqual(t, localParameters.UsernameFragment, "")
assert.NotEqual(t, localParameters.Password, "")
closePairNow(t, offerer, answerer)
}