-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathio_test.go
43 lines (36 loc) · 953 Bytes
/
io_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
package helpers
import (
"bytes"
"io"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestReadWithTimeout(t *testing.T) {
var buf1 bytes.Buffer
data := ReadWithTimeout(&buf1, 5, time.Millisecond*50)
assert.Len(t, data, 0)
var buf2 bytes.Buffer
buf2.WriteString("he")
data = ReadWithTimeout(&buf2, 5, time.Millisecond*50)
assert.Equal(t, "he", string(data))
var buf3 bytes.Buffer
buf3.WriteString("hello")
data = ReadWithTimeout(&buf3, 5, time.Millisecond*50)
assert.Equal(t, "hello", string(data))
r1, w1 := io.Pipe()
go func() {
w1.Write([]byte("good"))
time.Sleep(10 * time.Millisecond)
w1.Write([]byte("bye"))
}()
data = ReadWithTimeout(r1, 7, time.Millisecond*100)
assert.Equal(t, "goodbye", string(data))
r2, w2 := io.Pipe()
go func() {
w2.Write([]byte("good"))
}()
data = ReadWithTimeout(r2, 7, time.Millisecond*100)
time.Sleep(time.Millisecond * 100)
assert.Equal(t, "good", string(data))
}