From 9260b266d692659d775a988d6398b03c3f39883d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20R=C3=B3=C5=BCa=C5=84ski?= Date: Wed, 31 Jan 2024 17:34:59 +0000 Subject: [PATCH] Wait for workers to stop in TestPostVerifierScaling (#5525) ## Motivation Unit test `TestPostVerifierScaling` is flaky because the test doesn't wait for the workers to stop after scaling the pool down. --- activation/post_verifier.go | 6 ++++++ activation/post_verifier_scaling_test.go | 1 + 2 files changed, 7 insertions(+) diff --git a/activation/post_verifier.go b/activation/post_verifier.go index 58f8afd701..fe3ab1a2d0 100644 --- a/activation/post_verifier.go +++ b/activation/post_verifier.go @@ -78,6 +78,7 @@ type postVerifierWorker struct { prioritized <-chan *verifyPostJob jobs <-chan *verifyPostJob stop chan struct{} // signal to stop this worker + stopped chan struct{} // signal that this worker has stopped shutdown chan struct{} // signal that the verifier is closing } @@ -215,6 +216,7 @@ func (v *offloadingPostVerifier) scale(target int) { prioritized: v.prioritized, jobs: v.jobs, stop: make(chan struct{}), + stopped: make(chan struct{}), shutdown: v.stop, } v.workers = append(v.workers, w) @@ -227,6 +229,9 @@ func (v *offloadingPostVerifier) scale(target int) { for _, worker := range toStop { close(worker.stop) } + for _, worker := range toStop { + <-worker.stopped + } } } @@ -293,6 +298,7 @@ func (v *offloadingPostVerifier) Close() error { func (w *postVerifierWorker) start() { w.log.Info("starting") defer w.log.Info("stopped") + defer close(w.stopped) for { // First try to process a prioritized job. diff --git a/activation/post_verifier_scaling_test.go b/activation/post_verifier_scaling_test.go index 7fbb199319..5915f66f98 100644 --- a/activation/post_verifier_scaling_test.go +++ b/activation/post_verifier_scaling_test.go @@ -62,6 +62,7 @@ func TestPostVerifierScaling(t *testing.T) { require.NoError(t, err) v.scale(0) + ctx, cancel = context.WithTimeout(context.Background(), 10*time.Millisecond) defer cancel() err = v.Verify(ctx, &shared.Proof{}, &shared.ProofMetadata{})