From 6500d68fc8f04a6a79c5d70a9070536b8103ef7b Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Sun, 19 Apr 2020 00:20:16 +0200 Subject: [PATCH] Added test to detect correct behaviour in Close and Read ops --- .github/workflows/test.yaml | 6 +++++- go.sum | 1 + serial_linux_test.go | 41 +++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 serial_linux_test.go diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 98ddb74..ff195d1 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -22,8 +22,12 @@ jobs: - name: Build native run: GOARCH=amd64 go build -v ./... shell: bash + - name: Install socat + if: matrix.os == 'ubuntu-latest' + run: sudo apt-get install socat + shell: bash - name: Run unit tests - run: go test -v ./... + run: go test -v -race ./... shell: bash - name: Cross-build for 386 if: matrix.os != 'macOS-latest' diff --git a/go.sum b/go.sum index e6f7dc3..b1c6e18 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,7 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9 h1:ZBzSG/7F4eNKz2L3GE9o300RX0Az1Bw5HF7PDraD+qU= golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/serial_linux_test.go b/serial_linux_test.go new file mode 100644 index 0000000..219efd5 --- /dev/null +++ b/serial_linux_test.go @@ -0,0 +1,41 @@ +// +// Copyright 2014-2020 Cristian Maglie. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. +// +// Testing code idea and fix thanks to @angri +// https://github.com/bugst/go-serial/pull/42 +// + +package serial + +import ( + "context" + "os/exec" + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +func TestSerialReadAndCloseConcurrency(t *testing.T) { + + // Run this test with race detector to actually test that + // the correct multitasking behaviour is happening. + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + cmd := exec.CommandContext(ctx, "socat", "STDIO", "pty,link=/tmp/faketty") + require.NoError(t, cmd.Start()) + 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) + buf := make([]byte, 100) + go port.Read(buf) + // let port.Read to start + time.Sleep(time.Millisecond * 1) + port.Close() +}