-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcptest_test.go
60 lines (49 loc) · 1.07 KB
/
tcptest_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 tcptest
import (
"fmt"
"net"
"reflect"
"testing"
"time"
)
func TestServer(t *testing.T) {
server, err := NewServer()
if err != nil {
t.Fatal(err)
}
conn, err := net.Dial("tcp", server.Address())
if err != nil {
t.Fatal(err)
}
if server.Received("foo") {
t.Error("haven't sent anything yet")
}
fmt.Fprintf(conn, "cool\nneat\nincomplete")
err = server.WaitForLines(2, time.Second)
if err != nil {
t.Error(err)
}
if !server.Received("oo") {
t.Error("didn't receive 'oo'")
}
if !server.ReceivedLine("cool") {
t.Error("didn't receive line 'cool'")
}
err = server.WaitForLines(3, time.Millisecond)
if err == nil {
t.Error("expected error, got none")
}
if server.Received("incomplete") {
t.Error("server should only count complete lines as received")
}
if server.ReceivedLine("incomplete") {
t.Error("server should only count complete lines as received")
}
expectLines := []string{"cool", "neat"}
got := server.Lines()
if !reflect.DeepEqual(got, expectLines) {
t.Errorf("got lines: %#v", got)
}
conn.Close()
server.Close()
}