-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
140 lines (117 loc) · 3.15 KB
/
client_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
package turn
import (
"net"
"testing"
"time"
"github.com/pion/logging"
"github.com/stretchr/testify/assert"
)
func createListeningTestClient(t *testing.T, loggerFactory logging.LoggerFactory) (*Client, bool) {
conn, err := net.ListenPacket("udp4", "0.0.0.0:0")
if !assert.NoError(t, err, "should succeed") {
return nil, false
}
c, err := NewClient(&ClientConfig{
Conn: conn,
Software: "TEST SOFTWARE",
LoggerFactory: loggerFactory,
})
if !assert.NoError(t, err, "should succeed") {
return nil, false
}
err = c.Listen()
if !assert.NoError(t, err, "should succeed") {
return nil, false
}
return c, true
}
func createListeningTestClientWithSTUNServ(t *testing.T, loggerFactory logging.LoggerFactory) (*Client, bool) {
conn, err := net.ListenPacket("udp4", "0.0.0.0:0")
if !assert.NoError(t, err, "should succeed") {
return nil, false
}
c, err := NewClient(&ClientConfig{
STUNServerAddr: "stun1.l.google.com:19302",
Conn: conn,
LoggerFactory: loggerFactory,
})
if !assert.NoError(t, err, "should succeed") {
return nil, false
}
err = c.Listen()
if !assert.NoError(t, err, "should succeed") {
return nil, false
}
return c, true
}
func TestClientWithSTUN(t *testing.T) {
loggerFactory := logging.NewDefaultLoggerFactory()
log := loggerFactory.NewLogger("test")
t.Run("SendBindingRequest", func(t *testing.T) {
c, ok := createListeningTestClientWithSTUNServ(t, loggerFactory)
if !ok {
return
}
defer c.Close()
resp, err := c.SendBindingRequest()
assert.NoError(t, err, "should succeed")
log.Debugf("mapped-addr: %s", resp.String())
assert.Equal(t, 0, c.trMap.Size(), "should be no transaction left")
})
t.Run("SendBindingRequestTo Parallel", func(t *testing.T) {
c, ok := createListeningTestClient(t, loggerFactory)
if !ok {
return
}
defer c.Close()
// simple channel fo go routine start signaling
started := make(chan struct{})
finished := make(chan struct{})
var err1 error
var resp1 interface{}
to, err := net.ResolveUDPAddr("udp4", "stun1.l.google.com:19302")
if !assert.NoError(t, err, "should succeed") {
return
}
// stun1.l.google.com:19302, more at https://gist.github.com/zziuni/3741933#file-stuns-L5
go func() {
close(started)
resp1, err1 = c.SendBindingRequestTo(to)
close(finished)
}()
// block until go routine is started to make two almost parallel requests
<-started
resp2, err2 := c.SendBindingRequestTo(to)
if err2 != nil {
t.Fatal(err)
} else {
t.Log(resp2)
}
<-finished
if err1 != nil {
t.Fatal(err)
} else {
t.Log(resp1)
}
})
t.Run("NewClient should fail if Conn is nil", func(t *testing.T) {
_, err := NewClient(&ClientConfig{
LoggerFactory: loggerFactory,
})
assert.Error(t, err, "should fail")
})
t.Run("SendBindingRequestTo timeout", func(t *testing.T) {
c, ok := createListeningTestClient(t, loggerFactory)
if !ok {
return
}
defer c.Close()
to, err := net.ResolveUDPAddr("udp4", "127.0.0.1:9")
if !assert.NoError(t, err, "should succeed") {
return
}
c.rto = 10 * time.Millisecond // force short timeout
_, err = c.SendBindingRequestTo(to)
log.Debug(err.Error())
})
}