1 Commits
Author SHA1 Message Date
sneak c8c7ac665d Stop the earlier poll loop on a second /connect (closes #228)
check / check (push) Waiting to run
A second /connect started a new poll loop without stopping the first.
The first loop then polled with the new client too, so each message
could be shown twice. It also read stopPoll without a.mu while
cmdConnect wrote it.

cmdConnect now closes the earlier loop's channel in the same locked
step that replaces the client and the cursor, and hands the new
channel to the new loop. The loop checks its channel under a.mu
before each poll and again before it moves the cursor, so a stopped
loop never polls with the new client or moves the new cursor.

Model: opus-5-5
2026-11-02 02:59:15 +00:00
+92
View File
@@ -490,6 +490,98 @@ func TestSecondConnectStopsFirstPollLoop(t *testing.T) {
app.mu.Unlock()
}
// TestSecondConnectIgnoresHeldPoll holds a poll of the earlier
// connection at its server while a second /connect runs, then
// answers it with a cursor and a message. It fails if that answer
// moves the cursor or shows the message.
func TestSecondConnectIgnoresHeldPoll(t *testing.T) {
t.Parallel()
const session = `{"id": 1, "nick": "erin"}`
const answer = `{"messages": [{"command": "PRIVMSG", "from": "bob",
"to": "#chan", "body": ["hello"]}], "last_id": 42}`
// The first server sends on polled when the poll arrives, and
// answers it once release is closed.
polled := make(chan struct{})
release := make(chan struct{})
first := httptest.NewServer(http.HandlerFunc(
func(writer http.ResponseWriter, request *http.Request) {
select {
case polled <- struct{}{}:
case <-request.Context().Done():
return
}
select {
case <-release:
_, _ = io.WriteString(writer, answer)
case <-request.Context().Done():
}
},
))
t.Cleanup(first.Close)
t.Cleanup(first.CloseClientConnections)
// The second server answers every request at once with the
// session. A poll answered with it has no cursor and no messages,
// so the second poll loop never moves the cursor.
second := newServer(t, session)
// The test runs the earlier connection's poll loop itself, so
// that it can wait for the loop to end.
stop := make(chan struct{})
app := &App{
ui: NewUI(),
client: api.NewClient(first.URL),
nick: testNick,
connected: true,
stopPoll: stop,
}
runUI(t, app.ui)
done := make(chan struct{})
go func() {
app.pollLoop(stop)
close(done)
}()
select {
case <-polled:
case <-time.After(eventLoopTimeout):
t.Fatalf("the first server got no poll within %v", eventLoopTimeout)
}
app.cmdConnect(second.URL)
close(release)
select {
case <-done:
case <-time.After(eventLoopTimeout):
t.Fatalf("the earlier poll loop did not end within %v", eventLoopTimeout)
}
app.mu.Lock()
cursor := app.lastQID
app.mu.Unlock()
if cursor != 0 {
t.Errorf("the earlier connection's answer moved the cursor to %d", cursor)
}
lines, _ := bufferState(app.ui, testChannel)
if lines != 0 {
t.Errorf("the earlier connection's message was shown in %s", testChannel)
}
// Stop the second poll loop as /quit does, without stopping the UI.
app.mu.Lock()
close(app.stopPoll)
app.mu.Unlock()
}
// TestStartUp runs the client's start-up path on a simulation
// screen. It fails unless the event loop starts and the status
// window gets two lines: first the one that holds "Welcome to