-
Notifications
You must be signed in to change notification settings - Fork 5
/
main_test.go
109 lines (89 loc) · 1.86 KB
/
main_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
package main
import (
"net"
"os"
"testing"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func TestSmoke(t *testing.T) {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout})
go newEchoServer(":9064")
signalServerAddr := "ws://localhost:8000/connection/websocket"
go NewAs("smoke", "secret", ":9064", signalServerAddr)
time.Sleep(1 * time.Second)
to := NewTo("smoke", "secret", signalServerAddr, ":9063")
go to.ListenAndServe()
time.Sleep(1 * time.Second)
receiveCount := 0
go func() {
addr, err := net.ResolveTCPAddr("tcp", ":9063")
if err != nil {
panic(err)
}
conn, err := net.DialTCP("tcp", nil, addr)
if err != nil {
panic(err)
}
log.Print("connected")
// read from socket
go func() {
for {
data := make([]byte, 1024)
n, err := conn.Read(data)
if err != nil {
panic(err)
}
resp := string(data[:n])
log.Printf("echo received: %#v", resp)
if resp == "smoke test\n" {
receiveCount++
} else {
t.Errorf("incorrect echo data %#v", data)
}
if receiveCount == 3 {
os.Exit(0)
}
}
}()
for i := 0; i < 3; i++ {
if _, err := conn.Write([]byte("smoke test\n")); err != nil {
panic(err)
}
time.Sleep(1 * time.Second)
}
}()
for {
}
}
func newEchoServer(port string) {
l, err := net.Listen("tcp", port)
if err != nil {
panic(err)
}
log.Print("Listening to connections on :9064")
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
panic(err)
}
go handleRequest(conn)
}
}
func handleRequest(conn net.Conn) {
log.Print("Accepted new connection.")
defer conn.Close()
defer log.Print("Closed connection.")
for {
buf := make([]byte, 1024)
size, err := conn.Read(buf)
if err != nil {
return
}
data := buf[:size]
log.Printf("Read new data from connection %#v", string(data))
conn.Write(data)
}
}