Skip to content

Commit 1cb6f22

Browse files
bearritobstrausser
and
bstrausser
authored
feat(exitcode): Add exit code sugar method (#2342)
* Add exit code sugar method * Fix lint errors * Tweak test name * Rename test --------- Co-authored-by: bstrausser <bstrausser@locusrobotics.com>
1 parent 32f372c commit 1cb6f22

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

wait/exec.go

+6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ func (ws *ExecStrategy) WithStartupTimeout(startupTimeout time.Duration) *ExecSt
4545
return ws
4646
}
4747

48+
func (ws *ExecStrategy) WithExitCode(exitCode int) *ExecStrategy {
49+
return ws.WithExitCodeMatcher(func(actualCode int) bool {
50+
return actualCode == exitCode
51+
})
52+
}
53+
4854
func (ws *ExecStrategy) WithExitCodeMatcher(exitCodeMatcher func(exitCode int) bool) *ExecStrategy {
4955
ws.ExitCodeMatcher = exitCodeMatcher
5056
return ws

wait/exec_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,27 @@ func TestExecStrategyWaitUntilReady_CustomExitCode(t *testing.T) {
156156
}
157157
}
158158

159+
func TestExecStrategyWaitUntilReady_withExitCode(t *testing.T) {
160+
target := mockExecTarget{
161+
exitCode: 10,
162+
}
163+
wg := wait.NewExecStrategy([]string{"true"}).WithExitCode(10)
164+
// Default is 60. Let's shorten that
165+
wg.WithStartupTimeout(time.Second * 2)
166+
err := wg.WaitUntilReady(context.Background(), target)
167+
if err != nil {
168+
t.Fatal(err)
169+
}
170+
171+
// Ensure we aren't spuriously returning on any code
172+
wg = wait.NewExecStrategy([]string{"true"}).WithExitCode(0)
173+
wg.WithStartupTimeout(time.Second * 2)
174+
err = wg.WaitUntilReady(context.Background(), target)
175+
if err == nil {
176+
t.Fatalf("Expected strategy to timeout out")
177+
}
178+
}
179+
159180
func TestExecStrategyWaitUntilReady_CustomResponseMatcher(t *testing.T) {
160181
// waitForExecExitCodeResponse {
161182
dockerReq := testcontainers.ContainerRequest{

0 commit comments

Comments
 (0)