-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent_udpmux_test.go
85 lines (67 loc) · 1.76 KB
/
agent_udpmux_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
//go:build !js
// +build !js
package ice
import (
"net"
"testing"
"time"
"github.com/pion/logging"
"github.com/pion/transport/test"
"github.com/stretchr/testify/require"
)
// TestMuxAgent is an end to end test over UDP mux, ensuring two agents could connect over mux
func TestMuxAgent(t *testing.T) {
report := test.CheckRoutines(t)
defer report()
lim := test.TimeOut(time.Second * 30)
defer lim.Stop()
const muxPort = 7686
c, err := net.ListenUDP(udp, &net.UDPAddr{
Port: muxPort,
})
loggerFactory := logging.NewDefaultLoggerFactory()
udpMux := NewUDPMuxDefault(UDPMuxParams{
Logger: loggerFactory.NewLogger("ice"),
UDPConn: c,
})
require.NoError(t, err)
defer func() {
_ = udpMux.Close()
_ = c.Close()
}()
muxedA, err := NewAgent(&AgentConfig{
UDPMux: udpMux,
CandidateTypes: []CandidateType{CandidateTypeHost},
NetworkTypes: []NetworkType{
NetworkTypeUDP4,
},
})
require.NoError(t, err)
a, err := NewAgent(&AgentConfig{
CandidateTypes: []CandidateType{CandidateTypeHost},
NetworkTypes: supportedNetworkTypes(),
})
require.NoError(t, err)
conn, muxedConn := connect(a, muxedA)
pair := muxedA.getSelectedPair()
require.NotNil(t, pair)
require.Equal(t, muxPort, pair.Local.Port())
// send a packet to Mux
data := []byte("hello world")
_, err = conn.Write(data)
require.NoError(t, err)
buffer := make([]byte, 1024)
n, err := muxedConn.Read(buffer)
require.NoError(t, err)
require.Equal(t, data, buffer[:n])
// send a packet from Mux
_, err = muxedConn.Write(data)
require.NoError(t, err)
n, err = conn.Read(buffer)
require.NoError(t, err)
require.Equal(t, data, buffer[:n])
// close it down
require.NoError(t, conn.Close())
require.NoError(t, muxedConn.Close())
require.NoError(t, udpMux.Close())
}