okami.mom/ultraviolet

git clone https://git.nkp.pet/okami.mom/ultraviolet

jane michelle arcfix: hold the poll reader for the input loop's lifetime (#179)666ce5e

upstream
1.2 KiB37 linesraw
1package uv
2
3import (
4	"runtime"
5	"testing"
6)
7
8// TestStopWithInputInFlightDoesNotPanic is the regression test for #177. Stop
9// set t.pr to nil while the input loop could still be about to evaluate it, so
10// the loop dereferenced nil, in ultraviolet's own goroutine where the
11// application cannot recover.
12//
13// each round starts a terminal with a key in flight and stops it at once.
14// there is nothing to assert beyond surviving the rounds.
15//
16// the test pins GOMAXPROCS to 1. with one processor the input loop has not
17// been scheduled by the time Stop returns, so the bug panics in the first
18// round on every run. with several, the loop usually gets to Read first on
19// another processor, and the bug went unseen in every -race run tried.
20func TestStopWithInputInFlightDoesNotPanic(t *testing.T) {
21	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
22
23	for i := range 1000 {
24		con := newPipeConsole()
25		tm := NewTerminal(con, nil)
26		if err := tm.Start(); err != nil {
27			t.Fatalf("round %d: Start: %v", i, err)
28		}
29		go con.send("x")
30		if err := tm.Stop(); err != nil {
31			t.Fatalf("round %d: Stop: %v", i, err)
32		}
33		// with the fix the input loop may still be blocked on the pipe;
34		// closing it fails that read and lets the goroutine exit.
35		_ = con.feed.Close()
36	}
37}