This repository has been archived by the owner on Nov 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 315
/
Copy pathstream_utils_test.go
111 lines (102 loc) · 2.32 KB
/
stream_utils_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
package twitter
import (
"bufio"
"bytes"
"io"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestStopped(t *testing.T) {
done := make(chan struct{})
assert.False(t, stopped(done))
close(done)
assert.True(t, stopped(done))
}
func TestSleepOrDone_Sleep(t *testing.T) {
wait := time.Nanosecond * 20
done := make(chan struct{})
completed := make(chan struct{})
go func() {
sleepOrDone(wait, done)
close(completed)
}()
// wait for goroutine SleepOrDone to sleep
assertDone(t, completed, defaultTestTimeout)
}
func TestSleepOrDone_Done(t *testing.T) {
wait := time.Second * 5
done := make(chan struct{})
completed := make(chan struct{})
go func() {
sleepOrDone(wait, done)
close(completed)
}()
// close done, interrupting SleepOrDone
close(done)
// assert that SleepOrDone exited, closing completed
assertDone(t, completed, defaultTestTimeout)
}
func TestStreamResponseBodyReader(t *testing.T) {
cases := []struct {
in []byte
want [][]byte
}{
{
in: []byte("foo\r\nbar\r\n"),
want: [][]byte{
[]byte("foo"),
[]byte("bar"),
},
},
{
in: []byte("foo\nbar\r\n"),
want: [][]byte{
[]byte("foo\nbar"),
},
},
{
in: []byte("foo\r\n\r\n"),
want: [][]byte{
[]byte("foo"),
[]byte(""),
},
},
{
in: []byte("foo\r\nbar"),
want: [][]byte{
[]byte("foo"),
[]byte("bar"),
},
},
{
// Message length is more than bufio.MaxScanTokenSize, which can't be
// parsed by bufio.Scanner with default buffer size.
in: []byte(strings.Repeat("X", bufio.MaxScanTokenSize+1) + "\r\n"),
want: [][]byte{
[]byte(strings.Repeat("X", bufio.MaxScanTokenSize+1)),
},
},
}
for _, c := range cases {
body := bytes.NewReader(c.in)
reader := newStreamResponseBodyReader(body)
for i, want := range c.want {
data, err := reader.readNext()
if err != nil {
t.Errorf("reader(%q).readNext() * %d: err == %q, want nil", c.in, i, err)
}
if !bytes.Equal(data, want) {
t.Errorf("reader(%q).readNext() * %d: data == %q, want %q", c.in, i, data, want)
}
}
data, err := reader.readNext()
if err != io.EOF {
t.Errorf("reader(%q).readNext() * %d: err == %q, want io.EOF", c.in, len(c.want), err)
}
if len(data) != 0 {
t.Errorf("reader(%q).readNext() * %d: data == %q, want \"\"", c.in, len(c.want), data)
}
}
}