-
Notifications
You must be signed in to change notification settings - Fork 451
/
io_chan_test.go
223 lines (203 loc) · 5.43 KB
/
io_chan_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package stream
import (
"bytes"
"io"
"testing"
"time"
)
func TestBasicReadWrite(t *testing.T) {
send := []byte("hello world")
c := make(chan *StreamChunk)
writer := NewChanWriter(c)
reader := NewChanReader(c)
go writer.Write(send)
buf := make([]byte, len(send))
n, err := reader.Read(buf)
if n != len(send) {
t.Fatalf("Read wrong number of bytes: %d expected %d", n, len(send))
}
if err != nil {
t.Fatal("Couldn't read from stream", err)
}
if !bytes.Equal(buf, send) {
t.Fatal("Got wrong message from stream", string(buf))
}
writer.Close()
n, err = reader.Read(buf)
if err != io.EOF {
t.Fatal("Read returned wrong error after close:", err)
}
if n != 0 {
t.Fatalf("Read still returned data after close: %d bytes", n)
}
}
func TestReadMoreThanWrite(t *testing.T) {
send := []byte("hello world")
c := make(chan *StreamChunk)
writer := NewChanWriter(c)
reader := NewChanReader(c)
go writer.Write(send)
buf := make([]byte, len(send)+10)
n, err := reader.Read(buf)
if err != nil {
t.Fatal("Couldn't read from stream", err)
}
if n != len(send) {
t.Fatalf("Read wrong number of bytes: %d expected %d", n, len(send))
}
if !bytes.Equal(buf[:n], send) {
t.Fatal("Got wrong message from stream", string(buf[:n]))
}
writer.Close()
n, err = reader.Read(buf)
if err != io.EOF {
t.Fatal("Read returned wrong error after close:", err)
}
if n != 0 {
t.Fatalf("Read still returned data after close: %d bytes", n)
}
}
func TestReadLessThanWrite(t *testing.T) {
send := []byte("hello world")
c := make(chan *StreamChunk)
writer := NewChanWriter(c)
reader := NewChanReader(c)
go writer.Write(send)
buf := make([]byte, 6)
n, err := reader.Read(buf)
if err != nil {
t.Fatal("Couldn't read from stream", err)
}
if n != len(buf) {
t.Fatalf("Read wrong number of bytes: %d expected %d", n, len(buf))
}
if !bytes.Equal(buf, send[:len(buf)]) {
t.Fatal("Got wrong message from stream", string(buf))
}
writer.Close()
n, err = reader.Read(buf)
if err != nil {
t.Fatal("Couldn't read from stream", err)
}
if n != len(send)-len(buf) {
t.Fatalf("Read wrong number of bytes: %d expected %d", n, len(send)-len(buf))
}
if !bytes.Equal(buf[:n], send[len(buf):]) {
t.Fatal("Got wrong message from stream", string(buf[:n]))
}
n, err = reader.Read(buf)
if err != io.EOF {
t.Fatal("Read returned wrong error after close:", err)
}
if n != 0 {
t.Fatalf("Read still returned data after close: %d bytes", n)
}
}
func TestMultiReadWrite(t *testing.T) {
send := []byte("hello world, this message is longer")
c := make(chan *StreamChunk)
writer := NewChanWriter(c)
reader := NewChanReader(c)
go func() {
writer.Write(send[:9])
writer.Write(send[9:19])
writer.Write(send[19:])
writer.Close()
}()
buf := make([]byte, 10)
for read := 0; read < len(send); {
n, err := reader.Read(buf)
if err != nil {
t.Fatal("Couldn't read from stream", err, n)
}
if !bytes.Equal(buf[:n], send[read:read+n]) {
t.Fatal("Got wrong message from stream", string(buf))
}
read += n
}
n, err := reader.Read(buf)
if err != io.EOF {
t.Fatal("Read returned wrong error after close:", err, string(buf[:n]))
}
if !bytes.Equal(buf[:n], send[len(send)-n:]) {
t.Fatal("Got wrong message from stream", string(buf[:n]))
}
}
func TestMultiWriteWithCopy(t *testing.T) {
send := []byte("hello world, this message is longer")
c := make(chan *StreamChunk)
writer := NewChanWriter(c)
reader := NewChanReader(c)
go func() {
writer.Write(send[:9])
writer.Write(send[9:19])
writer.Write(send[19:])
writer.Close()
}()
buf := new(bytes.Buffer)
n, err := io.Copy(buf, reader)
if int(n) != len(send) {
t.Fatalf("Read wrong number of bytes: %d expected %d", n, len(send))
}
if err != nil {
t.Fatal("Couldn't read from stream", err)
}
if !bytes.Equal(buf.Bytes(), send) {
t.Fatal("Got wrong message from stream", buf.String())
}
}
func TestStream_ReadCorrectness(t *testing.T) {
sendMsg := []byte("hello world")
c := make(chan *StreamChunk)
interrupt := make(chan struct{})
writer := NewChanWriter(c)
reader := NewChanReader(c)
reader.SetInterrupt(interrupt)
go writer.Write(sendMsg)
readMsg := make([]byte, len(sendMsg))
n, err := reader.Read(readMsg)
if err != nil {
t.Fatalf("Couldn't read from stream: %v", err)
}
if n != len(sendMsg) {
t.Fatalf("Read wrong number of bytes: %d expected %d", n, len(sendMsg))
}
if !bytes.Equal(readMsg, sendMsg) {
t.Fatal("Got wrong message from stream", string(readMsg))
}
}
func TestStream_ReadInterrupt(t *testing.T) {
sendMsg := []byte("hello world")
c := make(chan *StreamChunk)
interrupt := make(chan struct{})
writer := NewChanWriter(c)
reader := NewChanReader(c)
reader.SetInterrupt(interrupt)
go writer.Write(sendMsg)
readMsg := make([]byte, len(sendMsg))
reader.Read(readMsg)
// Interrupting the stream mid-read
go func() {
time.Sleep(50 * time.Millisecond)
interrupt <- struct{}{}
}()
n, err := reader.Read(readMsg)
if err != ErrInterrupted {
t.Fatalf("Read returned wrong error after interrupt: %v", err)
}
if n != 0 {
t.Fatalf("Read still returned data after interrput: %d bytes", n)
}
// Try writing again after the channel was interrupted
go writer.Write(sendMsg)
n, err = reader.Read(readMsg)
if n != len(sendMsg) {
t.Fatalf("Read wrong number of bytes: %d expected %d", n, len(sendMsg))
}
if err != nil {
t.Fatalf("Couldn't read from stream: %v", err)
}
if !bytes.Equal(readMsg, sendMsg) {
t.Fatal("Got wrong message from stream", string(readMsg))
}
}