Added test for port double close

This commit is contained in:
Cristian Maglie
2020-04-19 00:47:22 +02:00
parent f0282e0b50
commit 66bfcede42
2 changed files with 17 additions and 1 deletions

View File

@@ -39,3 +39,18 @@ func TestSerialReadAndCloseConcurrency(t *testing.T) {
time.Sleep(time.Millisecond * 1) time.Sleep(time.Millisecond * 1)
port.Close() port.Close()
} }
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())
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)
require.NoError(t, port.Close())
require.NoError(t, port.Close())
}

View File

@@ -30,8 +30,9 @@ type unixPort struct {
func (port *unixPort) Close() error { func (port *unixPort) Close() error {
if !atomic.CompareAndSwapUint32(&port.opened, 1, 0) { if !atomic.CompareAndSwapUint32(&port.opened, 1, 0) {
return &PortError{code: PortClosed} return nil
} }
// Close port // Close port
port.releaseExclusiveAccess() port.releaseExclusiveAccess()
if err := unix.Close(port.handle); err != nil { if err := unix.Close(port.handle); err != nil {