-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_test.go
84 lines (62 loc) · 1.7 KB
/
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"fmt"
"net"
"testing"
"time"
)
var _ = func() bool {
testing.Init()
return true
}()
func TestServerRoomGame(t *testing.T) {
go start()
time.Sleep(1 * time.Second)
wrongTokenConn, err := net.Dial("tcp", ":12345")
if err != nil {
t.Fatal(err)
}
fmt.Fprintf(wrongTokenConn, "connect\n")
//try to connect with wrong token
fmt.Fprintf(wrongTokenConn, "%s\n", "wrong123")
var response string
fmt.Fscan(wrongTokenConn, &response)
//we expect the server to return "wrong_token" for the one who connects
if response != "wrong_token" {
t.Errorf("expected response to be \"wrong token\" but it was %s\n", response)
}
wrongTokenConn.Close()
conn, err := net.Dial("tcp", ":12345")
if err != nil {
t.Fatal(err)
}
//first the one who creates room connects
fmt.Fprintf(conn, "wait\n")
var token string
fmt.Fscan(conn, &token)
//we expect the server to return us a 5 characters random token
if len(token) != 5 {
t.Errorf("server did not return 5 character token but returned %s\n", token)
}
connectToRoom, err := net.Dial("tcp", ":12345")
if err != nil {
t.Fatal(err)
}
fmt.Fprintf(connectToRoom, "connect\n")
fmt.Fprintf(connectToRoom, "%s\n", token)
fmt.Fscan(connectToRoom, &response)
if response != "first" {
t.Errorf("expected response to be \"first\" but it was %s\n", response)
}
fmt.Fscan(conn, &response)
if response != "second" {
t.Errorf("expected response to be \"second\" but it was %s\n", response)
}
//the first player places
fmt.Fprintf(connectToRoom, "2\n")
//expect the server to return where the other player has placed
fmt.Fscan(conn, &response)
if response != "2" {
t.Errorf("expected response to be 2 but it was %s\n", response)
}
}