forked from muesli/cancelreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cancelreader_fallback_test.go
88 lines (77 loc) · 1.81 KB
/
cancelreader_fallback_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
package cancelreader
import (
"bytes"
"fmt"
"io/ioutil"
"sync"
"testing"
)
type blockingReader struct {
sync.Mutex
read bool
unblockCh chan bool
startedCh chan bool
}
func (r *blockingReader) Read([]byte) (int, error) {
defer func() {
r.Lock()
defer r.Unlock()
r.read = true
}()
r.startedCh <- true
<-r.unblockCh
return 0, fmt.Errorf("this error should be ignored")
}
func TestFallbackReaderConcurrentCancel(t *testing.T) {
doneCh := make(chan bool, 1)
startedCh := make(chan bool, 1)
unblockCh := make(chan bool, 1)
r := blockingReader{
startedCh: startedCh,
unblockCh: unblockCh,
}
cr, err := newFallbackCancelReader(&r)
if err != nil {
t.Errorf("expected no error, but got %s", err)
}
go func() {
defer func() { doneCh <- true }()
if _, err := ioutil.ReadAll(cr); err != ErrCanceled {
t.Errorf("expected canceled error, got %v", err)
}
}()
// make sure the read started before canceling the reader
<-startedCh
cr.Cancel()
unblockCh <- true
// wait for the read to end to ensure its assertions were made
<-doneCh
// make sure that it waited for the reader
if !r.read {
t.Error("seems like the reader was canceled before the read, this shouldn't happen")
}
}
func TestFallbackReader(t *testing.T) {
var r bytes.Buffer
cr, err := newFallbackCancelReader(&r)
if err != nil {
t.Errorf("expected no error, but got %s", err)
}
txt := "first"
_, _ = r.WriteString(txt)
first, err := ioutil.ReadAll(cr)
if err != nil {
t.Errorf("expected no error, but got %s", err)
}
if string(first) != txt {
t.Errorf("expected output to be %q, got %q", txt, string(first))
}
cr.Cancel()
second, err := ioutil.ReadAll(cr)
if err != ErrCanceled {
t.Errorf("expected ErrCanceled, got %v", err)
}
if len(second) > 0 {
t.Errorf("expected an empty read, got %q", string(second))
}
}