Skip to content

Commit d6d1c8e

Browse files
committed
Use io.Pipe instead of bytes.Buffer to avoid any sync issues
Signed-off-by: Swagat Bora <[email protected]>
1 parent 19823b8 commit d6d1c8e

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

runtime/service_integ_test.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -929,21 +929,23 @@ func startAndWaitTask(ctx context.Context, t testing.TB, c containerd.Container)
929929
stdoutR, stdoutW := io.Pipe()
930930
stderrR, stderrW := io.Pipe()
931931

932-
var stdoutBuf, stderrBuf bytes.Buffer
933-
var stdoutErr, stderrErr error
934-
var wg sync.WaitGroup
932+
stdoutCh := make(chan string)
933+
stderrCh := make(chan string)
935934

936-
wg.Add(2)
937935
go func() {
938-
defer wg.Done()
939-
_, stdoutErr = io.Copy(&stdoutBuf, stdoutR)
940-
stdoutR.Close()
936+
var out bytes.Buffer
937+
_, err := io.Copy(&out, stdoutR)
938+
require.NoError(t, err, "failed to read stdout")
939+
stdoutCh <- out.String()
940+
close(stdoutCh)
941941
}()
942942

943943
go func() {
944-
defer wg.Done()
945-
_, stderrErr = io.Copy(&stderrBuf, stderrR)
946-
stderrR.Close()
944+
var out bytes.Buffer
945+
_, err := io.Copy(&out, stderrR)
946+
require.NoError(t, err, "failed to read stderr")
947+
stderrCh <- out.String()
948+
close(stderrCh)
947949
}()
948950

949951
task, err := c.NewTask(ctx, cio.NewCreator(cio.WithStreams(nil, stdoutW, stderrW)))
@@ -963,16 +965,6 @@ func startAndWaitTask(ctx context.Context, t testing.TB, c containerd.Container)
963965
assert.NoError(t, exitStatus.Error(), "failed to retrieve exitStatus")
964966
assert.Equal(t, uint32(0), exitStatus.ExitCode())
965967

966-
wg.Wait()
967-
968-
require.NoError(t, stdoutErr, "error copying stdout")
969-
require.NoError(t, stderrErr, "error copying stderr")
970-
971-
stderrOutput := stderrBuf.String()
972-
if len(stderrOutput) != 0 {
973-
fmt.Printf("stderr output from container %s: %s", c.ID(), stderrOutput)
974-
}
975-
976968
status, err := task.Delete(ctx)
977969
assert.NoErrorf(t, err, "failed to delete task %q after exit", c.ID())
978970
if status != nil {
@@ -983,7 +975,13 @@ func startAndWaitTask(ctx context.Context, t testing.TB, c containerd.Container)
983975
"context cancelled while waiting for container %s to exit, err: %v", c.ID(), ctx.Err())
984976
}
985977

986-
return stdoutBuf.String()
978+
stdout := <-stdoutCh
979+
stderr := <-stderrCh
980+
981+
assert.Equal(t, "", stderr, "stderr is not empty")
982+
assert.NotEmpty(t, stdout, "stdout is empty")
983+
984+
return stdout
987985
}
988986

989987
func testCreateContainerWithSameName(t *testing.T, vmID string) {

0 commit comments

Comments
 (0)