Skip to content

Commit cfd4e72

Browse files
committed
Attempt to fix output test flakes
In TestStderrVsStdout maybe process.Wait() yielded before the copy got the last bit of stdout? I am not sure that is possible but add a wait group. In the testExecer case there was already a wait group but I replaced it to be consistent. There is a flake here as well but I have no idea how to fix it.
1 parent ad87a3b commit cfd4e72

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

client_test.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"cdr.dev/slog/sloggers/slogtest/assert"
1616
"cdr.dev/wsep/internal/proto"
1717
"github.com/google/go-cmp/cmp"
18+
"golang.org/x/sync/errgroup"
1819
"nhooyr.io/websocket"
1920
)
2021

@@ -151,12 +152,22 @@ func TestStderrVsStdout(t *testing.T) {
151152
})
152153
assert.Success(t, "start command", err)
153154

154-
go io.Copy(&stdout, process.Stdout())
155-
go io.Copy(&stderr, process.Stderr())
155+
var outputgroup errgroup.Group
156+
outputgroup.Go(func() error {
157+
_, err := io.Copy(&stdout, process.Stdout())
158+
return err
159+
})
160+
outputgroup.Go(func() error {
161+
_, err := io.Copy(&stderr, process.Stderr())
162+
return err
163+
})
156164

157165
err = process.Wait()
158166
assert.Success(t, "wait for process to complete", err)
159167

168+
err = outputgroup.Wait()
169+
assert.Success(t, "wait for output", err)
170+
160171
assert.Equal(t, "stdout", "stdout-message", strings.TrimSpace(stdout.String()))
161172
assert.Equal(t, "stderr", "stderr-message", strings.TrimSpace(stderr.String()))
162173
}

localexec_test.go

+21-24
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ import (
44
"bytes"
55
"context"
66
"io"
7-
"io/ioutil"
87
"os"
98
"strings"
10-
"sync"
119
"testing"
1210
"time"
1311

1412
"cdr.dev/slog/sloggers/slogtest/assert"
13+
"golang.org/x/sync/errgroup"
1514
)
1615

1716
func TestLocalExec(t *testing.T) {
@@ -27,35 +26,33 @@ func testExecer(ctx context.Context, t *testing.T, execer Execer) {
2726
Command: "pwd",
2827
})
2928
assert.Success(t, "start local cmd", err)
29+
3030
var (
31-
stderr = process.Stderr()
32-
stdout = process.Stdout()
33-
wg sync.WaitGroup
31+
stdout bytes.Buffer
32+
stderr bytes.Buffer
3433
)
3534

36-
wg.Add(1)
37-
go func() {
38-
defer wg.Done()
35+
var outputgroup errgroup.Group
36+
outputgroup.Go(func() error {
37+
_, err := io.Copy(&stdout, process.Stdout())
38+
return err
39+
})
40+
outputgroup.Go(func() error {
41+
_, err := io.Copy(&stderr, process.Stderr())
42+
return err
43+
})
3944

40-
stdoutByt, err := ioutil.ReadAll(stdout)
41-
assert.Success(t, "read stdout", err)
42-
wd, err := os.Getwd()
43-
assert.Success(t, "get real working dir", err)
45+
err = process.Wait()
46+
assert.Success(t, "wait for process to complete", err)
4447

45-
assert.Equal(t, "stdout", wd, strings.TrimSuffix(string(stdoutByt), "\n"))
46-
}()
47-
wg.Add(1)
48-
go func() {
49-
defer wg.Done()
48+
err = outputgroup.Wait()
49+
assert.Success(t, "wait for output", err)
5050

51-
stderrByt, err := ioutil.ReadAll(stderr)
52-
assert.Success(t, "read stderr", err)
53-
assert.True(t, "len stderr", len(stderrByt) == 0)
54-
}()
51+
wd, err := os.Getwd()
52+
assert.Success(t, "get real working dir", err)
5553

56-
wg.Wait()
57-
err = process.Wait()
58-
assert.Success(t, "wait for process to complete", err)
54+
assert.Equal(t, "stdout", wd, strings.TrimSpace(stdout.String()))
55+
assert.Equal(t, "stderr", "", strings.TrimSpace(stderr.String()))
5956
}
6057

6158
func TestExitCode(t *testing.T) {

0 commit comments

Comments
 (0)