Added test for port double close
This commit is contained in:
@@ -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())
|
||||||
|
}
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user