Skip to content

Commit

Permalink
Read from StdErr and StdOut concurrently to prevent buffers to fill
Browse files Browse the repository at this point in the history
  • Loading branch information
aduffeck committed Nov 29, 2023
1 parent c844761 commit e7e9589
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions tests/ociswrapper/ocis/ocis.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,29 @@ func Start(envMap map[string]any) {
log.Panic(err)
}

// Read and print the logs when the 'ocis server' command is running
logScanner := bufio.NewScanner(logs)
for logScanner.Scan() {
m := logScanner.Text()
fmt.Println(m)
}
// Read output when the 'ocis server' command gets exited
outputScanner := bufio.NewScanner(output)
for outputScanner.Scan() {
m := outputScanner.Text()
fmt.Println(m)
}
outChan := make(chan string)

// Read the logs when the 'ocis server' command is running
go func() {
for logScanner.Scan() {
outChan <- logScanner.Text()
}
}()

go func() {
for outputScanner.Scan() {
outChan <- outputScanner.Text()
}
}()

// Fetch logs from the channel and print them
go func() {
for s := range outChan {
fmt.Println(s)
}
}()

if err := cmd.Wait(); err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
Expand All @@ -82,6 +93,7 @@ func Start(envMap map[string]any) {
Start(envMap)
}
}
close(outChan)
}
}
}
Expand Down

0 comments on commit e7e9589

Please sign in to comment.