From c52e3ed37bb0be236841c0e2415dfaeeb84bb0c0 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 20 Apr 2022 15:01:17 -0400 Subject: [PATCH] test fixture race --- internal/command/views/hook_json_test.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/internal/command/views/hook_json_test.go b/internal/command/views/hook_json_test.go index bde9863d06..cb1cbc920b 100644 --- a/internal/command/views/hook_json_test.go +++ b/internal/command/views/hook_json_test.go @@ -2,6 +2,7 @@ package views import ( "fmt" + "sync" "testing" "time" @@ -18,8 +19,14 @@ func TestJSONHook_create(t *testing.T) { streams, done := terminal.StreamsForTesting(t) hook := newJSONHook(NewJSONView(NewView(streams))) + var nowMu sync.Mutex now := time.Now() - hook.timeNow = func() time.Time { return now } + hook.timeNow = func() time.Time { + nowMu.Lock() + defer nowMu.Unlock() + return now + } + after := make(chan time.Time, 1) hook.timeAfter = func(time.Duration) <-chan time.Time { return after } @@ -52,18 +59,24 @@ func TestJSONHook_create(t *testing.T) { // Travel 10s into the future, notify the progress goroutine, and sleep // briefly to allow it to execute + nowMu.Lock() now = now.Add(10 * time.Second) after <- now + nowMu.Unlock() time.Sleep(1 * time.Millisecond) // Travel 10s into the future, notify the progress goroutine, and sleep // briefly to allow it to execute + nowMu.Lock() now = now.Add(10 * time.Second) after <- now + nowMu.Unlock() time.Sleep(1 * time.Millisecond) // Travel 2s into the future. We have arrived! + nowMu.Lock() now = now.Add(2 * time.Second) + nowMu.Unlock() action, err = hook.PostApply(addr, states.CurrentGen, plannedNewState, nil) testHookReturnValues(t, action, err)