forked from Netflix/go-expect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passthrough_pipe_test.go
53 lines (39 loc) · 1.04 KB
/
passthrough_pipe_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
package expect
import (
"errors"
"io"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestPassthroughPipe(t *testing.T) {
r, w := io.Pipe()
passthroughPipe, err := NewPassthroughPipe(r)
require.NoError(t, err)
err = passthroughPipe.SetReadDeadline(time.Now().Add(time.Hour))
require.NoError(t, err)
pipeError := errors.New("pipe error")
err = w.CloseWithError(pipeError)
require.NoError(t, err)
p := make([]byte, 1)
_, err = passthroughPipe.Read(p)
require.Equal(t, err, pipeError)
}
func TestPassthroughPipeTimeout(t *testing.T) {
r, w := io.Pipe()
passthroughPipe, err := NewPassthroughPipe(r)
require.NoError(t, err)
err = passthroughPipe.SetReadDeadline(time.Now())
require.NoError(t, err)
_, err = w.Write([]byte("a"))
require.NoError(t, err)
p := make([]byte, 1)
_, err = passthroughPipe.Read(p)
require.True(t, os.IsTimeout(err))
err = passthroughPipe.SetReadDeadline(time.Time{})
require.NoError(t, err)
n, err := passthroughPipe.Read(p)
require.Equal(t, 1, n)
require.NoError(t, err)
}