|
4 | 4 | "context" |
5 | 5 | "errors" |
6 | 6 | "fmt" |
| 7 | + "sync/atomic" |
7 | 8 | "testing" |
| 9 | + "time" |
8 | 10 |
|
9 | 11 | "github.com/ghosind/go-assert" |
10 | 12 | "github.com/ghosind/go-async" |
@@ -98,3 +100,97 @@ func ExampleSeq() { |
98 | 100 | // [2] |
99 | 101 | // <nil> |
100 | 102 | } |
| 103 | + |
| 104 | +func TestSeqGroups(t *testing.T) { |
| 105 | + a := assert.New(t) |
| 106 | + cnts := make([]atomic.Int32, 3) |
| 107 | + groups := make([][]async.AsyncFn, 0, 3) |
| 108 | + expectedCnts := []int{2, 3, 4} |
| 109 | + |
| 110 | + for i := 0; i < 3; i++ { |
| 111 | + tasks := make([]async.AsyncFn, 0) |
| 112 | + idx := i |
| 113 | + for j := 0; j < i+2; j++ { |
| 114 | + tasks = append(tasks, func() { |
| 115 | + cnts[idx].Add(1) |
| 116 | + }) |
| 117 | + } |
| 118 | + groups = append(groups, tasks) |
| 119 | + } |
| 120 | + |
| 121 | + err := async.SeqGroups(groups...) |
| 122 | + a.NilNow(err) |
| 123 | + for i := 0; i < 3; i++ { |
| 124 | + a.EqualNow(cnts[i].Load(), expectedCnts[i]) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func TestSeqGroupsWithoutTasks(t *testing.T) { |
| 129 | + a := assert.New(t) |
| 130 | + |
| 131 | + err := async.SeqGroups() |
| 132 | + a.NilNow(err) |
| 133 | +} |
| 134 | + |
| 135 | +func TestSeqGroupsWithFailure(t *testing.T) { |
| 136 | + a := assert.New(t) |
| 137 | + cnts := make([]atomic.Int32, 3) |
| 138 | + groups := make([][]async.AsyncFn, 0, 3) |
| 139 | + expectedErr := errors.New("expected error") |
| 140 | + expectedCnts := []int{2, 0, 0} |
| 141 | + |
| 142 | + for i := 0; i < 3; i++ { |
| 143 | + tasks := make([]async.AsyncFn, 0) |
| 144 | + idx := i |
| 145 | + for j := 0; j < i+2; j++ { |
| 146 | + tasks = append(tasks, func() error { |
| 147 | + v := cnts[idx].Add(1) |
| 148 | + |
| 149 | + if idx == 1 && v == 2 { |
| 150 | + return expectedErr |
| 151 | + } |
| 152 | + |
| 153 | + return nil |
| 154 | + }) |
| 155 | + } |
| 156 | + groups = append(groups, tasks) |
| 157 | + } |
| 158 | + |
| 159 | + err := async.SeqGroups(groups...) |
| 160 | + a.NotNilNow(err) |
| 161 | + a.ContainsStringNow(err.Error(), expectedErr.Error()) |
| 162 | + |
| 163 | + for i := 0; i < 3; i++ { |
| 164 | + if i == 1 { |
| 165 | + continue |
| 166 | + } |
| 167 | + a.EqualNow(cnts[i].Load(), expectedCnts[i]) |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +func TestSeqGroupsWithContext(t *testing.T) { |
| 172 | + a := assert.New(t) |
| 173 | + cnts := make([]atomic.Int32, 3) |
| 174 | + groups := make([][]async.AsyncFn, 0, 3) |
| 175 | + expectedCnts := []int{2, 3, 4} |
| 176 | + |
| 177 | + for i := 0; i < 3; i++ { |
| 178 | + tasks := make([]async.AsyncFn, 0) |
| 179 | + idx := i |
| 180 | + for j := 0; j < i+2; j++ { |
| 181 | + tasks = append(tasks, func() { |
| 182 | + cnts[idx].Add(1) |
| 183 | + }) |
| 184 | + } |
| 185 | + groups = append(groups, tasks) |
| 186 | + } |
| 187 | + |
| 188 | + ctx, canFunc := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 189 | + defer canFunc() |
| 190 | + |
| 191 | + err := async.SeqGroupsWithContext(ctx, groups...) |
| 192 | + a.NilNow(err) |
| 193 | + for i := 0; i < 3; i++ { |
| 194 | + a.EqualNow(cnts[i].Load(), expectedCnts[i]) |
| 195 | + } |
| 196 | +} |
0 commit comments