diff --git a/internal/execution/worker/worker_test.go b/internal/execution/worker/worker_test.go index 7b3fca7..e2938a5 100644 --- a/internal/execution/worker/worker_test.go +++ b/internal/execution/worker/worker_test.go @@ -24,7 +24,13 @@ func TestWorker_Start_IsAlive(t *testing.T) { defer w.Kill() - assert.Equal(t, true, util.IsProcessAlive(w.Pid())) + pid := w.Pid() + require.NotZero(t, pid, "pid should be set after Start") + + require.Eventually(t, func() bool { + return util.IsProcessAlive(pid) + }, 2*time.Second, 10*time.Millisecond, "process never reported alive") + } func TestWorker_Start_FailsIfStarted(t *testing.T) { @@ -35,8 +41,14 @@ func TestWorker_Start_FailsIfStarted(t *testing.T) { defer w.Kill() - err = w.Start(context.Background()) - assert.Error(t, err) + require.Eventually(t, func() bool { + pid := w.Pid() + return pid != 0 && util.IsProcessAlive(pid) + }, 2*time.Second, 10*time.Millisecond, "worker never became alive") + + // Now a second Start should deterministically fail. + secondWorkerErr := w.Start(context.Background()) + require.Error(t, secondWorkerErr) } func TestWorker_Start_ReturnsErrorIfInvalidCommand(t *testing.T) { @@ -72,7 +84,7 @@ func TestWorker_TerminatesIfContextCancelled(t *testing.T) { require.Eventually(t, func() bool { evt, waitError = w.Wait(context.Background()) return waitError == nil && evt.Signal != nil - }, time.Second, 10*time.Millisecond) + }, 5*time.Second, 10*time.Millisecond) require.NoError(t, waitError) require.NotNil(t, evt) @@ -125,6 +137,7 @@ func TestWorker_Wait_ReturnsErrorIfContextCancelled(t *testing.T) { _, err = w.Wait(ctx) assert.Error(t, err) } + func TestWorker_Wait_ReturnsErrorIfCalledMultiple(t *testing.T) { w := worker.NewProcessWorker(context.Background(), worker.StartConfig{Cmd: "cat"}, zap.NewNop()) @@ -184,6 +197,7 @@ func TestWorker_Kill_KillsProcess(t *testing.T) { evt, waitError = w.Wait(context.Background()) return waitError == nil && evt.Signal != nil }, time.Second, 10*time.Millisecond) + require.NoError(t, waitError) } func TestWorker_Terminate_TerminatesProcess(t *testing.T) { @@ -274,5 +288,11 @@ func TestWorker_Read_ReadsFromStdout(t *testing.T) { _, err = io.Copy(&outputBuf, readPipe) assert.NoError(t, err) - assert.Equal(t, "foobar\n", outputBuf.String()) + expected := "foobar\n" + + require.Eventually(t, func() bool { + return outputBuf.String() == expected + }, 2*time.Second, 10*time.Millisecond) + + require.Equal(t, expected, outputBuf.String()) }