forked from creack/pty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_test.go
119 lines (102 loc) · 2.77 KB
/
io_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
//go:build go1.12
// +build go1.12
package pty
import (
"testing"
"context"
"errors"
"os"
"runtime"
"sync"
"time"
)
const (
errMarker byte = 0xEE
timeout = time.Second
)
var mu sync.Mutex
// Check that SetDeadline() works for ptmx.
// Outstanding Read() calls must be interrupted by deadline.
//
// https://github.com/creack/pty/issues/162
func TestReadDeadline(t *testing.T) {
ptmx, success := prepare(t)
err := ptmx.SetDeadline(time.Now().Add(timeout / 10))
if err != nil {
if errors.Is(err, os.ErrNoDeadline) {
t.Skipf("deadline is not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
} else {
t.Fatalf("error: set deadline: %v\n", err)
}
}
var buf = make([]byte, 1)
n, err := ptmx.Read(buf)
success()
if n != 0 && buf[0] != errMarker {
t.Errorf("received unexpected data from pmtx (%d bytes): 0x%X; err=%v", n, buf, err)
}
}
// Check that ptmx.Close() interrupts outstanding ptmx.Read() calls
//
// https://github.com/creack/pty/issues/114
// https://github.com/creack/pty/issues/88
func TestReadClose(t *testing.T) {
ptmx, success := prepare(t)
go func() {
time.Sleep(timeout / 10)
err := ptmx.Close()
if err != nil {
t.Errorf("failed to close ptmx: %v", err)
}
}()
var buf = make([]byte, 1)
n, err := ptmx.Read(buf)
success()
if n != 0 && buf[0] != errMarker {
t.Errorf("received unexpected data from pmtx (%d bytes): 0x%X; err=%v", n, buf, err)
}
}
// Open pty and setup watchdogs for graceful and not so graceful failure modes
func prepare(t *testing.T) (ptmx *os.File, done func()) {
if runtime.GOOS == "darwin" {
t.Log("creack/pty uses blocking i/o on darwin intentionally:")
t.Log("> https://github.com/creack/pty/issues/52")
t.Log("> https://github.com/creack/pty/pull/53")
t.Log("> https://github.com/golang/go/issues/22099")
t.SkipNow()
}
// Due to data race potential in (*os.File).Fd()
// we should never run these two tests in parallel
mu.Lock()
t.Cleanup(mu.Unlock)
ptmx, pts, err := Open()
if err != nil {
t.Fatalf("error: open: %v\n", err)
}
t.Cleanup(func() { _ = ptmx.Close() })
t.Cleanup(func() { _ = pts.Close() })
ctx, done := context.WithCancel(context.Background())
t.Cleanup(done)
go func() {
select {
case <-ctx.Done():
// ptmx.Read() did not block forever, yay!
case <-time.After(timeout):
_, err := pts.Write([]byte{errMarker}) // unblock ptmx.Read()
if err != nil {
t.Errorf("failed to write to pts: %v", err)
}
t.Error("ptmx.Read() was not unblocked")
done() // cancel panic()
}
}()
go func() {
select {
case <-ctx.Done():
// Test has either failed or succeeded; it definitely did not hang
case <-time.After(timeout * 10 / 9): // timeout +11%
panic("ptmx.Read() was not unblocked; avoid hanging forever") // just in case
}
}()
return ptmx, done
}