Skip to content

Commit 5f5faa1

Browse files
authored
Reviewed tests for race conditions (#11)
1 parent 20454c2 commit 5f5faa1

1 file changed

Lines changed: 25 additions & 5 deletions

File tree

internal/execution/worker/worker_test.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ func TestWorker_Start_IsAlive(t *testing.T) {
2424

2525
defer w.Kill()
2626

27-
assert.Equal(t, true, util.IsProcessAlive(w.Pid()))
27+
pid := w.Pid()
28+
require.NotZero(t, pid, "pid should be set after Start")
29+
30+
require.Eventually(t, func() bool {
31+
return util.IsProcessAlive(pid)
32+
}, 2*time.Second, 10*time.Millisecond, "process never reported alive")
33+
2834
}
2935

3036
func TestWorker_Start_FailsIfStarted(t *testing.T) {
@@ -35,8 +41,14 @@ func TestWorker_Start_FailsIfStarted(t *testing.T) {
3541

3642
defer w.Kill()
3743

38-
err = w.Start(context.Background())
39-
assert.Error(t, err)
44+
require.Eventually(t, func() bool {
45+
pid := w.Pid()
46+
return pid != 0 && util.IsProcessAlive(pid)
47+
}, 2*time.Second, 10*time.Millisecond, "worker never became alive")
48+
49+
// Now a second Start should deterministically fail.
50+
secondWorkerErr := w.Start(context.Background())
51+
require.Error(t, secondWorkerErr)
4052
}
4153

4254
func TestWorker_Start_ReturnsErrorIfInvalidCommand(t *testing.T) {
@@ -72,7 +84,7 @@ func TestWorker_TerminatesIfContextCancelled(t *testing.T) {
7284
require.Eventually(t, func() bool {
7385
evt, waitError = w.Wait(context.Background())
7486
return waitError == nil && evt.Signal != nil
75-
}, time.Second, 10*time.Millisecond)
87+
}, 5*time.Second, 10*time.Millisecond)
7688

7789
require.NoError(t, waitError)
7890
require.NotNil(t, evt)
@@ -125,6 +137,7 @@ func TestWorker_Wait_ReturnsErrorIfContextCancelled(t *testing.T) {
125137
_, err = w.Wait(ctx)
126138
assert.Error(t, err)
127139
}
140+
128141
func TestWorker_Wait_ReturnsErrorIfCalledMultiple(t *testing.T) {
129142
w := worker.NewProcessWorker(context.Background(), worker.StartConfig{Cmd: "cat"}, zap.NewNop())
130143

@@ -184,6 +197,7 @@ func TestWorker_Kill_KillsProcess(t *testing.T) {
184197
evt, waitError = w.Wait(context.Background())
185198
return waitError == nil && evt.Signal != nil
186199
}, time.Second, 10*time.Millisecond)
200+
require.NoError(t, waitError)
187201
}
188202

189203
func TestWorker_Terminate_TerminatesProcess(t *testing.T) {
@@ -274,5 +288,11 @@ func TestWorker_Read_ReadsFromStdout(t *testing.T) {
274288
_, err = io.Copy(&outputBuf, readPipe)
275289
assert.NoError(t, err)
276290

277-
assert.Equal(t, "foobar\n", outputBuf.String())
291+
expected := "foobar\n"
292+
293+
require.Eventually(t, func() bool {
294+
return outputBuf.String() == expected
295+
}, 2*time.Second, 10*time.Millisecond)
296+
297+
require.Equal(t, expected, outputBuf.String())
278298
}

0 commit comments

Comments
 (0)