forked from pion/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtcquictransport_test.go
221 lines (176 loc) · 4.02 KB
/
rtcquictransport_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
package webrtc
import (
"testing"
"time"
"github.com/pions/transport/test"
"github.com/pions/webrtc/pkg/quic"
)
func TestRTCQuicTransport_E2E(t *testing.T) {
// Limit runtime in case of deadlocks
lim := test.TimeOut(time.Second * 20)
defer lim.Stop()
// TODO: Check how we can make sure quic-go closes without leaking
// report := test.CheckRoutines(t)
// defer report()
stackA, stackB, err := newQuicPair()
if err != nil {
t.Fatal(err)
}
awaitSetup := make(chan struct{})
stackB.quic.OnBidirectionalStream(func(stream *quic.BidirectionalStream) {
go quicReadLoop(stream) // Read to pull incoming messages
close(awaitSetup)
})
err = signalQuicPair(stackA, stackB)
if err != nil {
t.Fatal(err)
}
stream, err := stackA.quic.CreateBidirectionalStream()
if err != nil {
t.Fatal(err)
}
go quicReadLoop(stream) // Read to pull incoming messages
// Write to open stream
data := quic.StreamWriteParameters{
Data: []byte("Hello"),
}
err = stream.Write(data)
if err != nil {
t.Fatal(err)
}
<-awaitSetup
err = stackA.close()
if err != nil {
t.Fatal(err)
}
err = stackB.close()
if err != nil {
t.Fatal(err)
}
}
func quicReadLoop(s *quic.BidirectionalStream) {
for {
buffer := make([]byte, 15)
_, err := s.ReadInto(buffer)
if err != nil {
return
}
}
}
type testQuicStack struct {
gatherer *RTCIceGatherer
ice *RTCIceTransport
quic *RTCQuicTransport
api *API
}
func (s *testQuicStack) setSignal(sig *testQuicSignal, isOffer bool) error {
iceRole := RTCIceRoleControlled
if isOffer {
iceRole = RTCIceRoleControlling
}
err := s.ice.SetRemoteCandidates(sig.ICECandidates)
if err != nil {
return err
}
// Start the ICE transport
err = s.ice.Start(nil, sig.ICEParameters, &iceRole)
if err != nil {
return err
}
// Start the Quic transport
err = s.quic.Start(sig.QuicParameters)
if err != nil {
return err
}
return nil
}
func (s *testQuicStack) getSignal() (*testQuicSignal, error) {
// Gather candidates
err := s.gatherer.Gather()
if err != nil {
return nil, err
}
iceCandidates, err := s.gatherer.GetLocalCandidates()
if err != nil {
return nil, err
}
iceParams, err := s.gatherer.GetLocalParameters()
if err != nil {
return nil, err
}
quicParams := s.quic.GetLocalParameters()
return &testQuicSignal{
ICECandidates: iceCandidates,
ICEParameters: iceParams,
QuicParameters: quicParams,
}, nil
}
func (s *testQuicStack) close() error {
var closeErrs []error
if err := s.quic.Stop(quic.TransportStopInfo{}); err != nil {
closeErrs = append(closeErrs, err)
}
if err := s.ice.Stop(); err != nil {
closeErrs = append(closeErrs, err)
}
return flattenErrs(closeErrs)
}
type testQuicSignal struct {
ICECandidates []RTCIceCandidate `json:"iceCandidates"`
ICEParameters RTCIceParameters `json:"iceParameters"`
QuicParameters RTCQuicParameters `json:"quicParameters"`
}
func newQuicPair() (stackA *testQuicStack, stackB *testQuicStack, err error) {
sa, err := newQuicStack()
if err != nil {
return nil, nil, err
}
sb, err := newQuicStack()
if err != nil {
return nil, nil, err
}
return sa, sb, nil
}
func newQuicStack() (*testQuicStack, error) {
api := NewAPI()
// Create the ICE gatherer
gatherer, err := api.NewRTCIceGatherer(RTCIceGatherOptions{})
if err != nil {
return nil, err
}
// Construct the ICE transport
ice := api.NewRTCIceTransport(gatherer)
// Construct the Quic transport
qt, err := api.NewRTCQuicTransport(ice, nil)
if err != nil {
return nil, err
}
return &testQuicStack{
api: api,
gatherer: gatherer,
ice: ice,
quic: qt,
}, nil
}
func signalQuicPair(stackA *testQuicStack, stackB *testQuicStack) error {
sigA, err := stackA.getSignal()
if err != nil {
return err
}
sigB, err := stackB.getSignal()
if err != nil {
return err
}
a := make(chan error)
b := make(chan error)
go func() {
a <- stackB.setSignal(sigA, false)
}()
go func() {
b <- stackA.setSignal(sigB, true)
}()
errA := <-a
errB := <-b
closeErrs := []error{errA, errB}
return flattenErrs(closeErrs)
}