-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
150 lines (116 loc) · 3.89 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
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
package gourd
import (
"bufio"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"net"
"testing"
"time"
)
func Test_wire_server_accepts_one_connection_on_the_specified_port(t *testing.T) {
var specified_port uint = 2531
testee := &gourd_wire_server{}
done := start_wire_server(testee, specified_port)
// Give the wire server some time to start accepting connection
time.Sleep(time.Millisecond)
conn, err := net.Dial("tcp", fmt.Sprintf("localhost:%d", specified_port))
assert.Nil(t, err, "Wire server is not listening.")
conn.Close()
assert_wire_server_exits(t, done)
}
func Test_wire_server_forwards_commands_to_parser_without_newlines(t *testing.T) {
parser := &parser_mock{}
testee := &gourd_wire_server{parser}
done := start_wire_server(testee, DefaultPort)
expected_command := []byte(`["begin_scenario"]`)
parser.On("parse", expected_command).Return("").Once()
// Give the wire server some time to start accepting connection
time.Sleep(time.Millisecond)
conn, err := net.Dial("tcp", "localhost:1847")
assert.Nil(t, err, "Wireserver is not listening.")
command := append(expected_command, '\n')
writer := bufio.NewWriter(conn)
_, err = writer.Write(command)
assert.Nil(t, err, "Failed to send command to wire server.")
writer.Flush()
conn.Close()
assert_wire_server_exits(t, done)
parser.Mock.AssertExpectations(t)
}
func Test_wire_server_reads_and_parses_line_by_line(t *testing.T) {
parser := &parser_mock{}
testee := &gourd_wire_server{parser}
done := start_wire_server(testee, DefaultPort)
// Give the wire server some time to start accepting connection
time.Sleep(time.Millisecond)
conn, err := net.Dial("tcp", "localhost:1847")
assert.Nil(t, err, "Wireserver is not listening.")
// First command
command := []byte(`["begin_scenario"]`)
parser.On("parse", command).Return("").Once()
writer := bufio.NewWriter(conn)
_, err = writer.Write(append(command, '\n'))
assert.Nil(t, err, "Failed to send command to wire server.")
writer.Flush()
// Next command
command = []byte(`["end_scenario"]`)
parser.On("parse", command).Return("").Once()
_, err = writer.Write(append(command, '\n'))
assert.Nil(t, err, "Failed to send command to wire server.")
writer.Flush()
conn.Close()
assert_wire_server_exits(t, done)
parser.Mock.AssertExpectations(t)
}
func Test_wire_server_sends_response_from_parser_including_newline(t *testing.T) {
parser := &parser_mock{}
testee := &gourd_wire_server{parser}
done := start_wire_server(testee, DefaultPort)
response := `["success"]`
parser.On("parse", mock.Anything).Return(response).Once()
// Give the wire server some time to start accepting connection
time.Sleep(time.Millisecond)
conn, err := net.Dial("tcp", "localhost:1847")
assert.Nil(t, err, "Wireserver is not listening.")
command := []byte(`["begin_scenario"]`)
writer := bufio.NewWriter(conn)
_, err = writer.Write(append(command, '\n'))
assert.Nil(t, err, "Failed to send command to wire server.")
writer.Flush()
expected_response := response + "\n"
assert_wire_server_responds(t, conn, expected_response)
conn.Close()
assert_wire_server_exits(t, done)
parser.Mock.AssertExpectations(t)
}
func start_wire_server(server *gourd_wire_server, port uint) chan bool {
done := make(chan bool)
go func() {
server.listen(port)
done <- true
}()
return done
}
func assert_wire_server_exits(t *testing.T, done chan bool) {
select {
case <-done:
case <-time.After(1 * time.Second):
assert.Fail(t, "Wireserver did not exit.")
}
}
func assert_wire_server_responds(t *testing.T, conn net.Conn, response string) {
done := make(chan bool)
go func() {
reader := bufio.NewReader(conn)
line, err := reader.ReadString('\n')
assert.Nil(t, err, "Failed to read response from wire server.")
assert.Equal(t, response, line)
done <- true
}()
select {
case <-done:
case <-time.After(1 * time.Second):
assert.Fail(t, "Wireserver did not respond.")
}
}