Skip to content

Parallelize upload_mock integration tests #2444

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

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
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
25 changes: 16 additions & 9 deletions internal/integrationtest/arduino-cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,20 @@ func (cli *ArduinoCLI) run(stdoutBuff, stderrBuff io.Writer, stdinBuff io.Reader
args = append([]string{"--config-file", cli.cliConfigPath.String()}, args...)
}

// Accumulate all output to terminal and spit-out all at once at the end of the test
// This allows to correctly group test output when running t.Parallel() tests.
terminalOut := new(bytes.Buffer)
defer func() {
fmt.Print(terminalOut.String())
}()

// Github-actions workflow tags to fold log lines
if os.Getenv("GITHUB_ACTIONS") != "" {
fmt.Printf("::group::Running %s\n", strings.Join(args, " "))
defer fmt.Println("::endgroup::")
fmt.Fprintf(terminalOut, "::group::Running %s\n", strings.Join(args, " "))
defer fmt.Fprintln(terminalOut, "::endgroup::")
}

fmt.Println(color.HiBlackString(">>> Running: ") + color.HiYellowString("%s %s", cli.path, strings.Join(args, " ")))
fmt.Fprintln(terminalOut, color.HiBlackString(">>> Running: ")+color.HiYellowString("%s %s", cli.path, strings.Join(args, " ")))
cliProc, err := executils.NewProcessFromPath(cli.convertEnvForExecutils(env), cli.path, args...)
cli.t.NoError(err)
stdout, err := cliProc.StdoutPipe()
Expand All @@ -325,29 +332,29 @@ func (cli *ArduinoCLI) run(stdoutBuff, stderrBuff io.Writer, stdinBuff io.Reader
if stdoutBuff == nil {
stdoutBuff = io.Discard
}
if _, err := io.Copy(stdoutBuff, io.TeeReader(stdout, os.Stdout)); err != nil {
fmt.Println(color.HiBlackString("<<< stdout copy error:"), err)
if _, err := io.Copy(stdoutBuff, io.TeeReader(stdout, terminalOut)); err != nil {
fmt.Fprintln(terminalOut, color.HiBlackString("<<< stdout copy error:"), err)
}
}()
go func() {
defer wg.Done()
if stderrBuff == nil {
stderrBuff = io.Discard
}
if _, err := io.Copy(stderrBuff, io.TeeReader(stderr, os.Stderr)); err != nil {
fmt.Println(color.HiBlackString("<<< stderr copy error:"), err)
if _, err := io.Copy(stderrBuff, io.TeeReader(stderr, terminalOut)); err != nil {
fmt.Fprintln(terminalOut, color.HiBlackString("<<< stderr copy error:"), err)
}
}()
if stdinBuff != nil {
go func() {
if _, err := io.Copy(stdin, stdinBuff); err != nil {
fmt.Println(color.HiBlackString("<<< stdin copy error:"), err)
fmt.Fprintln(terminalOut, color.HiBlackString("<<< stdin copy error:"), err)
}
}()
}
wg.Wait()
cliErr := cliProc.Wait()
fmt.Println(color.HiBlackString("<<< Run completed (err = %v)", cliErr))
fmt.Fprintln(terminalOut, color.HiBlackString("<<< Run completed (err = %v)", cliErr))

return cliErr
}
Expand Down
20 changes: 11 additions & 9 deletions internal/integrationtest/upload_mock/upload_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type parametersMap struct {

func TestUploadSketch(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
t.Cleanup(env.CleanUp)

indexes := []string{
"https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json",
Expand Down Expand Up @@ -655,13 +655,14 @@ func TestUploadSketch(t *testing.T) {
sketchPath := cli.SketchbookDir().Join(sketchName)
_, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)
buildDir := generateBuildDir(sketchPath, t)
t.Cleanup(func() { buildDir.RemoveAll() })

var stdout []byte

for i, test := range testParameters {
for i, _test := range testParameters {
test := _test
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
buildDir := generateBuildDir(sketchPath, t)
defer buildDir.RemoveAll()
t.Parallel()
var stdout []byte
if test.Programmer != "" {
if test.UploadPort != "" {
stdout, _, err = cli.Run("upload", "-p", test.UploadPort, "-P", test.Programmer, "-b", test.Fqbn, sketchPath.String(), "--dry-run", "-v")
Expand All @@ -686,10 +687,11 @@ func TestUploadSketch(t *testing.T) {
})
}

for i, test := range testParametersMap {
for i, _test := range testParametersMap {
test := _test
t.Run(fmt.Sprintf("WithMap%d", i), func(t *testing.T) {
buildDir := generateBuildDir(sketchPath, t)
defer buildDir.RemoveAll()
t.Parallel()
var stdout []byte
if test.Programmer != "" {
if test.UploadPort != "" {
stdout, _, err = cli.Run("upload", "-p", test.UploadPort, "-P", test.Programmer, "-b", test.Fqbn, sketchPath.String(), "--dry-run", "-v")
Expand Down