-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient_test.go
160 lines (155 loc) · 3.32 KB
/
client_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
154
155
156
157
158
159
160
package sse
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
)
const CLIENT_DELAY = time.Millisecond * 100
func receiveAtLeastNEvents(
n int,
c *Client,
timeout time.Duration,
) error {
tCh := time.After(timeout)
for i := 0; i < n; i++ {
select {
case _, ok := <-c.Events:
if !ok {
return errors.New("unexpected end of events")
}
case <-tCh:
return fmt.Errorf("expected %d events, received %d", n, i)
}
}
return nil
}
func flushAndWait(w http.ResponseWriter) {
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
time.Sleep(CLIENT_DELAY * 2)
}
func TestClient(t *testing.T) {
for _, v := range []struct {
Name string
Client func(c *Client) error
Server func(w http.ResponseWriter, r *http.Request, i int) error
}{
{
Name: "Cancel immediately",
Client: func(c *Client) error {
return nil
},
Server: func(w http.ResponseWriter, r *http.Request, i int) error {
return nil
},
},
{
Name: "Cancel while waiting for next event",
Client: func(c *Client) error {
time.Sleep(CLIENT_DELAY)
return nil
},
Server: func(w http.ResponseWriter, r *http.Request, i int) error {
w.Write([]byte(""))
flushAndWait(w)
return nil
},
},
{
Name: "Cancel while sending on channel",
Client: func(c *Client) error {
time.Sleep(CLIENT_DELAY)
c.Close()
if _, ok := <-c.Events; ok {
return errors.New("unexpected event received")
}
return nil
},
Server: func(w http.ResponseWriter, r *http.Request, i int) error {
w.Write([]byte("data\n\n"))
flushAndWait(w)
return nil
},
},
{
Name: "Send two events",
Client: func(c *Client) error {
return receiveAtLeastNEvents(2, c, CLIENT_DELAY)
},
Server: func(w http.ResponseWriter, r *http.Request, i int) error {
w.Write([]byte("data\n\ndata\n\n"))
return nil
},
},
{
Name: "Use last ID",
Client: func(c *Client) error {
return receiveAtLeastNEvents(2, c, CLIENT_DELAY)
},
Server: func(w http.ResponseWriter, r *http.Request, i int) error {
const eventID = "1"
if i > 0 {
lastEventID := r.Header.Get("Last-Event-ID")
if lastEventID != eventID {
w.WriteHeader(http.StatusNoContent)
return fmt.Errorf("%#v != %#v", lastEventID, eventID)
}
}
w.Write([]byte("id:1\nretry:50\ndata\n\n"))
return nil
},
},
{
Name: "Reply with 204",
Client: func(c *Client) error {
<-c.Events
_, ok := <-c.Events
if ok {
return errors.New("expected channel close")
}
return nil
},
Server: func(w http.ResponseWriter, r *http.Request, i int) error {
if i > 0 {
w.WriteHeader(http.StatusNoContent)
} else {
w.Write([]byte("retry:50\ndata\n\n"))
}
return nil
},
},
} {
func() {
var (
i = 0
serverErr error
)
s := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
if err := v.Server(w, r, i); err != nil {
serverErr = err
}
i += 1
},
))
defer func() {
s.Close()
if serverErr != nil {
t.Fatalf("%s: %s", v.Name, serverErr)
}
}()
c, err := NewClientFromURL(s.URL)
if err != nil {
t.Fatalf("%s: %s", v.Name, err)
}
defer c.Close()
if err := v.Client(c); err != nil {
t.Fatalf("%s: %s", v.Name, err)
}
}()
}
}