forked from aerogo/packet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stream_test.go
165 lines (122 loc) · 3.2 KB
/
Stream_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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package packet_test
import (
"net"
"testing"
"github.com/stretchr/testify/assert"
"github.com/aerogo/packet"
)
// Use the following helper messages to check goroutine lifetime:
//
// stream.read:
//
// fmt.Println("start read", connection.LocalAddr(), "->", connection.RemoteAddr())
// defer fmt.Println("end read", connection.LocalAddr(), "->", connection.RemoteAddr())
//
// stream.write:
//
// fmt.Println("start write", connection.LocalAddr(), "->", connection.RemoteAddr())
// defer fmt.Println("end write", connection.LocalAddr(), "->", connection.RemoteAddr())
func startServer(t *testing.T) net.Listener {
listener, err := net.Listen("tcp", ":7000")
assert.NotNil(t, listener)
assert.NoError(t, err)
go func() {
for {
conn, err := listener.Accept()
if conn == nil {
return
}
assert.NotNil(t, conn)
assert.NoError(t, err)
client := packet.NewStream(1024)
client.OnError(func(err packet.IOError) {
conn.Close()
})
client.SetConnection(conn)
go func() {
for msg := range client.Incoming {
assert.Equal(t, "ping", string(msg.Data))
client.Outgoing <- packet.New(0, []byte("pong"))
}
}()
}
}()
return listener
}
func TestCommunication(t *testing.T) {
// Server
server := startServer(t)
// Client
conn, err := net.Dial("tcp", "localhost:7000")
assert.NoError(t, err)
client := packet.NewStream(1024)
client.SetConnection(conn)
// Send message
client.Outgoing <- packet.New(0, []byte("ping"))
// Receive message
msg := <-client.Incoming
// Check message contents
assert.Equal(t, "pong", string(msg.Data))
// Close connection
conn.Close()
// Send packet (will be buffered until reconnect finishes)
client.Outgoing <- packet.New(0, []byte("ping"))
// Reconnect
conn, err = net.Dial("tcp", "localhost:7000")
assert.NoError(t, err)
// Hot-swap connection
client.SetConnection(conn)
// Receive message
msg = <-client.Incoming
assert.Equal(t, "pong", string(msg.Data))
// Close
client.Connection().Close()
server.Close()
}
func TestUtils(t *testing.T) {
ping := packet.New(0, []byte("ping"))
assert.Len(t, ping.Bytes(), 1+8+4)
length, err := packet.Int64FromBytes(packet.Int64ToBytes(ping.Length))
assert.NoError(t, err)
assert.Equal(t, ping.Length, length)
}
func TestDisconnect(t *testing.T) {
listener, err := net.Listen("tcp", ":7000")
assert.NotNil(t, listener)
assert.NoError(t, err)
go func() {
for {
conn, err := listener.Accept()
if conn == nil {
return
}
assert.NotNil(t, conn)
assert.NoError(t, err)
client := packet.NewStream(1024)
client.OnError(func(err packet.IOError) {
conn.Close()
})
client.SetConnection(conn)
go func() {
for msg := range client.Incoming {
assert.Equal(t, "ping", string(msg.Data))
client.Outgoing <- packet.New(0, []byte("pong"))
}
}()
}
}()
// Client
conn, err := net.Dial("tcp", "localhost:7000")
assert.NoError(t, err)
client := packet.NewStream(1024)
client.SetConnection(conn)
// Send message
client.Outgoing <- packet.New(0, []byte("ping"))
// Receive message
msg := <-client.Incoming
// Check message contents
assert.Equal(t, "pong", string(msg.Data))
// Close connection
conn.Close()
listener.Close()
}