Skip to content

Commit a2f2704

Browse files
authored
Fix/unit tests (#26)
* use constants in netutils_test * return meaningful errors from epoll fails * add connect/accept test, improve socket option test output * add test for multiple consecutive accepts (#8)
1 parent 06a3bcd commit a2f2704

File tree

3 files changed

+118
-17
lines changed

3 files changed

+118
-17
lines changed

netutils_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package srtgo
22

33
import (
44
"testing"
5+
6+
"golang.org/x/sys/unix"
57
)
68

79
func TestCreateAddrInetV4(t *testing.T) {
@@ -15,7 +17,7 @@ func TestCreateAddrInetV4(t *testing.T) {
1517
t.Error("Ip Address size does not match", size)
1618
}
1719

18-
if ip1.sa_family != 2 {
20+
if ip1.sa_family != unix.AF_INET {
1921
t.Error("Ip Address family does not match")
2022
}
2123

@@ -43,7 +45,7 @@ func TestCreateAddrInetV6(t *testing.T) {
4345
t.Error("Ipv6 Address size does not match", size)
4446
}
4547

46-
if ip1.sa_family != 30 {
48+
if ip1.sa_family != unix.AF_INET6 {
4749
t.Error("Ipv6 Address family does not match")
4850
}
4951
data := []int{31, -102, 0, 0, 0, 0, 32, 1, 13, -72, -123, -93, 0, 0}

srtgo.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,15 @@ func (s SrtSocket) Accept() (*SrtSocket, *net.UDPAddr, error) {
197197
timeoutMs := C.int64_t(-1)
198198
ready := [2]C.int{SRT_INVALID_SOCK, SRT_INVALID_SOCK}
199199
if C.srt_epoll_wait(s.epollConnect, &ready[0], &len, nil, nil, timeoutMs, nil, nil, nil, nil) == -1 {
200-
return nil, nil, fmt.Errorf("srt accept, epoll wait issue")
200+
return nil, nil, fmt.Errorf("srt accept, epoll error: %s", C.GoString(C.srt_getlasterror_str()))
201201
}
202202
}
203203

204204
var addr syscall.RawSockaddrAny
205205
sclen := C.int(syscall.SizeofSockaddrAny)
206206
socket := C.srt_accept(s.socket, (*C.struct_sockaddr)(unsafe.Pointer(&addr)), &sclen)
207207
if socket == SRT_INVALID_SOCK {
208-
return nil, nil, fmt.Errorf("srt accept, error accepting the connection: %v", C.GoString(C.srt_getlasterror_str()))
208+
return nil, nil, fmt.Errorf("srt accept, error accepting the connection: %s", C.GoString(C.srt_getlasterror_str()))
209209
}
210210

211211
newSocket, err := newFromSocket(&s, socket)
@@ -263,7 +263,7 @@ func (s SrtSocket) Connect() error {
263263
return fmt.Errorf("srt connect, connection failed %d", state)
264264
}
265265
} else {
266-
return fmt.Errorf("srt connect, epoll wait issue")
266+
return fmt.Errorf("srt connect, epoll error: %s", C.GoString(C.srt_getlasterror_str()))
267267
}
268268
}
269269

srtgo_test.go

+111-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package srtgo
22

33
import (
4+
"sync"
45
"testing"
6+
"time"
57
)
68

79
func TestNewSocket(t *testing.T) {
@@ -97,19 +99,108 @@ func TestListen(t *testing.T) {
9799
}
98100
}
99101

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+
100187
func TestSetSockOptInt(t *testing.T) {
101188
InitSRT()
102189
options := make(map[string]string)
103190
a := NewSrtSocket("localhost", 8090, options)
104191

105-
err := a.SetSockOptInt(SRTO_LATENCY, 200)
192+
expected := 200
193+
err := a.SetSockOptInt(SRTO_LATENCY, expected)
106194
if err != nil {
107-
t.Error("Error on TestSetSockOpt")
195+
t.Error(err)
108196
}
109197

110198
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)
113204
}
114205
}
115206

@@ -118,14 +209,18 @@ func TestSetSockOptString(t *testing.T) {
118209
options := make(map[string]string)
119210
a := NewSrtSocket("localhost", 8090, options)
120211

121-
err := a.SetSockOptString(SRTO_STREAMID, "123")
212+
expected := "123"
213+
err := a.SetSockOptString(SRTO_STREAMID, expected)
122214
if err != nil {
123-
t.Error("Error on TestSetSockOpt")
215+
t.Error(err)
124216
}
125217

126218
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)
129224
}
130225
}
131226

@@ -134,13 +229,17 @@ func TestSetSockOptBool(t *testing.T) {
134229
options := make(map[string]string)
135230
a := NewSrtSocket("localhost", 8090, options)
136231

137-
err := a.SetSockOptBool(SRTO_MESSAGEAPI, true)
232+
expected := true
233+
err := a.SetSockOptBool(SRTO_MESSAGEAPI, expected)
138234
if err != nil {
139-
t.Error("Error on TestSetSockOpt")
235+
t.Error(err)
140236
}
141237

142238
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)
145244
}
146245
}

0 commit comments

Comments
 (0)