-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_input_test.go
87 lines (80 loc) · 1.73 KB
/
file_input_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
package main
import (
"fmt"
"io"
"testing"
)
// Test stdin line reader
type scanLnMsg struct {
path string
err error
}
// implements `io.Reader` interface
type testReader struct {
scanLnChIn chan scanLnMsg
scanLnChOut chan scanLnMsg
scanLnChErr chan error
}
func (tb *testReader) Read(p []byte) (n int, err error) {
msg := <-tb.scanLnChIn
if msg.err != nil {
return 0, err
}
n = copy(p, msg.path)
return
}
func testBufferSetup(items []scanLnMsg) *testReader {
tb := testReader{
scanLnChIn: make(chan scanLnMsg, 5),
scanLnChOut: make(chan scanLnMsg, 5),
scanLnChErr: make(chan error, 5),
}
for _, testItem := range items {
tb.scanLnChIn <- scanLnMsg{
path: testItem.path + "\n",
err: nil,
}
tb.scanLnChOut <- testItem
}
tb.scanLnChIn <- scanLnMsg{
path: "",
err: io.EOF,
}
close(tb.scanLnChIn)
return &tb
}
func (tb *testReader) testHandler(path string) (err error) {
msg := <-tb.scanLnChOut
if msg.err != nil {
return err
}
if msg.path != path {
err = fmt.Errorf("got %s, expected %s", path, msg.path)
tb.scanLnChErr <- err
}
return err
}
func TestReadFileList(t *testing.T) {
tb := testBufferSetup([]scanLnMsg{
{"path1", nil},
{"path 2", nil},
{"path3", fmt.Errorf("handled error")}, // should continue despite this error being injected
{"path/4", nil},
})
mc := InitMassCRC32C(1, 1)
mc.HandlerFunc = tb.testHandler
mc.stdin = tb
fi := FileInput{mc: mc}
mc.Startup(1)
fi.ReadFileList()
mc.TearDown()
if len(tb.scanLnChIn) > 0 {
t.Errorf("input queue isn't empty: %d remaining", len(tb.scanLnChIn))
}
if len(tb.scanLnChOut) > 0 {
t.Errorf("out queue isn't empty: %d remaining", len(tb.scanLnChOut))
}
if len(tb.scanLnChErr) > 0 {
t.Errorf("%v\n", <-tb.scanLnChErr)
}
}