Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a developer error in the jobreceiver tests #1659

Merged
merged 1 commit into from
Aug 19, 2024
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
38 changes: 23 additions & 15 deletions pkg/receiver/jobreceiver/command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,14 @@ func TestExecute(t *testing.T) {
t.Log("Running basic test")
// Basic command execution
t.Run("basic", func(t *testing.T) {
t.Log("debug 1")
echo := NewExecution(ctx, withTestHelper(t, ExecutionRequest{Command: "echo", Arguments: []string{"hello", "world"}}))
t.Log("debug 2")
outC := eventualOutput(t, echo)
t.Log("debug 3")
resp, err := echo.Run()
t.Log("debug 4")
require.NoError(t, err)
t.Log("debug 5")
assert.Equal(t, 0, resp.Status)
t.Log("debug 6")
assert.Contains(t, <-outC, "hello world")
t.Log("debug 7")
output := <-outC
require.NoError(t, output.Error)
assert.Contains(t, output.Message, "hello world")
})

// Command exits non-zero
Expand Down Expand Up @@ -97,7 +92,9 @@ func TestExecute(t *testing.T) {
time.Sleep(time.Millisecond * 100)
_, err = echo.Run()
assert.Error(t, err)
assert.Contains(t, <-outC, "hello world")
output := <-outC
require.NoError(t, output.Error)
assert.Contains(t, output.Message, "hello world")
})
}

Expand All @@ -110,21 +107,32 @@ func withTestHelper(t *testing.T, r ExecutionRequest) ExecutionRequest {
return r
}

func eventualOutput(t *testing.T, i *Execution) <-chan string {
type executionOutput struct {
Message string
Error error
}

func eventualOutput(t *testing.T, i *Execution) <-chan executionOutput {
t.Helper()
out := make(chan string, 1)
out := make(chan executionOutput, 1)
stdout, err := i.Stdout()
require.NoError(t, err)
stderr, err := i.Stderr()
require.NoError(t, err)
go func() {
defer close(out)
var buf bytes.Buffer
_, err := io.Copy(&buf, stdout)
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like calling NoError in the go routine was the issue...

if err != nil {
out <- executionOutput{Error: err}
return
}
_, err = io.Copy(&buf, stderr)
require.NoError(t, err)
out <- buf.String()
close(out)
if err != nil {
out <- executionOutput{Error: err}
return
}
out <- executionOutput{Message: buf.String()}
}()
return out
}
Loading