1 Star 0 Fork 0

liuzh0324/webrtc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
icegatherer_test.go 2.92 KB
一键复制 编辑 原始数据 按行查看 历史
// +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())
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/natoverload/webrtc.git
git@gitee.com:natoverload/webrtc.git
natoverload
webrtc
webrtc
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385