fix: test flakiness from nondeterministic waiting (#1337)

Signed-off-by: Brian Vander Schaaf <6099457+bvand@users.noreply.github.com>
This commit is contained in:
Brian Vander Schaaf
2024-03-06 16:25:14 -05:00
committed by GitHub
parent 5e239012a1
commit 783100123c
3 changed files with 33 additions and 10 deletions

View File

@@ -11,6 +11,8 @@ import (
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/opentofu/opentofu/internal/addrs"
"github.com/opentofu/opentofu/internal/plans"
"github.com/opentofu/opentofu/internal/states"
@@ -62,21 +64,25 @@ func TestJSONHook_create(t *testing.T) {
action, err = hook.PostProvisionInstanceStep(addr, "local-exec", nil)
testHookReturnValues(t, action, err)
// 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)
elapsedChan := hook.applying[addr.String()].elapsed
// Travel 10s into the future, notify the progress goroutine, and sleep
// briefly to allow it to execute
// Travel 10s into the future, notify the progress goroutine, and wait
// for execution via 'elapsed' progress
nowMu.Lock()
now = now.Add(10 * time.Second)
after <- now
nowMu.Unlock()
time.Sleep(1 * time.Millisecond)
elapsed := <-elapsedChan
testDurationEqual(t, 10*time.Second, elapsed)
// Travel 10s into the future, notify the progress goroutine, and wait
// for execution via 'elapsed' progress
nowMu.Lock()
now = now.Add(10 * time.Second)
after <- now
nowMu.Unlock()
elapsed = <-elapsedChan
testDurationEqual(t, 20*time.Second, elapsed)
// Travel 2s into the future. We have arrived!
nowMu.Lock()
@@ -90,6 +96,7 @@ func TestJSONHook_create(t *testing.T) {
hook.applyingLock.Lock()
for key, progress := range hook.applying {
close(progress.done)
close(progress.elapsed)
<-progress.heartbeatDone
delete(hook.applying, key)
}
@@ -221,6 +228,7 @@ func TestJSONHook_errors(t *testing.T) {
hook.applyingLock.Lock()
for key, progress := range hook.applying {
close(progress.done)
close(progress.elapsed)
<-progress.heartbeatDone
delete(hook.applying, key)
}
@@ -341,3 +349,11 @@ func testHookReturnValues(t *testing.T, action tofu.HookAction, err error) {
t.Fatalf("Expected hook to continue, given: %#v", action)
}
}
func testDurationEqual(t *testing.T, wantedDuration time.Duration, gotDuration time.Duration) {
t.Helper()
if !cmp.Equal(wantedDuration, gotDuration) {
t.Errorf("unexpected time elapsed:%s\n", cmp.Diff(wantedDuration, gotDuration))
}
}