83 lines
2.5 KiB
Go
83 lines
2.5 KiB
Go
// Fenrir console screen reader.
|
|
// Copyright (c) 2026 Storm Dragon
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify it
|
|
// under the terms of the GNU General Public License as published by the Free
|
|
// Software Foundation, either version 3 of the License, or (at your option)
|
|
// any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
// more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License along with
|
|
// this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.
|
|
|
|
//go:build linux && cgo
|
|
|
|
package terminal
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"os"
|
|
"syscall"
|
|
"testing"
|
|
)
|
|
|
|
func TestTerminalOutputBackpressurePreservesBytes(t *testing.T) {
|
|
var pipeFds [2]int
|
|
if err := syscall.Pipe2(pipeFds[:], syscall.O_NONBLOCK|syscall.O_CLOEXEC); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer func() {
|
|
if pipeFds[0] >= 0 {
|
|
syscall.Close(pipeFds[0])
|
|
}
|
|
}()
|
|
output := os.NewFile(uintptr(pipeFds[1]), "terminal-output")
|
|
defer output.Close()
|
|
want := bytes.Repeat([]byte("\x1b[1mredraw é界\x1b[0m\r\n"), 16384)
|
|
session := &Session{output: output, outputPending: bytes.Clone(want)}
|
|
if err := session.flush_output(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
remaining := len(session.outputPending)
|
|
if remaining == 0 || remaining == len(want) {
|
|
t.Fatalf("expected partial write, %d of %d bytes remain", remaining, len(want))
|
|
}
|
|
if err := session.flush_output(); err != nil || len(session.outputPending) != remaining {
|
|
t.Fatalf("full output must defer without losing data: remaining=%d err=%v", len(session.outputPending), err)
|
|
}
|
|
var got []byte
|
|
var buffer [4096]byte
|
|
for {
|
|
n, err := syscall.Read(pipeFds[0], buffer[:])
|
|
if n > 0 {
|
|
got = append(got, buffer[:n]...)
|
|
}
|
|
if errors.Is(err, syscall.EAGAIN) {
|
|
if len(session.outputPending) == 0 {
|
|
break
|
|
}
|
|
if err := session.flush_output(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
} else if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if !bytes.Equal(got, want) {
|
|
t.Fatalf("terminal output changed: got %d bytes, want %d", len(got), len(want))
|
|
}
|
|
// A closed terminal remains an error; only temporary fullness is retried.
|
|
syscall.Close(pipeFds[0])
|
|
pipeFds[0] = -1
|
|
session.outputPending = []byte("closed")
|
|
if err := session.flush_output(); !errors.Is(err, syscall.EPIPE) {
|
|
t.Fatalf("closed output: %v", err)
|
|
}
|
|
}
|