Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix TestTaskError and TestMultiError flaky tests (#2779) #2820

Merged
Merged
25 changes: 16 additions & 9 deletions pkg/workerpool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ var _ = check.Suite(&workerPoolSuite{})
func (s *workerPoolSuite) TestTaskError(c *check.C) {
defer testleak.AfterTest(c)()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

pool := newDefaultPoolImpl(&defaultHasher{}, 4)
errg, ctx := errgroup.WithContext(ctx)
Expand All @@ -55,23 +54,27 @@ func (s *workerPoolSuite) TestTaskError(c *check.C) {
c.Assert(err, check.ErrorMatches, "test error")
})

errg.Go(func() error {
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < 10; i++ {
err := handle.AddEvent(ctx, i)
if err != nil {
c.Assert(err, check.ErrorMatches, ".*ErrWorkerPoolHandleCancelled.*")
return nil
}
}
return nil
})
}()

select {
case <-ctx.Done():
c.FailNow()
case err := <-handle.ErrCh():
c.Assert(err, check.ErrorMatches, "test error")
}
// Only cancel the context after all events have been sent,
// otherwise the event delivery may fail due to context cancellation.
wg.Wait()
cancel()

err := errg.Wait()
Expand Down Expand Up @@ -115,7 +118,6 @@ func (s *workerPoolSuite) TestTimerError(c *check.C) {
func (s *workerPoolSuite) TestMultiError(c *check.C) {
defer testleak.AfterTest(c)()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

pool := newDefaultPoolImpl(&defaultHasher{}, 4)
errg, ctx := errgroup.WithContext(ctx)
Expand All @@ -130,22 +132,27 @@ func (s *workerPoolSuite) TestMultiError(c *check.C) {
return nil
})

errg.Go(func() error {
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < 10; i++ {
err := handle.AddEvent(ctx, i)
if err != nil {
c.Assert(err, check.ErrorMatches, ".*ErrWorkerPoolHandleCancelled.*")
}
}
return nil
})
}()

select {
case <-ctx.Done():
c.FailNow()
case err := <-handle.ErrCh():
c.Assert(err, check.ErrorMatches, "test error")
}
// Only cancel the context after all events have been sent,
// otherwise the event delivery may fail due to context cancellation.
wg.Wait()
cancel()

err := errg.Wait()
Expand Down