-
Notifications
You must be signed in to change notification settings - Fork 13
/
hare_test.go
85 lines (67 loc) · 1.41 KB
/
hare_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
package hare
import (
"testing"
"time"
)
func TestSend(t *testing.T) {
expected := "Hello World"
r, _ := Listen("3000")
Send("3000", expected)
if r.HasNewMessages() {
if r.GetMessage() != expected {
t.Errorf("Output different from input")
}
}
}
func TestSendFailConnect(t *testing.T) {
err := Send("3001", "Hello")
if err == nil {
t.Errorf("Sended message to unknow port")
}
}
func TestListen(t *testing.T) {
r, err := Listen("3002")
if err != nil {
panic(err)
}
Send("3002", "Hey Hey")
// waiting for the message to be sended
time.Sleep(time.Second)
if r.HasNewMessages() == false {
t.Errorf("Listen didn't receive any messsage")
}
}
func TestListenBusyPort(t *testing.T) {
Listen("3003")
_, err := Listen("3003")
if err == nil {
t.Errorf("Listen opened a busy port")
}
}
func TestGetMessage(t *testing.T) {
expected := "Hey gopher!"
r, err := Listen("3004")
if err != nil {
t.Errorf("Couldn't open port")
}
Send("3004", expected)
time.Sleep(time.Second)
if r.GetMessage() != expected {
t.Errorf("Input message different from output")
}
}
func TestListenStop(t *testing.T) {
expected := "Hello World"
r, _ := Listen("3005")
Send("3005", expected)
if r.HasNewMessages() {
if r.GetMessage() != expected {
t.Errorf("Output different from input")
}
}
r.Stop()
err := Send("3005", "This should fails")
if err == nil {
t.Errorf("Couldn't close connection")
}
}