forked from firstrow/tcp_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_server_test.go
60 lines (51 loc) · 1.34 KB
/
tcp_server_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
package tcp_server
import (
. "github.com/smartystreets/goconvey/convey"
"net"
"testing"
"time"
)
func buildTestServer() *server {
return New("localhost:9999")
}
func Test_accepting_new_client_callback(t *testing.T) {
server := buildTestServer()
var messageReceived bool
var messageText string
var newClient bool
var connectinClosed bool
server.OnNewClient(func(c *Client) {
newClient = true
})
server.OnNewMessage(func(c *Client, message string) {
messageReceived = true
messageText = message
})
server.OnClientConnectionClosed(func(c *Client, err error) {
connectinClosed = true
})
go server.Listen()
// Wait for server
// If test fails - increase this values
time.Sleep(10 * time.Millisecond)
conn, err := net.Dial("tcp", "localhost:9999")
if err != nil {
t.Fatal("Failed to connect to test server")
}
conn.Write([]byte("Test message\n"))
conn.Close()
// Wait for server
time.Sleep(10 * time.Millisecond)
Convey("Messages should be equal", t, func() {
So(messageText, ShouldEqual, "Test message\n")
})
Convey("It should receive new client callback", t, func() {
So(newClient, ShouldEqual, true)
})
Convey("It should receive message callback", t, func() {
So(messageReceived, ShouldEqual, true)
})
Convey("It should receive connection closed callback", t, func() {
So(connectinClosed, ShouldEqual, true)
})
}