mirror of
https://github.com/pion/webrtc.git
synced 2025-10-05 15:16:52 +08:00
Adding OnDial handler for datachannels
Adds an `OnDial` handler to be fired once the dialing side has sent the DCEP OPEN message over the data channel.
This commit is contained in:
@@ -33,12 +33,17 @@ func TestDataChannel_EventHandlers(t *testing.T) {
|
||||
api := NewAPI()
|
||||
dc := &DataChannel{api: api}
|
||||
|
||||
onDialCalled := make(chan struct{})
|
||||
onOpenCalled := make(chan struct{})
|
||||
onMessageCalled := make(chan struct{})
|
||||
|
||||
// Verify that the noop case works
|
||||
assert.NotPanics(t, func() { dc.onOpen() })
|
||||
|
||||
dc.OnDial(func() {
|
||||
close(onDialCalled)
|
||||
})
|
||||
|
||||
dc.OnOpen(func() {
|
||||
close(onOpenCalled)
|
||||
})
|
||||
@@ -48,10 +53,12 @@ func TestDataChannel_EventHandlers(t *testing.T) {
|
||||
})
|
||||
|
||||
// Verify that the set handlers are called
|
||||
assert.NotPanics(t, func() { dc.onDial() })
|
||||
assert.NotPanics(t, func() { dc.onOpen() })
|
||||
assert.NotPanics(t, func() { dc.onMessage(DataChannelMessage{Data: []byte("o hai")}) })
|
||||
|
||||
// Wait for all handlers to be called
|
||||
<-onDialCalled
|
||||
<-onOpenCalled
|
||||
<-onMessageCalled
|
||||
}
|
||||
@@ -578,3 +585,74 @@ func TestDataChannel_NonStandardSessionDescription(t *testing.T) {
|
||||
<-onDataChannelCalled
|
||||
closePairNow(t, offerPC, answerPC)
|
||||
}
|
||||
|
||||
func TestDataChannel_Dial(t *testing.T) {
|
||||
t.Run("handler should be called once, by dialing peer only", func(t *testing.T) {
|
||||
report := test.CheckRoutines(t)
|
||||
defer report()
|
||||
|
||||
dialCalls := make(chan bool, 2)
|
||||
wg := new(sync.WaitGroup)
|
||||
wg.Add(2)
|
||||
|
||||
offerPC, answerPC, err := newPair()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create a PC pair for testing")
|
||||
}
|
||||
|
||||
answerPC.OnDataChannel(func(d *DataChannel) {
|
||||
if d.Label() != expectedLabel {
|
||||
return
|
||||
}
|
||||
|
||||
d.OnDial(func() {
|
||||
// only dialing side should fire OnDial
|
||||
t.Fatalf("answering side should not call on dial")
|
||||
})
|
||||
|
||||
d.OnOpen(wg.Done)
|
||||
})
|
||||
|
||||
d, err := offerPC.CreateDataChannel(expectedLabel, nil)
|
||||
assert.NoError(t, err)
|
||||
d.OnDial(func() {
|
||||
dialCalls <- true
|
||||
wg.Done()
|
||||
})
|
||||
|
||||
assert.NoError(t, signalPair(offerPC, answerPC))
|
||||
|
||||
wg.Wait()
|
||||
closePairNow(t, offerPC, answerPC)
|
||||
|
||||
assert.Len(t, dialCalls, 1)
|
||||
})
|
||||
|
||||
t.Run("handler should be called immediately if already dialed", func(t *testing.T) {
|
||||
report := test.CheckRoutines(t)
|
||||
defer report()
|
||||
|
||||
done := make(chan bool)
|
||||
|
||||
offerPC, answerPC, err := newPair()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create a PC pair for testing")
|
||||
}
|
||||
|
||||
d, err := offerPC.CreateDataChannel(expectedLabel, nil)
|
||||
assert.NoError(t, err)
|
||||
d.OnOpen(func() {
|
||||
// when the offer DC has been opened, its guaranteed to have dialed since it has
|
||||
// received a response to said dial. this test represents an unrealistic usage,
|
||||
// but its the best way to guarantee we "missed" the dial event and still invoke
|
||||
// the handler.
|
||||
d.OnDial(func() {
|
||||
done <- true
|
||||
})
|
||||
})
|
||||
|
||||
assert.NoError(t, signalPair(offerPC, answerPC))
|
||||
|
||||
closePair(t, offerPC, answerPC, done)
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user