-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathapi_test.go
57 lines (50 loc) · 990 Bytes
/
api_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
package tcp
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vadv/gopher-lua-libs/tests"
"io"
"net"
"testing"
"time"
)
func runPingPongServer(addr string) (io.Closer, error) {
listener, err := net.Listen("tcp", addr)
if err != nil {
return nil, nil
}
go func() {
for {
conn, err := listener.Accept()
if err != nil {
panic(err)
}
handleTCPClient(conn)
}
}()
return listener, nil
}
func handleTCPClient(conn net.Conn) {
buf := make([]byte, 1024)
for {
count, err := conn.Read(buf)
if err != nil {
return
}
data := buf[0:count]
if string(data) == "ping" {
conn.Write([]byte("pong\n"))
} else {
conn.Write([]byte("unknown\n"))
}
}
}
func TestApi(t *testing.T) {
closer, err := runPingPongServer(":12345")
require.NoError(t, err)
t.Cleanup(func() {
_ = closer.Close()
})
time.Sleep(time.Second)
assert.NotZero(t, tests.RunLuaTestFile(t, Preload, "./test/test_api.lua"))
}