Skip to content

Commit b7b73b3

Browse files
committed
Synchonize captureFifoToFile's goroutine with its test
Instead of waiting for 250ms, we should explicitly synchronize the goroutine and the test, to be safer. Signed-off-by: Kazuyoshi Kato <[email protected]>
1 parent a9de986 commit b7b73b3

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

machine.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,10 @@ func (m *Machine) setupLogging(ctx context.Context) error {
584584
}
585585

586586
func (m *Machine) captureFifoToFile(ctx context.Context, logger *log.Entry, fifoPath string, w io.Writer) error {
587+
return m.captureFifoToFileWithChannel(ctx, logger, fifoPath, w, make(chan error, 1))
588+
}
589+
590+
func (m *Machine) captureFifoToFileWithChannel(ctx context.Context, logger *log.Entry, fifoPath string, w io.Writer, done chan error) error {
587591
// open the fifo pipe which will be used
588592
// to write its contents to a file.
589593
fifoPipe, err := fifo.OpenFifo(ctx, fifoPath, syscall.O_RDONLY|syscall.O_NONBLOCK, 0600)
@@ -620,7 +624,10 @@ func (m *Machine) captureFifoToFile(ctx context.Context, logger *log.Entry, fifo
620624

621625
if _, err := io.Copy(w, fifoPipe); err != nil {
622626
logger.WithError(err).Warn("io.Copy failed to copy contents of fifo pipe")
627+
done <- err
623628
}
629+
630+
close(done)
624631
}()
625632

626633
return nil

machine_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,20 +1073,19 @@ func TestCaptureFifoToFile_leak(t *testing.T) {
10731073
logger := fctesting.NewLogEntry(t)
10741074
logger.Logger.Level = logrus.WarnLevel
10751075
logger.Logger.Out = loggerBuffer
1076-
err = m.captureFifoToFile(context.Background(), logger, fifoPath, buf)
1076+
1077+
done := make(chan error)
1078+
err = m.captureFifoToFileWithChannel(context.Background(), logger, fifoPath, buf, done)
10771079
assert.NoError(t, err, "failed to capture fifo to file")
1080+
1081+
// Stopping the machine will close the FIFO
10781082
close(m.exitCh)
10791083

1080-
// wait sometime for the logs to populate
1081-
time.Sleep(250 * time.Millisecond)
1082-
expectedClosedFifo := `reading from a closed fifo`
1083-
expectedClosedFile := `file already closed`
1084-
logs := loggerBuffer.String()
1085-
if !(strings.Contains(logs, expectedClosedFifo) ||
1086-
strings.Contains(logs, expectedClosedFile)) {
1084+
// Waiting the channel to make sure that the contents of the FIFO has been copied
1085+
copyErr := <-done
10871086

1088-
t.Errorf("logs did not container a closed fifo error or closed file")
1089-
}
1087+
assert.Contains(t, copyErr.Error(), `file already closed`, "error")
1088+
assert.Contains(t, loggerBuffer.String(), `file already closed`, "log")
10901089
}
10911090

10921091
func TestWaitWithKill(t *testing.T) {

0 commit comments

Comments
 (0)