-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker_test.go
189 lines (145 loc) · 3.52 KB
/
worker_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package conveyor_test
import (
"errors"
"sync"
"testing"
"github.com/fgehrlicher/conveyor"
"github.com/stretchr/testify/assert"
)
func GetSingleChunkChan(file string, size int, out conveyor.ChunkWriter) chan conveyor.Chunk {
tasks := make(chan conveyor.Chunk, 1)
tasks <- conveyor.Chunk{
Id: 1,
In: &conveyor.FileReader{FilePath: file},
Size: size,
Out: out,
}
close(tasks)
return tasks
}
func TestWorkerReturnsErrorFromLineProcessor(t *testing.T) {
assertion := assert.New(t)
expectedErr := errors.New("test error")
chunkSize := 8000
results := make(chan conveyor.ChunkResult, 1)
wg := &sync.WaitGroup{}
wg.Add(1)
conveyor.NewWorker(
1,
GetSingleChunkChan("testdata/data.txt", chunkSize, nil),
results,
conveyor.LineProcessorFunc(func(i []byte, metadata conveyor.LineMetadata) ([]byte, error) {
return i, expectedErr
}),
int64(chunkSize),
1024,
wg,
).Work()
close(results)
var result conveyor.ChunkResult
for item := range results {
result = item
}
assertion.ErrorIs(result.Err, expectedErr)
}
func TestWorkerReturnsErrorFromLastLineProcessed(t *testing.T) {
assertion := assert.New(t)
expectedErr := errors.New("test error")
chunkSize := 6900
results := make(chan conveyor.ChunkResult, 1)
wg := &sync.WaitGroup{}
wg.Add(1)
errLine := 100
conveyor.NewWorker(
1,
GetSingleChunkChan("testdata/data.txt", chunkSize, nil),
results,
conveyor.LineProcessorFunc(func(i []byte, metadata conveyor.LineMetadata) ([]byte, error) {
if metadata.Line == errLine {
return nil, expectedErr
}
return i, nil
}),
int64(chunkSize),
1024,
wg,
).Work()
close(results)
var result conveyor.ChunkResult
for item := range results {
result = item
}
assertion.ErrorIs(result.Err, expectedErr)
}
func TestWorkerFailsForInvalidOutputHandle(t *testing.T) {
assertion := assert.New(t)
tWriter := &TestWriter{FailAt: 0}
chunkSize := 8000
tasks := GetSingleChunkChan("testdata/data.txt", chunkSize, tWriter)
results := make(chan conveyor.ChunkResult, 1)
wg := &sync.WaitGroup{}
wg.Add(1)
conveyor.NewWorker(1, tasks, results, NullLineProcessor, int64(chunkSize), 1024, wg).Work()
close(results)
wg.Wait()
for result := range results {
assertion.False(result.Ok())
assertion.ErrorIs(result.Err, ErrInvalidWrite)
}
}
func TestWorkerFailsForReadAfterSeek(t *testing.T) {
assertion := assert.New(t)
tWriter := &TestWriter{FailAt: 0}
chunkSize := 8000
tasks := make(chan conveyor.Chunk, 1)
tasks <- conveyor.Chunk{
Id: 1,
In: &FailureFileReader{Filename: "Test"},
Size: chunkSize,
Out: tWriter,
}
close(tasks)
results := make(chan conveyor.ChunkResult, 1)
wg := &sync.WaitGroup{}
wg.Add(1)
conveyor.NewWorker(1, tasks, results, NullLineProcessor, int64(chunkSize), 1024, wg).Work()
close(results)
wg.Wait()
for result := range results {
assertion.False(result.Ok())
assertion.ErrorIs(result.Err, ErrInvalidRead)
}
}
func TestWorkerFailsOverflowBuff(t *testing.T) {
assertion := assert.New(t)
chunkSize := 6900
results := make(chan conveyor.ChunkResult, 1)
wg := &sync.WaitGroup{}
wg.Add(1)
tasks := make(chan conveyor.Chunk, 1)
tasks <- conveyor.Chunk{
Id: 1,
In: &FailureAtReader{
Filename: "testdata/data.txt",
FailAt: 1,
},
Size: chunkSize,
Out: nil,
}
close(tasks)
conveyor.NewWorker(
1,
tasks,
results,
NullLineProcessor,
int64(chunkSize),
1024,
wg,
).Work()
close(results)
wg.Wait()
for result := range results {
assertion.False(result.Ok())
assertion.ErrorIs(result.Err, ErrInvalidRead)
}
}