diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-07-20 10:24:19 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-07-20 15:32:04 +0300 |
commit | a5cec2a85888d323519cabbd56feac3e452f8ab9 (patch) | |
tree | 556cdd9670e8c88b0f51eaaba48d57e87b160761 | |
parent | 893137c05e065ab96edd21af1a5ad8f6745b4a08 (diff) |
datastore: Fix unsynced Goroutine causing racy test case
One of the datastore tests verifies that if we trigger the health
updater twice, that the job lock's timestamp got updated. The way we
run the health updater is racy, though, as we do not verify that it
actually exits before the test case finishes. As a result, it may
execute queries against the database while the follwing test truncates
all tables already.
Let's fix this race by synchronizing shutdown of the Goroutine.
-rw-r--r-- | internal/praefect/datastore/queue_test.go | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/internal/praefect/datastore/queue_test.go b/internal/praefect/datastore/queue_test.go index c8be612e8..3ea4de70d 100644 --- a/internal/praefect/datastore/queue_test.go +++ b/internal/praefect/datastore/queue_test.go @@ -5,6 +5,7 @@ package datastore import ( "context" "testing" + "sync" "time" "github.com/stretchr/testify/assert" @@ -927,8 +928,12 @@ func TestPostgresReplicationEventQueue_StartHealthUpdate(t *testing.T) { t.Run("triggers all passed in events", func(t *testing.T) { db.TruncateAll(t) + var wg sync.WaitGroup ctx, cancel := testhelper.Context() - defer cancel() + defer func() { + cancel() + wg.Wait() + }() queue := PostgresReplicationEventQueue{qc: db} events := []ReplicationEvent{eventType1, eventType2, eventType3, eventType4} @@ -950,11 +955,13 @@ func TestPostgresReplicationEventQueue_StartHealthUpdate(t *testing.T) { initialJobLocks := fetchJobLocks(t, ctx, db) trigger := make(chan time.Time, 1) + wg.Add(1) go func() { - trigger <- time.Time{} + defer wg.Done() assert.NoError(t, queue.StartHealthUpdate(ctx, trigger, dequeuedEventsToTrigger)) }() + trigger <- time.Time{} time.Sleep(time.Millisecond) // we should sleep as the processing is too fast and won't give different time trigger <- time.Time{} // once this consumed we are sure that the previous update has been executed |