forked from bugst/go-serial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_linux_test.go
135 lines (112 loc) · 2.79 KB
/
serial_linux_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
// Copyright 2014-2021 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"
"errors"
"io"
"os"
"os/exec"
"testing"
"time"
"github.com/stretchr/testify/require"
)
const ttyPath = "/tmp/faketty"
type ttyProc struct {
t *testing.T
cmd *exec.Cmd
}
func (tp *ttyProc) Close() error {
err := tp.cmd.Process.Signal(os.Interrupt)
require.NoError(tp.t, err)
return tp.cmd.Wait()
}
func (tp *ttyProc) waitForPort() {
for {
_, err := os.Stat(ttyPath)
if err == nil {
return
}
if !errors.Is(err, os.ErrNotExist) {
require.NoError(tp.t, err)
}
time.Sleep(time.Millisecond)
}
}
func startSocatAndWaitForPort(t *testing.T, ctx context.Context) io.Closer {
cmd := exec.CommandContext(ctx, "socat", "STDIO", "pty,link="+ttyPath)
require.NoError(t, cmd.Start())
socat := &ttyProc{
t: t,
cmd: cmd,
}
socat.waitForPort()
return socat
}
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()
socat := startSocatAndWaitForPort(t, ctx)
defer socat.Close()
port, err := Open(ttyPath, &Mode{})
require.NoError(t, err)
defer port.Close()
buf := make([]byte, 100)
go port.Read(buf)
// let port.Read to start
time.Sleep(time.Millisecond * 1)
}
func TestDoubleCloseIsNoop(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
socat := startSocatAndWaitForPort(t, ctx)
defer socat.Close()
port, err := Open(ttyPath, &Mode{})
require.NoError(t, err)
require.NoError(t, port.Close())
require.NoError(t, port.Close())
}
func TestCancelStopsRead(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
socat := startSocatAndWaitForPort(t, ctx)
defer socat.Close()
port, err := Open(ttyPath, &Mode{})
require.NoError(t, err)
defer port.Close()
readCtx, readCancel := context.WithCancel(context.Background())
done := make(chan struct{})
var readErr error
go func() {
buf := make([]byte, 100)
_, readErr = port.ReadContext(readCtx, buf)
close(done)
}()
time.Sleep(time.Millisecond)
select {
case <-done:
require.NoError(t, readErr)
require.Fail(t, "expected reading to be in-progress")
default:
}
readCancel()
time.Sleep(time.Millisecond)
select {
case <-done:
default:
require.Fail(t, "expected reading to be finished")
}
var portErr *PortError
if !errors.As(readErr, &portErr) {
require.Fail(t, "expected read error to be a port error")
}
require.Equal(t, portErr.Code(), ReadCanceled)
}