-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_test.go
152 lines (120 loc) · 3.75 KB
/
group_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
package resultgroup
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var (
err1 = errors.New("Error 1")
err2 = errors.New("Error 2")
err3 = errors.New("Error 3")
)
func TestGroup(t *testing.T) {
t.Parallel()
t.Run("no errors", testGroupNoErrors)
t.Run("with errors", testGroupWithErrors)
t.Run("max errors reached", testGroupMaxErrorsReached)
t.Run("no error limit", testGroupNoErrorLimit)
}
// testGroupNoErrors checks if the Group works correctly when there are no errors.
func testGroupNoErrors(t *testing.T) {
t.Parallel()
group, _ := WithErrorsThreshold[int](context.Background(), 3)
for i := 0; i < 5; i++ {
i := i
group.Go(func() ([]int, error) {
time.Sleep(10 * time.Millisecond)
return []int{i}, nil
})
}
results, err := group.Wait()
assert.Nil(t, err, "Expected no error, got: %v", err)
assert.Len(t, results, 5, "Expected 5 results, got: %d", len(results))
}
// testGroupWithErrors checks if the Group works correctly when some tasks return errors.
func testGroupWithErrors(t *testing.T) {
t.Parallel()
group, _ := WithErrorsThreshold[int](context.Background(), 3)
group.Go(func() ([]int, error) {
return nil, err1
})
group.Go(func() ([]int, error) {
return []int{1}, nil
})
group.Go(func() ([]int, error) {
return nil, err2
})
group.Go(func() ([]int, error) {
return []int{2}, nil
})
results, err := group.Wait()
assert.NotNil(t, err, "Expected an error, got nil")
assert.Len(t, err.Unwrap(), 2, "Expected 2 errors, got: %d", len(err.Unwrap()))
assert.True(t, errors.Is(err, err1), "Expected error to be: %v, got: %v", err1, err)
assert.True(t, errors.Is(err, err2), "Expected error to be: %v, got: %v", err2, err)
assert.Len(t, results, 2, "Expected 2 results, got: %d", len(results))
}
// testGroupMaxErrorsReached checks if the Group cancels the context and stops processing when the error threshold is reached.
func testGroupMaxErrorsReached(t *testing.T) {
t.Parallel()
group, ctx := WithErrorsThreshold[int](context.Background(), 2)
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
group.Go(func() ([]int, error) {
time.Sleep(10 * time.Millisecond)
return nil, err1
})
group.Go(func() ([]int, error) {
time.Sleep(10 * time.Millisecond)
return []int{2}, nil
})
group.Go(func() ([]int, error) {
time.Sleep(11 * time.Millisecond)
return nil, err2
})
group.Go(func() ([]int, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(12 * time.Millisecond):
return []int{4}, err3
}
})
group.Go(func() ([]int, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(12 * time.Millisecond):
return []int{1}, nil
}
})
results, err := group.Wait()
assert.NotNil(t, err, "Expected an error, got nil")
assert.Len(t, err.Unwrap(), 2, "Expected 2 errors, got: %d", len(err.Unwrap()))
assert.True(t, errors.Is(err, err1), "Expected error to be: %v, got: %v", err1, err)
assert.True(t, errors.Is(err, err2), "Expected error to be: %v, got: %v", err2, err)
assert.Len(t, results, 1, "Expected 1 result, got: %d", len(results))
assert.Equal(t, results, []int{2}, "Expected result to be: %v, got: %v", []int{2}, results[0])
}
// testGroupNoErrorLimit checks if the Group works correctly without setting an error threshold.
func testGroupNoErrorLimit(t *testing.T) {
t.Parallel()
group := Group[int]{}
group.Go(func() ([]int, error) {
return nil, err1
})
group.Go(func() ([]int, error) {
return []int{1}, nil
})
group.Go(func() ([]int, error) {
return nil, err2
})
group.Go(func() ([]int, error) {
return []int{2}, nil
})
results, err := group.Wait()
assert.NotNil(t, err, "Expected an error, got nil")
assert.Len(t, results, 2, "Expected 2 results, got: %d", len(results))
}