forked from pion/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peerconnection_setlocaldescription_test.go
107 lines (89 loc) · 2.42 KB
/
peerconnection_setlocaldescription_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
// +build !js
package webrtc
import (
"errors"
"math/rand"
"strings"
"testing"
"time"
"github.com/hcm007/webrtc/v2/pkg/media"
)
func check(err error) {
if err != nil {
panic(err)
}
}
func runOfferingPeer(offerChan chan<- SessionDescription, answerChan <-chan SessionDescription) {
config := Configuration{}
peerConnection, err := NewPeerConnection(config)
check(err)
track, err := peerConnection.NewTrack(DefaultPayloadTypeOpus, rand.Uint32(), "audio", "pion1")
check(err)
_, err = peerConnection.AddTrack(track)
check(err)
offer, err := peerConnection.CreateOffer(nil)
check(err)
err = peerConnection.SetLocalDescription(offer)
check(err)
offerChan <- offer
answer := <-answerChan
err = peerConnection.SetRemoteDescription(answer)
check(err)
for {
// send bogus data
sample := media.Sample{
Data: []byte{0x00},
Samples: 1,
}
err = track.WriteSample(sample)
check(err)
}
}
func runAnsweringPeer(offerChan <-chan SessionDescription, answerChan chan<- SessionDescription, resultChan chan<- error) {
s := SettingEngine{
LoggerFactory: testCatchAllLoggerFactory{
callback: func(msg string) {
if strings.Contains(msg, "SetLocalDescription not called") {
resultChan <- nil
}
},
},
}
api := NewAPI(WithSettingEngine(s))
api.mediaEngine.RegisterDefaultCodecs()
peerConnection, err := api.NewPeerConnection(Configuration{})
check(err)
_, err = peerConnection.AddTransceiver(RTPCodecTypeAudio)
check(err)
peerConnection.OnTrack(func(track *Track, receiver *RTPReceiver) {
buf := make([]byte, 1400)
_, err = track.Read(buf)
check(err)
resultChan <- errors.New("Data erroneously received")
})
offer := <-offerChan
err = peerConnection.SetRemoteDescription(offer)
check(err)
answer, err := peerConnection.CreateAnswer(nil)
check(err)
answerChan <- answer
}
func TestNoPanicIfSetLocalDescriptionNotCalledByAnsweringPeer(t *testing.T) {
offerChan := make(chan SessionDescription)
answerChan := make(chan SessionDescription)
resultChan := make(chan error)
go runOfferingPeer(offerChan, answerChan)
go runAnsweringPeer(offerChan, answerChan, resultChan)
// wait for either:
// - the expected logging (success!)
// - the read to succeed (which is actually a bad thing)
// - or a timeout (also bad)
select {
case err := <-resultChan:
if err != nil {
t.Fatal(err.Error())
}
case <-time.After(140 * time.Second):
t.Fatalf("Timed out waiting for expected logging")
}
}