diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-10-28 11:56:17 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-10-29 09:55:53 +0300 |
commit | 8820e003e977e84556e6eb863970eb30e25feee4 (patch) | |
tree | 2ddeb68104aec27f34f103fd1cec8d759084d582 | |
parent | 6aabab39e06f8a4f786a99449a49d5f0cb332310 (diff) |
datastore: Plug race by waiting for Goroutine to exit
The `listenNotify` function is spawning a Goroutine which has a timeout
at its end, but we don't actually wait for that Goroutine to exit. Let's
do so by using another waitgroup.
-rw-r--r-- | internal/praefect/datastore/listener_postgres_test.go | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/internal/praefect/datastore/listener_postgres_test.go b/internal/praefect/datastore/listener_postgres_test.go index e6529d415..ebeba3c54 100644 --- a/internal/praefect/datastore/listener_postgres_test.go +++ b/internal/praefect/datastore/listener_postgres_test.go @@ -115,29 +115,37 @@ func TestPostgresListener_Listen(t *testing.T) { require.NoError(t, err) + var wg sync.WaitGroup ctx, cancel := testhelper.Context() - defer cancel() + defer func() { + cancel() + wg.Wait() + }() numResults := len(payloads) * numNotifiers allReceivedChan := make(chan struct{}) + wg.Add(1) go func() { - defer cancel() + defer func() { + cancel() + wg.Done() + }() time.Sleep(100 * time.Millisecond) - var wg sync.WaitGroup - wg.Add(numNotifiers) + var notifyWG sync.WaitGroup + notifyWG.Add(numNotifiers) for i := 0; i < numNotifiers; i++ { go func() { - defer wg.Done() + defer notifyWG.Done() for _, payload := range payloads { notifyListener(t, opts.Channel, payload) } }() } - wg.Wait() + notifyWG.Wait() select { case <-time.After(time.Second): |