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.
This commit is contained in:
Elliott Sales de Andrade
2020-04-20 01:58:37 -04:00
committed by Cristian Maglie
parent 49ab90eff3
commit 640e755b94

View File

@@ -18,6 +18,20 @@ import (
"github.com/stretchr/testify/require" "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) { func TestSerialReadAndCloseConcurrency(t *testing.T) {
// Run this test with race detector to actually test that // 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()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
cmd := exec.CommandContext(ctx, "socat", "STDIO", "pty,link=/tmp/faketty") cmd := startSocatAndWaitForPort(t, ctx)
require.NoError(t, cmd.Start())
go cmd.Wait() go cmd.Wait()
// let our fake serial port node to appear
time.Sleep(time.Millisecond * 100)
port, err := Open("/tmp/faketty", &Mode{}) port, err := Open("/tmp/faketty", &Mode{})
require.NoError(t, err) require.NoError(t, err)
@@ -43,11 +54,8 @@ func TestSerialReadAndCloseConcurrency(t *testing.T) {
func TestDoubleCloseIsNoop(t *testing.T) { func TestDoubleCloseIsNoop(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
cmd := exec.CommandContext(ctx, "socat", "STDIO", "pty,link=/tmp/faketty") cmd := startSocatAndWaitForPort(t, ctx)
require.NoError(t, cmd.Start())
go cmd.Wait() go cmd.Wait()
// let our fake serial port node to appear
time.Sleep(time.Millisecond * 100)
port, err := Open("/tmp/faketty", &Mode{}) port, err := Open("/tmp/faketty", &Mode{})
require.NoError(t, err) require.NoError(t, err)