-
Notifications
You must be signed in to change notification settings - Fork 10
/
udp_test.go
101 lines (81 loc) · 2.25 KB
/
udp_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
package udp
import (
"net"
"testing"
"time"
)
var (
testAddr = ":8126"
)
func setup(t *testing.T) net.Conn {
udpClient, err := net.DialTimeout("udp", testAddr, time.Second)
if err != nil {
t.Fatal(err)
}
SetAddr(testAddr)
return udpClient
}
func TestAll(t *testing.T) {
udpClient := setup(t)
testValues := [][]interface{}{
{"foo", "foo", true, true},
{"foo", "bar", false, false},
{"foo", "foobar", false, true},
{"foo", "", false, false},
{"", "", true, true},
}
for _, values := range testValues {
shouldGet := values[0].(string)
sendString := values[1].(string)
shouldEquals := values[2].(bool)
shouldContains := values[3].(bool)
got, equals, contains := get(t, shouldGet, func() {
udpClient.Write([]byte(sendString))
})
if got != sendString {
t.Errorf("Should've got %#v but got %#v", sendString, got)
}
if equals != shouldEquals {
t.Errorf("Equals should've been %#v but was %#v", shouldEquals, equals)
}
if contains != shouldContains {
t.Errorf("Contains should've been %#v but was %#v", shouldContains, contains)
}
}
ShouldReceiveOnly(t, "foo", func() {
udpClient.Write([]byte("foo"))
})
ShouldNotReceiveOnly(t, "bar", func() {
udpClient.Write([]byte("foo"))
})
ShouldReceive(t, "foo", func() {
udpClient.Write([]byte("barfoo"))
})
ShouldNotReceive(t, "bar", func() {
udpClient.Write([]byte("fooba"))
})
ShouldReceiveAll(t, []string{"foo", "bar"}, func() {
udpClient.Write([]byte("foobizbar"))
})
ShouldNotReceiveAny(t, []string{"fooby", "bars"}, func() {
udpClient.Write([]byte("foobizbar"))
})
ShouldReceiveAllAndNotReceiveAny(t, []string{"foo", "bar"}, []string{"fooby", "bars"}, func() {
udpClient.Write([]byte("foo"))
udpClient.Write([]byte("biz"))
udpClient.Write([]byte("bar"))
})
// This should fail, but it also shouldn't stall out
// ShouldReceive(t, "foo", func() {})
}
func TestRaceConditionInReadingResults(t *testing.T) {
udpClient := setup(t)
ShouldReceiveAllAndNotReceiveAny(t, []string{"foo", "bar", "biz"}, []string{"fooby", "bars"}, func() {
time.Sleep(time.Millisecond * 100)
udpClient.Write([]byte("foo"))
time.Sleep(time.Millisecond * 200)
udpClient.Write([]byte("biz"))
time.Sleep(time.Millisecond * 500)
udpClient.Write([]byte("bar"))
})
}