forked from Netflix/go-expect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_lease_test.go
64 lines (54 loc) · 1.02 KB
/
reader_lease_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
package expect
import (
"context"
"io"
"sync"
"testing"
"github.com/stretchr/testify/require"
)
func TestReaderLease(t *testing.T) {
in, out := io.Pipe()
defer out.Close()
defer in.Close()
rm := NewReaderLease(in)
tests := []struct {
title string
expected string
}{
{
"Read cancels with deadline",
"apple",
},
{
"Second read has no bytes stolen",
"banana",
},
}
for _, test := range tests {
t.Run(test.title, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
tin, tout := io.Pipe()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
io.Copy(tout, rm.NewReader(ctx))
}()
wg.Add(1)
go func() {
defer wg.Done()
_, err := out.Write([]byte(test.expected))
require.Nil(t, err)
}()
for i := 0; i < len(test.expected); i++ {
p := make([]byte, 1)
n, err := tin.Read(p)
require.Nil(t, err)
require.Equal(t, 1, n)
require.Equal(t, test.expected[i], p[0])
}
cancel()
wg.Wait()
})
}
}