diff --git a/serial_test.go b/serial_test.go index 78fce0a..226ebb5 100644 --- a/serial_test.go +++ b/serial_test.go @@ -3,6 +3,7 @@ package modbus import ( "bytes" "io" + "sync/atomic" "testing" "time" ) @@ -10,11 +11,11 @@ import ( type nopCloser struct { io.ReadWriter - closed bool + closed atomic.Bool } func (n *nopCloser) Close() error { - n.closed = true + n.closed.Store(true) return nil } @@ -30,7 +31,9 @@ func TestSerialCloseIdle(t *testing.T) { s.startCloseTimer() time.Sleep(150 * time.Millisecond) - if !port.closed || s.port != nil { + s.mu.Lock() + defer s.mu.Unlock() + if !port.closed.Load() || s.port != nil { t.Fatalf("serial port is not closed when inactivity: %+v", port) } } diff --git a/tcpclient_test.go b/tcpclient_test.go index 3854951..4abb6f4 100644 --- a/tcpclient_test.go +++ b/tcpclient_test.go @@ -83,6 +83,8 @@ func TestTCPTransporter(t *testing.T) { t.Fatalf("unexpected response: %x", rsp) } time.Sleep(150 * time.Millisecond) + client.mu.Lock() + defer client.mu.Unlock() if client.conn != nil { t.Fatalf("connection is not closed: %+v", client.conn) }