1
1
package srtgo
2
2
3
3
import (
4
+ "sync"
4
5
"testing"
6
+ "time"
5
7
)
6
8
7
9
func TestNewSocket (t * testing.T ) {
@@ -97,19 +99,108 @@ func TestListen(t *testing.T) {
97
99
}
98
100
}
99
101
102
+ func AcceptHelper (numSockets int , options map [string ]string , t * testing.T ) {
103
+ listening := make (chan struct {})
104
+ listener := NewSrtSocket ("localhost" , 8090 , options )
105
+ var connectors []* SrtSocket
106
+ for i := 0 ; i < numSockets ; i ++ {
107
+ connectors = append (connectors , NewSrtSocket ("localhost" , 8090 , options ))
108
+ }
109
+ wg := sync.WaitGroup {}
110
+ timer := time .AfterFunc (time .Second , func () {
111
+ t .Log ("Accept timed out" )
112
+ listener .Close ()
113
+ for _ , s := range connectors {
114
+ s .Close ()
115
+ }
116
+ })
117
+ wg .Add (1 )
118
+ go func () {
119
+ defer wg .Done ()
120
+ <- listening
121
+ for _ , s := range connectors {
122
+ err := s .Connect ()
123
+ if err != nil {
124
+ t .Error (err )
125
+ }
126
+ }
127
+ }()
128
+
129
+ err := listener .Listen (numSockets )
130
+ if err != nil {
131
+ t .Error (err )
132
+ }
133
+ listening <- struct {}{}
134
+ for i := 0 ; i < numSockets ; i ++ {
135
+ sock , addr , err := listener .Accept ()
136
+ if err != nil {
137
+ t .Error (err )
138
+ }
139
+ if sock == nil || addr == nil {
140
+ t .Error ("Expected non-nil addr and sock" )
141
+ }
142
+ }
143
+
144
+ wg .Wait ()
145
+ if timer .Stop () {
146
+ listener .Close ()
147
+ for _ , s := range connectors {
148
+ s .Close ()
149
+ }
150
+ }
151
+ }
152
+
153
+ func TestAcceptNonBlocking (t * testing.T ) {
154
+ InitSRT ()
155
+
156
+ options := make (map [string ]string )
157
+ options ["transtype" ] = "file"
158
+ AcceptHelper (1 , options , t )
159
+ }
160
+
161
+ func TestAcceptBlocking (t * testing.T ) {
162
+ InitSRT ()
163
+
164
+ options := make (map [string ]string )
165
+ options ["blocking" ] = "1"
166
+ options ["transtype" ] = "file"
167
+ AcceptHelper (1 , options , t )
168
+ }
169
+
170
+ func TestMultipleAcceptNonBlocking (t * testing.T ) {
171
+ InitSRT ()
172
+
173
+ options := make (map [string ]string )
174
+ options ["transtype" ] = "file"
175
+ AcceptHelper (3 , options , t )
176
+ }
177
+
178
+ func TestMultipleAcceptBlocking (t * testing.T ) {
179
+ InitSRT ()
180
+
181
+ options := make (map [string ]string )
182
+ options ["blocking" ] = "1"
183
+ options ["transtype" ] = "file"
184
+ AcceptHelper (3 , options , t )
185
+ }
186
+
100
187
func TestSetSockOptInt (t * testing.T ) {
101
188
InitSRT ()
102
189
options := make (map [string ]string )
103
190
a := NewSrtSocket ("localhost" , 8090 , options )
104
191
105
- err := a .SetSockOptInt (SRTO_LATENCY , 200 )
192
+ expected := 200
193
+ err := a .SetSockOptInt (SRTO_LATENCY , expected )
106
194
if err != nil {
107
- t .Error ("Error on TestSetSockOpt" )
195
+ t .Error (err )
108
196
}
109
197
110
198
v , err := a .GetSockOptInt (SRTO_LATENCY )
111
- if v != 200 {
112
- t .Error ("Error in SetSockOptInt/GetSockOptInt" , v )
199
+ if err != nil {
200
+ t .Error (err )
201
+ }
202
+ if v != expected {
203
+ t .Errorf ("Failed to set SRTO_LATENCY expected %d, got %d\n " , expected , v )
113
204
}
114
205
}
115
206
@@ -118,14 +209,18 @@ func TestSetSockOptString(t *testing.T) {
118
209
options := make (map [string ]string )
119
210
a := NewSrtSocket ("localhost" , 8090 , options )
120
211
121
- err := a .SetSockOptString (SRTO_STREAMID , "123" )
212
+ expected := "123"
213
+ err := a .SetSockOptString (SRTO_STREAMID , expected )
122
214
if err != nil {
123
- t .Error ("Error on TestSetSockOpt" )
215
+ t .Error (err )
124
216
}
125
217
126
218
v , err := a .GetSockOptString (SRTO_STREAMID )
127
- if v != "123" {
128
- t .Error ("Error in SetSockOptString/GetSockOptString" , v )
219
+ if err != nil {
220
+ t .Error (err )
221
+ }
222
+ if v != expected {
223
+ t .Errorf ("Failed to set SRTO_STREAMID expected %s, got %s\n " , expected , v )
129
224
}
130
225
}
131
226
@@ -134,13 +229,17 @@ func TestSetSockOptBool(t *testing.T) {
134
229
options := make (map [string ]string )
135
230
a := NewSrtSocket ("localhost" , 8090 , options )
136
231
137
- err := a .SetSockOptBool (SRTO_MESSAGEAPI , true )
232
+ expected := true
233
+ err := a .SetSockOptBool (SRTO_MESSAGEAPI , expected )
138
234
if err != nil {
139
- t .Error ("Error on TestSetSockOpt" )
235
+ t .Error (err )
140
236
}
141
237
142
238
v , err := a .GetSockOptBool (SRTO_MESSAGEAPI )
143
- if v != true {
144
- t .Error ("Error in SetSockOptBool/GetSockOptBool" , v )
239
+ if err != nil {
240
+ t .Error (err )
241
+ }
242
+ if v != expected {
243
+ t .Errorf ("Failed to set SRTO_MESSAGEAPI expected %t, got %t\n " , expected , v )
145
244
}
146
245
}
0 commit comments