Return Context error if it is canceled before at least 1 task failed

This commit is contained in:
alitto
2023-01-31 15:25:28 -03:00
parent fbc6f5dc97
commit e4395b22ac
2 changed files with 52 additions and 16 deletions
+31
View File
@@ -120,3 +120,34 @@ func TestGroupContextWithNilContext(t *testing.T) {
assertEqual(t, "a non-nil context needs to be specified when using GroupContext", thrownPanic)
}
func TestGroupContextWithCanceledContext(t *testing.T) {
pool := pond.New(3, 100)
assertEqual(t, 0, pool.RunningWorkers())
// Submit a group of tasks
var doneCount, startedCount int32
userCtx, cancel := context.WithCancel(context.Background())
group, ctx := pool.GroupContext(userCtx)
for i := 0; i < 10; i++ {
group.Submit(func() error {
atomic.AddInt32(&startedCount, 1)
select {
case <-time.After(10 * time.Millisecond):
atomic.AddInt32(&doneCount, 1)
case <-ctx.Done():
}
return nil
})
}
// Cancel context right after submitting tasks
cancel()
err := group.Wait()
assertEqual(t, context.Canceled, err)
assertEqual(t, int32(0), atomic.LoadInt32(&doneCount))
}