Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions internal/execution/worker/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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())

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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())
}
Loading