-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathep_test.go
363 lines (315 loc) · 8.26 KB
/
ep_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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package ep_test
import (
"context"
"fmt"
"github.com/panoplyio/ep"
"strings"
"sync"
"time"
)
var _ = ep.Runners.
Register("waitForCancel", &waitForCancel{}).
Register("fixedData", &fixedData{}).
Register("dataRunner", &dataRunner{}).
Register("nodeAddr", &nodeAddr{}).
Register("count", &count{}).
Register("upper", &upper{}).
Register("question", &question{}).
Register("localSort", &localSort{})
const batchSize = 3
// waitForCancel infinitely emits data until it's canceled
type waitForCancel struct {
isRunningLock sync.Mutex
// isRunning flag helps tests ensure that the go-routine didn't leak
isRunning bool
// Name is unused field, defined to allow gob-ing infinityRunner between peers
Name string
}
func (*waitForCancel) Equals(other interface{}) bool {
_, ok := other.(*waitForCancel)
return ok
}
func (*waitForCancel) Returns() []ep.Type { return []ep.Type{str} }
func (r *waitForCancel) IsRunning() bool {
r.isRunningLock.Lock()
defer r.isRunningLock.Unlock()
return r.isRunning
}
func (r *waitForCancel) Run(ctx context.Context, inp, out chan ep.Dataset) error {
ticker := time.NewTicker(100 * time.Millisecond)
r.isRunningLock.Lock()
r.isRunning = true
r.isRunningLock.Unlock()
defer func() {
r.isRunningLock.Lock()
r.isRunning = false
r.isRunningLock.Unlock()
}()
go func() {
for range inp {
} // drain input, waitForCancel doesn't depend in input
}()
// infinitely produce data, until canceled
for {
select {
case <-ctx.Done():
return nil
case <-ticker.C:
out <- ep.NewDataset(strs{"data"})
}
}
}
type fixedData struct {
ep.Dataset
}
func (f *fixedData) Equals(other interface{}) bool {
r, ok := other.(*fixedData)
return ok && f.Dataset.Equal(r.Dataset)
}
func (r *fixedData) Returns() []ep.Type {
var types []ep.Type
for i := 0; i < r.Dataset.Len(); i++ {
types = append(types, r.Dataset.At(i).Type())
}
return types
}
func (r *fixedData) Run(ctx context.Context, _, out chan ep.Dataset) (err error) {
out <- r.Dataset
return nil
}
type dataRunner struct {
// ThrowOnData is a condition for throwing error. in case the last column
// contains exactly this string in first row - fail with error
ThrowOnData string
ThrowIgnorable bool
}
func (d *dataRunner) Equals(other interface{}) bool {
r, ok := other.(*dataRunner)
return ok && d.ThrowOnData == r.ThrowOnData && d.ThrowIgnorable == r.ThrowIgnorable
}
func (r *dataRunner) Returns() []ep.Type { return []ep.Type{ep.Wildcard} }
func (r *dataRunner) Run(ctx context.Context, inp, out chan ep.Dataset) (err error) {
if r.ThrowIgnorable {
err = ep.ErrIgnorable
} else {
err = fmt.Errorf("error %s", r.ThrowOnData)
}
for data := range inp {
out <- data
if r.ThrowOnData == data.At(data.Width() - 1).Strings()[0] {
return err
}
}
return nil
}
type nodeAddr struct{}
func (*nodeAddr) Equals(other interface{}) bool {
_, ok := other.(*nodeAddr)
return ok
}
func (*nodeAddr) Returns() []ep.Type { return []ep.Type{ep.Wildcard, str} }
func (*nodeAddr) Run(ctx context.Context, inp, out chan ep.Dataset) error {
addr := ep.NodeAddress(ctx)
for data := range inp {
res := make(strs, data.Len())
for i := range res {
res[i] = addr
}
var outset []ep.Data
for i := 0; i < data.Width(); i++ {
outset = append(outset, data.At(i))
}
outset = append(outset, res)
out <- ep.NewDataset(outset...)
}
return nil
}
type count struct{}
func (*count) Equals(other interface{}) bool {
_, ok := other.(*count)
return ok
}
func (*count) Returns() []ep.Type { return []ep.Type{str} }
func (*count) Run(_ context.Context, inp, out chan ep.Dataset) error {
for data := range inp {
out <- ep.NewDataset(strs{fmt.Sprintf("%d", data.Len())})
}
return nil
}
type upper struct{}
func (*upper) Equals(other interface{}) bool {
_, ok := other.(*upper)
return ok
}
func (*upper) Returns() []ep.Type { return []ep.Type{ep.SetAlias(str, "upper")} }
func (*upper) Run(_ context.Context, inp, out chan ep.Dataset) error {
for data := range inp {
res := make(strs, data.Len())
for i, v := range data.At(0).(strs) {
res[i] = strings.ToUpper(v)
}
out <- ep.NewDataset(res)
}
return nil
}
func (*upper) Scopes() ep.StringsSet {
return ep.StringsSet{"upper_scope": struct{}{}}
}
type question struct {
// called flag helps tests ensure runner was/wasn't called
called bool
}
func (*question) Equals(other interface{}) bool {
_, ok := other.(*question)
return ok
}
func (*question) Returns() []ep.Type { return []ep.Type{ep.SetAlias(str, "question")} }
func (q *question) Run(_ context.Context, inp, out chan ep.Dataset) error {
q.called = true
for data := range inp {
res := make(strs, data.Len())
for i, v := range data.At(0).(strs) {
res[i] = "is " + v + "?"
}
out <- ep.NewDataset(res)
}
return nil
}
type addInts struct{}
func (*addInts) Equals(other interface{}) bool {
_, ok := other.(*addInts)
return ok
}
func (*addInts) Returns() []ep.Type { return []ep.Type{integer} }
func (*addInts) BatchFunction() ep.BatchFunction {
return func(data ep.Dataset) (ep.Dataset, error) {
d0 := data.At(0).(integers)
d1 := data.At(1).(integers)
res := make(integers, data.Len())
for i := range res {
res[i] = d0[i] + d1[i]
}
return ep.NewDataset(res), nil
}
}
type negateInt struct{}
func (*negateInt) Equals(other interface{}) bool {
_, ok := other.(*negateInt)
return ok
}
func (*negateInt) Returns() []ep.Type { return []ep.Type{integer} }
func (*negateInt) BatchFunction() ep.BatchFunction {
return func(data ep.Dataset) (ep.Dataset, error) {
d0 := data.At(0).(integers)
res := make(integers, data.Len())
for i := range res {
res[i] = -1 * d0[i]
}
return ep.NewDataset(res), nil
}
}
type mulIntBy2 struct{}
func (*mulIntBy2) Equals(other interface{}) bool {
_, ok := other.(*mulIntBy2)
return ok
}
func (*mulIntBy2) Returns() []ep.Type { return []ep.Type{integer} }
func (*mulIntBy2) BatchFunction() ep.BatchFunction {
return func(data ep.Dataset) (ep.Dataset, error) {
d0 := data.At(0).(integers)
res := make(integers, data.Len())
for i := range res {
res[i] = d0[i] * 2
}
return ep.NewDataset(res), nil
}
}
type localSort struct {
SortingCols []ep.SortingCol
}
func (l *localSort) Equals(other interface{}) bool {
r, ok := other.(*localSort)
if !ok || len(l.SortingCols) != len(r.SortingCols) {
return false
}
for i, col := range l.SortingCols {
if !col.Equals(r.SortingCols[i]) {
return false
}
}
return true
}
func (*localSort) Returns() []ep.Type { return []ep.Type{ep.Wildcard, str} }
func (ls *localSort) Run(ctx context.Context, inp, out chan ep.Dataset) error {
builder := ep.NewDatasetBuilder()
hasData := false
// consume entire local input before sorting all together
for data := range inp {
builder.Append(data)
hasData = true
}
var selfData ep.Data
if hasData {
selfData = builder.Data()
}
size := selfData.Len()
if size > 0 {
selfDataset := selfData.(ep.Dataset)
ep.Sort(selfDataset, ls.SortingCols)
// once sorted, split to batches again to stream batches to distributedOrder
i := 0
for ; i < size/batchSize; i++ {
out <- selfData.Slice(i*batchSize, (i+1)*batchSize).(ep.Dataset)
}
if i*batchSize < size {
out <- selfData.Slice(i*batchSize, size).(ep.Dataset)
}
}
return nil
}
type runOther struct {
runner ep.Runner
}
func (r *runOther) Equals(other interface{}) bool {
o, ok := other.(*runOther)
return ok && r.runner.Equals(o.runner)
}
func (r *runOther) Returns() []ep.Type { return r.runner.Returns() }
func (r *runOther) Run(ctx context.Context, inp, out chan ep.Dataset) error {
innerInp := make(chan ep.Dataset)
innerOut := make(chan ep.Dataset)
var err error
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
defer close(innerOut)
// running without ep.Run so the inp won't be draining
err = r.runner.Run(ctx, innerInp, innerOut)
}()
wg.Add(1)
go func() {
defer wg.Done()
for data := range inp {
innerInp <- data
}
close(innerInp)
}()
for data := range innerOut {
out <- data
}
wg.Wait()
return err
}
type runnerWithSize struct {
ep.Runner
size int
}
func (r *runnerWithSize) Equals(other interface{}) bool {
o, ok := other.(*runnerWithSize)
return ok && r.size == o.size && r.Runner.Equals(o.Runner)
}
func (r *runnerWithSize) Returns() []ep.Type { return []ep.Type{ep.Wildcard} }
func (r *runnerWithSize) ApproxSize() int {
return r.size
}