From 640e755b9433574aa3e73b230d98c187c962e85d Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Mon, 20 Apr 2020 01:58:37 -0400 Subject: [PATCH] Wait for socat to be ready before running full test. The -D option tells it to print something just before starting the transfer phase, which should be enough to ensure that all the file links are available without having to guess a suitable timeout. --- serial_linux_test.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/serial_linux_test.go b/serial_linux_test.go index 5eaea0f..01e3923 100644 --- a/serial_linux_test.go +++ b/serial_linux_test.go @@ -18,6 +18,20 @@ import ( "github.com/stretchr/testify/require" ) +func startSocatAndWaitForPort(t *testing.T, ctx context.Context) *exec.Cmd { + cmd := exec.CommandContext(ctx, "socat", "-D", "STDIO", "pty,link=/tmp/faketty") + r, err := cmd.StderrPipe() + require.NoError(t, err) + require.NoError(t, cmd.Start()) + // Let our fake serial port node appear. + // socat will write to stderr before starting transfer phase; + // we don't really care what, just that it did, because then it's ready. + buf := make([]byte, 1024) + _, err = r.Read(buf) + require.NoError(t, err) + return cmd +} + func TestSerialReadAndCloseConcurrency(t *testing.T) { // Run this test with race detector to actually test that @@ -25,11 +39,8 @@ func TestSerialReadAndCloseConcurrency(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - cmd := exec.CommandContext(ctx, "socat", "STDIO", "pty,link=/tmp/faketty") - require.NoError(t, cmd.Start()) + cmd := startSocatAndWaitForPort(t, ctx) go cmd.Wait() - // let our fake serial port node to appear - time.Sleep(time.Millisecond * 100) port, err := Open("/tmp/faketty", &Mode{}) require.NoError(t, err) @@ -43,11 +54,8 @@ func TestSerialReadAndCloseConcurrency(t *testing.T) { func TestDoubleCloseIsNoop(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - cmd := exec.CommandContext(ctx, "socat", "STDIO", "pty,link=/tmp/faketty") - require.NoError(t, cmd.Start()) + cmd := startSocatAndWaitForPort(t, ctx) go cmd.Wait() - // let our fake serial port node to appear - time.Sleep(time.Millisecond * 100) port, err := Open("/tmp/faketty", &Mode{}) require.NoError(t, err)