-
Notifications
You must be signed in to change notification settings - Fork 6
/
batch_test.go
67 lines (55 loc) · 1.42 KB
/
batch_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
// Copyright 2019 Grabtaxi Holdings PTE LTE (GRAB), All rights reserved.
// Use of this source code is governed by an MIT-style license that can be found in the LICENSE file
package async
import (
"context"
"fmt"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func TestBatch(t *testing.T) {
const taskCount = 10
var wg sync.WaitGroup
wg.Add(taskCount)
// reducer that multiplies items by 10 at once
r := NewBatch(context.Background(), func(input []interface{}) []interface{} {
result := make([]interface{}, len(input))
for i, number := range input {
result[i] = number.(int) * 10
}
return result
})
for i := 0; i < taskCount; i++ {
number := i
r.Append(number).ContinueWith(context.TODO(), func(result interface{}, err error) (interface{}, error) {
assert.Equal(t, result.(int), number*10)
assert.NoError(t, err)
wg.Done()
return nil, nil
})
}
assert.Equal(t, 10, r.Size())
r.Reduce()
wg.Wait()
}
func ExampleBatch() {
var wg sync.WaitGroup
wg.Add(2)
r := NewBatch(context.Background(), func(input []interface{}) []interface{} {
fmt.Println(input)
return input
})
r.Append(1).ContinueWith(context.TODO(), func(result interface{}, err error) (interface{}, error) {
wg.Done()
return nil, nil
})
r.Append(2).ContinueWith(context.TODO(), func(result interface{}, err error) (interface{}, error) {
wg.Done()
return nil, nil
})
r.Reduce()
wg.Wait()
// Output:
// [1 2]
}