Skip to content

Commit

Permalink
[skip-changelog] Parallelize upload_mock integration tests (#2444)
Browse files Browse the repository at this point in the history
* Parallelize upload_mock integration tests

* Accumulate output in integration test' runs of arduino-cli

Otherwise the output may be interleaved if tests are run in parallel.
  • Loading branch information
cmaglie authored Nov 29, 2023
1 parent ab03161 commit ecc1ece
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
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

0 comments on commit ecc1ece

Please sign in to comment.