| 1 | package modbus |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "io" |
| 6 | "testing" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | type nopCloser struct { |
| 11 | io.ReadWriter |
| 12 | |
| 13 | closed bool |
| 14 | } |
| 15 | |
| 16 | func (n *nopCloser) Close() error { |
| 17 | n.closed = true |
| 18 | return nil |
| 19 | } |
| 20 | |
| 21 | func TestSerialCloseIdle(t *testing.T) { |
| 22 | port := &nopCloser{ |
| 23 | ReadWriter: &bytes.Buffer{}, |
| 24 | } |
| 25 | s := serialPort{ |
| 26 | port: port, |
| 27 | IdleTimeout: 100 * time.Millisecond, |
| 28 | } |
| 29 | s.lastActivity = time.Now() |
| 30 | s.startCloseTimer() |
| 31 | |
| 32 | time.Sleep(150 * time.Millisecond) |
| 33 | if !port.closed || s.port != nil { |
| 34 | t.Fatalf("serial port is not closed when inactivity: %+v", port) |
| 35 | } |
| 36 | } |