forked from sourcegraph/httpfstream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream_test.go
68 lines (57 loc) · 1.32 KB
/
stream_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
package httpfstream
import (
"io"
"log"
"net/url"
"strings"
"testing"
"time"
)
func waitForWrite() {
time.Sleep(time.Millisecond * 10)
}
func TestStream(t *testing.T) {
t.Parallel()
server := newTestServer()
defer server.close()
path := "/stream"
u, _ := url.Parse(server.URL + path)
log.SetFlags(0)
w, err := OpenAppend(u)
if err != nil {
t.Fatalf("OpenAppend: %s", err)
}
defer w.Close()
r, err := Follow(u)
if err != nil {
t.Fatalf("Follow: %s", err)
}
defer r.Close()
writedata := []string{"foo", "bar", "baz", "qux"}
wantdata := []string{"abcfoo", "bar", "baz", "qux"}
io.WriteString(w, "abc")
for i, d := range writedata {
io.WriteString(w, d)
waitForWrite()
// Check that APPEND data is persisted.
want := strings.Join(wantdata[:i+1], "")
if got := httpGET(t, u); want != got {
t.Errorf("want all == %q, got %q", want, got)
return
}
// Check that APPEND data is sent to WebSocket reader.
followdata := string(limitRead(t, r, int64(len(wantdata[i]))))
if wantdata[i] != followdata {
t.Errorf("want msg == %q, got %q", wantdata[i], followdata)
}
}
}
func limitRead(t *testing.T, rdr io.Reader, n int64) []byte {
lr := io.LimitReader(rdr, n)
data := make([]byte, n)
_, err := io.ReadAtLeast(lr, data, int(n))
if err != nil {
t.Fatal("limitRead", err)
}
return data
}