Added test to detect correct behaviour in Close and Read ops

This commit is contained in:
Cristian Maglie
2020-04-19 00:20:16 +02:00
parent 1b760a82af
commit 6500d68fc8
3 changed files with 47 additions and 1 deletions

View File

@@ -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'

1
go.sum
View File

@@ -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=

41
serial_linux_test.go Normal file
View File

@@ -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()
}