diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-12-02 16:48:56 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-12-10 14:48:29 +0300 |
commit | 74ddbae836a41a22cb93206adab90f93a144295b (patch) | |
tree | c630e2ef7367612221c39564bd3fb6f8374868d4 | |
parent | 3941d0593cd85af75d3236e835129a5172efb130 (diff) |
command: Use explicit context cancellation instead of sleep
The test which verifies that a command gets killed when the context is
cancelled is currently using a timeout for this: it spawns a sleep which
waits for three seconds, but the timeout is fired after one second.
While this hasn't caused any flakes to the best of my knowledge, it is
still unnecessarily flaky and wastes some time.
Refactor the test to instead sleep for one hour and use manual context
cancellation. Like this, there is no need for timeouts in the test given
that the test would be killed after 10 minutes anyway, and the test is
quicker to finish.
-rw-r--r-- | internal/command/command_test.go | 27 |
1 files changed, 10 insertions, 17 deletions
diff --git a/internal/command/command_test.go b/internal/command/command_test.go index 0421b5fd0..d3d71f1a2 100644 --- a/internal/command/command_test.go +++ b/internal/command/command_test.go @@ -209,28 +209,21 @@ wait: require.Contains(t, err.Error(), "process spawn timed out after") } -func TestCommand_Wait_interrupts_after_context_timeout(t *testing.T) { +func TestCommand_Wait_interrupts_after_context_cancellation(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - ctx, timeout := context.WithTimeout(ctx, time.Second) - defer timeout() - - cmd, err := New(ctx, exec.CommandContext(ctx, "sleep", "3"), nil, nil, nil) + cmd, err := New(ctx, exec.CommandContext(ctx, "sleep", "1h"), nil, nil, nil) require.NoError(t, err) - completed := make(chan error, 1) - go func() { completed <- cmd.Wait() }() - - select { - case err := <-completed: - require.Error(t, err) - s, ok := ExitStatus(err) - require.True(t, ok) - require.Equal(t, -1, s) - case <-time.After(2 * time.Second): - require.FailNow(t, "process is running too long") - } + // Cancel the command early. + go cancel() + + err = cmd.Wait() + require.Error(t, err) + s, ok := ExitStatus(err) + require.True(t, ok) + require.Equal(t, -1, s) } func TestNewCommandWithSetupStdin(t *testing.T) { |