mirror of
https://github.com/creack/pty.git
synced 2026-03-31 10:37:09 +09:00
When compiled with go older than 1.12 creack/pty will not include a fix for blocking Read() and will be prone to data races - but at least it will work For more information see issues: #88 #114 #156 #162
106 lines
2.3 KiB
Go
106 lines
2.3 KiB
Go
//go:build go1.12
|
|
// +build go1.12
|
|
|
|
package pty
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"context"
|
|
"os"
|
|
"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 {
|
|
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()) {
|
|
|
|
// 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())
|
|
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
|
|
}
|