-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp_test.go
89 lines (75 loc) · 1.66 KB
/
udp_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
package cuckooc
import (
"context"
"net"
"runtime"
"sync"
"testing"
)
func initSocket(config Config) (context.Context, *sync.WaitGroup, chan Executor, context.CancelFunc) {
wg := new(sync.WaitGroup)
ctx, cancel := context.WithCancel(context.Background())
reqCh := make(chan Executor)
gk := NewGatekeeper(reqCh)
wg.Add(1)
go gk.Start(ctx, config, wg)
runtime.Gosched()
return ctx, wg, reqCh, cancel
}
func testSocket(t *testing.T, c net.Conn) {
tests := []struct {
cmd string
result string
}{
{
cmd: "test new",
result: "true",
},
{
cmd: "test setu a b c d e f g",
result: "true true true true true true true",
},
{
cmd: "test check a b 1 2 c d e f g",
result: "true true false false true true true true true",
},
{
cmd: "test set 1 2 3 4\ntest check 1 2 3 5",
result: "true true true true\ntrue true true false",
},
{
cmd: "test stop",
result: "true",
},
}
b := make([]byte, 1024)
for _, s := range tests {
_, err := c.Write([]byte(s.cmd))
if err != nil {
t.Fatalf("failed to write data on socket: %v", err)
}
n, err := c.Read(b)
if err != nil {
t.Fatalf("failed to read data from socket :%v", err)
}
res := string(b[:n])
if s.result != res {
t.Fatalf("expected %s but got %s", s.result, res)
}
}
c.Close()
}
func TestUDP_integration(t *testing.T) {
config := Config{UDP: ":5000"}
ctx, wg, reqCh, cancel := initSocket(config)
wg.Add(1)
defer wg.Wait()
defer cancel()
go StartUDPServer(ctx, config, wg, reqCh)
runtime.Gosched()
c, err := net.Dial("udp", config.UDP)
if err != nil {
t.Fatalf("failed to initiate udp connection: %v", err)
}
testSocket(t, c)
}