/
server_test.go
153 lines (141 loc) · 3.45 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
151
152
153
package testsrv
import (
"net/http"
"sync"
"testing"
"time"
"github.com/arschles/assert"
)
const (
recvWaitTime = 10 * time.Millisecond
)
func TestNoMsgs(t *testing.T) {
srv := StartServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}))
defer srv.Close()
recvCh := make(chan []*ReceivedRequest)
// ensure that it returns after waitTime
go func() {
recvCh <- srv.AcceptN(20, recvWaitTime/2)
}()
select {
case recv := <-recvCh:
assert.Equal(t, len(recv), 0, "number of received messages")
case <-time.After(recvWaitTime):
t.Errorf("AcceptN didn't return after [%+v]", recvWaitTime)
}
}
func TestMsgs(t *testing.T) {
const numSends = 20
srv := StartServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
ch := make(chan []*ReceivedRequest)
waitTime := 10 * time.Millisecond
for i := 0; i < numSends; i++ {
resp, err := http.Get(srv.URLStr())
assert.NoErr(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode, "status code")
}
go func() {
ch <- srv.AcceptN(numSends, waitTime)
}()
select {
case r := <-ch:
assert.Equal(t, len(r), numSends, "number of recevied messages")
case <-time.After(recvWaitTime):
t.Errorf("AcceptN didn't return after [%+v]", recvWaitTime)
}
}
func TestConcurrentClose(t *testing.T) {
srv := StartServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
defer srv.Close()
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
srv.Close()
}()
}
wg.Wait()
}
func TestConcurrentSend(t *testing.T) {
const numSends = 100
srv := StartServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
var wg sync.WaitGroup
for i := 0; i < numSends; i++ {
wg.Add(1)
go func() {
defer wg.Done()
resp, err := http.Get(srv.URLStr())
if err != nil {
t.Errorf("error on GET [%s]", err)
return
}
if resp.StatusCode != http.StatusOK {
t.Errorf("expected code [%d] got [%d]", http.StatusOK, resp.StatusCode)
return
}
}()
}
wg.Wait()
recv := srv.AcceptN(numSends, 10*time.Millisecond)
assert.Equal(t, numSends, len(recv), "number of received requests")
}
func TestHandlerSleep(t *testing.T) {
const numSends = 10
ch := make(chan struct{})
srv := StartServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.(http.Flusher).Flush()
<-ch
}))
defer srv.Close()
defer close(ch)
var wg sync.WaitGroup
for i := 0; i < numSends; i++ {
wg.Add(1)
go func() {
defer wg.Done()
resp, err := http.Get(srv.URLStr())
if err != nil {
t.Errorf("got error on GET when not expected [%s]", err)
return
}
if resp.StatusCode != http.StatusOK {
t.Errorf("got status [%d] expected [%d]", resp.StatusCode, http.StatusOK)
return
}
}()
}
wgChan := make(chan struct{})
go func() {
wg.Wait()
close(wgChan)
}()
select {
case <-wgChan:
case <-time.After(100 * time.Millisecond):
t.Fatalf("not all senders completed within 100ms")
}
recv := srv.AcceptN(numSends, 10*time.Millisecond)
assert.Equal(t, 0, len(recv), "num received messages")
}
func TestConcurrenctClose(t *testing.T) {
srv := StartServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
srv.Close()
}()
}
wg.Wait()
}