forked from Haivision/srtgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
srtgo_test.go
263 lines (220 loc) · 5.3 KB
/
srtgo_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package srtgo
import (
"math/rand"
"sync"
"testing"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func randomPort() uint16 {
return uint16(rand.Intn(32768-1024) + 1024)
}
func TestNewSocket(t *testing.T) {
options := make(map[string]string)
a := NewSrtSocket("localhost", 8090, options)
if a == nil {
t.Error("Could not create a srt socket")
}
}
func TestNewSocketBlocking(t *testing.T) {
options := make(map[string]string)
options["blocking"] = "true"
a := NewSrtSocket("localhost", 8090, options)
if a == nil {
t.Error("Could not create a srt socket")
}
}
func TestNewSocketLinger(t *testing.T) {
options := make(map[string]string)
options["linger"] = "1000"
a := NewSrtSocket("localhost", 8090, options)
if a == nil {
t.Error("Could not create a srt socket with linger")
}
// read back value to make sure
res, err := getSocketLingerOption(a)
if err != nil {
t.Error(err)
}
if res != 1000 {
t.Error("Failed to set linger option")
}
}
func TestNewSocketWithTransType(t *testing.T) {
options := make(map[string]string)
options["transtype"] = "3"
a := NewSrtSocket("localhost", 8090, options)
if a == nil {
t.Error("Could not create a srt socket")
}
}
func TestNewSocketWithParameters(t *testing.T) {
options := make(map[string]string)
options["pbkeylen"] = "32"
a := NewSrtSocket("localhost", 8090, options)
if a == nil {
t.Error("Could not create a srt socket")
}
}
func TestNewSocketWithInt64Param(t *testing.T) {
options := make(map[string]string)
options["maxbw"] = "300000"
a := NewSrtSocket("localhost", 8090, options)
if a == nil {
t.Error("Could not create a srt socket")
}
}
func TestNewSocketWithBoolParam(t *testing.T) {
options := make(map[string]string)
options["enforcedencryption"] = "0"
a := NewSrtSocket("localhost", 8090, options)
if a == nil {
t.Error("Could not create a srt socket")
}
}
func TestNewSocketWithStringParam(t *testing.T) {
options := make(map[string]string)
options["passphrase"] = "11111111111"
a := NewSrtSocket("localhost", 8090, options)
if a == nil {
t.Error("Could not create a srt socket")
}
}
func TestListen(t *testing.T) {
InitSRT()
options := make(map[string]string)
options["blocking"] = "0"
options["transtype"] = "file"
a := NewSrtSocket("0.0.0.0", 8090, options)
err := a.Listen(2)
if err != nil {
t.Error("Error on testListen")
}
}
func AcceptHelper(numSockets int, port uint16, options map[string]string, t *testing.T) {
listening := make(chan struct{})
listener := NewSrtSocket("localhost", port, options)
var connectors []*SrtSocket
for i := 0; i < numSockets; i++ {
connectors = append(connectors, NewSrtSocket("localhost", port, options))
}
wg := sync.WaitGroup{}
timer := time.AfterFunc(time.Second, func() {
t.Log("Accept timed out")
listener.Close()
for _, s := range connectors {
s.Close()
}
})
wg.Add(1)
go func() {
defer wg.Done()
<-listening
for _, s := range connectors {
err := s.Connect()
if err != nil {
t.Error(err)
}
}
}()
err := listener.Listen(numSockets)
if err != nil {
t.Error(err)
}
listening <- struct{}{}
for i := 0; i < numSockets; i++ {
sock, addr, err := listener.Accept()
if err != nil {
t.Error(err)
}
if sock == nil || addr == nil {
t.Error("Expected non-nil addr and sock")
}
}
wg.Wait()
if timer.Stop() {
listener.Close()
for _, s := range connectors {
s.Close()
}
}
}
func TestAcceptNonBlocking(t *testing.T) {
InitSRT()
options := make(map[string]string)
options["transtype"] = "file"
AcceptHelper(1, 8091, options, t)
}
func TestAcceptBlocking(t *testing.T) {
InitSRT()
options := make(map[string]string)
options["blocking"] = "1"
options["transtype"] = "file"
AcceptHelper(1, 8092, options, t)
}
func TestMultipleAcceptNonBlocking(t *testing.T) {
InitSRT()
options := make(map[string]string)
options["transtype"] = "file"
AcceptHelper(3, 8093, options, t)
}
func TestMultipleAcceptBlocking(t *testing.T) {
InitSRT()
options := make(map[string]string)
options["blocking"] = "1"
options["transtype"] = "file"
AcceptHelper(3, 8094, options, t)
}
func TestSetSockOptInt(t *testing.T) {
InitSRT()
options := make(map[string]string)
a := NewSrtSocket("localhost", 8090, options)
expected := 200
err := a.SetSockOptInt(SRTO_LATENCY, expected)
if err != nil {
t.Error(err)
}
v, err := a.GetSockOptInt(SRTO_LATENCY)
if err != nil {
t.Error(err)
}
if v != expected {
t.Errorf("Failed to set SRTO_LATENCY expected %d, got %d\n", expected, v)
}
}
func TestSetSockOptString(t *testing.T) {
InitSRT()
options := make(map[string]string)
a := NewSrtSocket("localhost", 8090, options)
expected := "123"
err := a.SetSockOptString(SRTO_STREAMID, expected)
if err != nil {
t.Error(err)
}
v, err := a.GetSockOptString(SRTO_STREAMID)
if err != nil {
t.Error(err)
}
if v != expected {
t.Errorf("Failed to set SRTO_STREAMID expected %s, got %s\n", expected, v)
}
}
func TestSetSockOptBool(t *testing.T) {
InitSRT()
options := make(map[string]string)
a := NewSrtSocket("localhost", 8090, options)
expected := true
err := a.SetSockOptBool(SRTO_MESSAGEAPI, expected)
if err != nil {
t.Error(err)
}
v, err := a.GetSockOptBool(SRTO_MESSAGEAPI)
if err != nil {
t.Error(err)
}
if v != expected {
t.Errorf("Failed to set SRTO_MESSAGEAPI expected %t, got %t\n", expected, v)
}
}