Keep neoirc-cli responding when a key or a typed line updates the screen (closes #223)
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
The Enter handler of the input line and the Alt and digit key handler ran the typed line on the tview event loop. Every UI method that changes the screen waits for that loop, so the client stopped responding. Each line now runs on its own goroutine, which first waits for the line before it, so lines keep the order they were typed in. /window now runs off the event loop. It reads the buffer name through the new BufferName, which runs on the loop, instead of reading the buffer list directly. A new test drives a running UI on a simulation screen with Alt and 1, then a typed line and Enter. Model: opus-5-5
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gdamore/tcell/v2"
|
||||
api "sneak.berlin/go/neoirc/internal/cli/api"
|
||||
@@ -20,6 +21,10 @@ const (
|
||||
testText = "hello"
|
||||
)
|
||||
|
||||
// eventLoopTimeout is how long a test waits for a typed line to
|
||||
// be handled, or for the UI to stop, before it fails.
|
||||
const eventLoopTimeout = 5 * time.Second
|
||||
|
||||
func TestMemberPrefix(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -251,7 +256,7 @@ func TestAltDigit(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
app := &App{ui: NewUI(), nick: testNick, target: testOtherNick}
|
||||
app.ui.OnInput(app.handleInput)
|
||||
handled := handledLines(app)
|
||||
runUI(t, app.ui)
|
||||
|
||||
// Window 1 is the channel. Window 2, the one shown, is
|
||||
@@ -260,7 +265,8 @@ func TestAltDigit(t *testing.T) {
|
||||
app.ui.SwitchToBuffer(testOtherNick)
|
||||
|
||||
key := tcell.NewEventKey(tcell.KeyRune, test.digit, tcell.ModAlt)
|
||||
app.ui.app.GetInputCapture()(key)
|
||||
app.ui.app.QueueEvent(key)
|
||||
waitHandled(t, handled, "/window "+string(test.digit))
|
||||
|
||||
var shown string
|
||||
|
||||
@@ -276,6 +282,34 @@ func TestAltDigit(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestEventLoopKeepsRunning sends Alt and 1, then a typed line and
|
||||
// Enter, to a running UI. It checks that both are handled, in that
|
||||
// order, so the event loop did not stop on either.
|
||||
func TestEventLoopKeepsRunning(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
app := &App{ui: NewUI(), nick: testNick}
|
||||
handled := handledLines(app)
|
||||
runUI(t, app.ui)
|
||||
|
||||
// Window 1 is the channel, and window 0, the status window, is
|
||||
// shown. The typed line adds a line to the status window, as
|
||||
// the client is not connected.
|
||||
app.ui.SwitchToBuffer(testChannel)
|
||||
app.ui.SwitchBuffer(0)
|
||||
|
||||
app.ui.app.QueueEvent(tcell.NewEventKey(tcell.KeyRune, '1', tcell.ModAlt))
|
||||
|
||||
for _, r := range testText {
|
||||
app.ui.app.QueueEvent(tcell.NewEventKey(tcell.KeyRune, r, tcell.ModNone))
|
||||
}
|
||||
|
||||
app.ui.app.QueueEvent(tcell.NewEventKey(tcell.KeyEnter, 0, tcell.ModNone))
|
||||
|
||||
waitHandled(t, handled, "/window 1")
|
||||
waitHandled(t, handled, testText)
|
||||
}
|
||||
|
||||
// TestOwnEventFromOtherClient checks that a message, notice,
|
||||
// join or part that the user's session makes from another
|
||||
// client shows as one line in the buffer it belongs to.
|
||||
@@ -358,6 +392,36 @@ func bufferState(ui *UI, name string) (int, int) {
|
||||
return lines, unread
|
||||
}
|
||||
|
||||
// handledLines makes app.ui pass each typed line to
|
||||
// app.handleInput, as Run does, and returns a channel that
|
||||
// receives each line once it has been handled.
|
||||
func handledLines(app *App) <-chan string {
|
||||
handled := make(chan string)
|
||||
|
||||
app.ui.OnInput(func(text string) {
|
||||
app.handleInput(text)
|
||||
|
||||
handled <- text
|
||||
})
|
||||
|
||||
return handled
|
||||
}
|
||||
|
||||
// waitHandled fails the test unless want is the next line handled,
|
||||
// within eventLoopTimeout.
|
||||
func waitHandled(t *testing.T, handled <-chan string, want string) {
|
||||
t.Helper()
|
||||
|
||||
select {
|
||||
case got := <-handled:
|
||||
if got != want {
|
||||
t.Fatalf("handled %q, want %q", got, want)
|
||||
}
|
||||
case <-time.After(eventLoopTimeout):
|
||||
t.Fatalf("%q was not handled within %v", want, eventLoopTimeout)
|
||||
}
|
||||
}
|
||||
|
||||
// newServer starts a server that answers every request with
|
||||
// body until the test ends.
|
||||
func newServer(t *testing.T, body string) *httptest.Server {
|
||||
@@ -379,16 +443,22 @@ func runUI(t *testing.T, ui *UI) {
|
||||
|
||||
ui.app.SetScreen(tcell.NewSimulationScreen(""))
|
||||
|
||||
done := make(chan error)
|
||||
// One slot, so that Run's goroutine can end even after the
|
||||
// cleanup below has stopped waiting for it.
|
||||
done := make(chan error, 1)
|
||||
|
||||
go func() { done <- ui.Run() }()
|
||||
|
||||
t.Cleanup(func() {
|
||||
ui.Stop()
|
||||
|
||||
err := <-done
|
||||
if err != nil {
|
||||
t.Errorf("run ui: %v", err)
|
||||
select {
|
||||
case err := <-done:
|
||||
if err != nil {
|
||||
t.Errorf("run ui: %v", err)
|
||||
}
|
||||
case <-time.After(eventLoopTimeout):
|
||||
t.Error("the UI did not stop: its event loop stopped responding")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user