Broadcast a session's QUIT once when its clients leave together (closes #154)
check / check (push) Canceled after 0s

Logout deleted the client, counted the session's clients and ran
BroadcastQuit as separate statements, and BroadcastQuit read the peers,
parted, deleted empty channels and deleted the session separately too.
Two logouts or QUITs of one session at once both sent its QUIT, and a
JOIN landing between a part and the channel's deletion got into a +k
channel without the key.

db.QuitSession now does all of that in one transaction, parting each
channel as PartChannelAndDeleteIfEmpty does, and reports whether it
deleted the session; only that caller sends the QUIT. db.LogoutClient
deletes the client and counts the rest in the same transaction.

Model: opus-5-5
This commit is contained in:
2026-10-20 21:09:39 +00:00
parent 108e381428
commit 6ed3fee559
6 changed files with 453 additions and 112 deletions
+164
View File
@@ -1393,6 +1393,170 @@ func TestQuit(t *testing.T) {
}
}
// quitRaceTrials is how many times checkQuitRace repeats
// its race.
const quitRaceTrials = 5
// TestConcurrentLogoutsSendOneQuit checks that when both
// clients of a session log out at once, the session's QUIT
// is sent once.
func TestConcurrentLogoutsSendOneQuit(t *testing.T) {
checkQuitRace(t, func(tserver *testServer, tokens []string) {
tserver.postConcurrently("/api/v1/logout", nil, tokens...)
})
}
// TestConcurrentQuitsSendOneQuit checks that when both
// clients of a session send QUIT at once, the session's
// QUIT is sent once.
func TestConcurrentQuitsSendOneQuit(t *testing.T) {
quit, err := json.Marshal(map[string]any{commandKey: irc.CmdQuit})
if err != nil {
t.Fatal(err)
}
checkQuitRace(t, func(tserver *testServer, tokens []string) {
tserver.postConcurrently(apiMessages, quit, tokens...)
})
}
// checkQuitRace gives a session a password and two
// clients, puts it on a channel with a second session, and
// calls leave with the tokens of the two clients. It checks
// that the second session then holds one QUIT from the
// first, and that the first session is deleted: its tokens
// are refused and its nick is free. It does all this
// quitRaceTrials times, each on a new server, because each
// trial logs in and logins are rate limited.
func checkQuitRace(
t *testing.T,
leave func(tserver *testServer, tokens []string),
) {
t.Helper()
const nick = "racingquitter"
for trial := range quitRaceTrials {
tserver := newTestServer(t)
firstToken := tserver.createSession(nick)
observerToken := tserver.createSession("raceobserver")
tserver.sendCommand(firstToken, map[string]any{
commandKey: irc.CmdPass, bodyKey: []string{testPassword},
})
tokens := []string{firstToken, tserver.login(nick)}
for _, token := range []string{firstToken, observerToken} {
tserver.sendCommand(token, map[string]any{
commandKey: joinCmd, toKey: "#quitrace",
})
}
_, lastID := tserver.pollMessages(observerToken, 0)
leave(tserver, tokens)
msgs, _ := tserver.pollMessages(observerToken, lastID)
quits := 0
for _, msg := range msgs {
if msg[commandKey] == irc.CmdQuit && msg["from"] == nick {
quits++
}
}
if quits != 1 {
t.Errorf("trial %d: %d QUITs, want 1: %v", trial, quits, msgs)
}
for _, token := range tokens {
status, _ := tserver.getState(token)
if status != http.StatusUnauthorized {
t.Errorf("trial %d: state got %d, want 401", trial, status)
}
}
// createSession fails the test if the nick is taken.
tserver.createSession(nick)
}
}
// login logs in to the session nick with testPassword and
// returns the new client's token.
func (tserver *testServer) login(nick string) string {
tserver.t.Helper()
body, err := json.Marshal(map[string]string{
nickKey: nick, passwordKey: testPassword,
})
if err != nil {
tserver.t.Fatalf("marshal login: %v", err)
}
resp, err := doRequest(
tserver.t, http.MethodPost,
tserver.url("/api/v1/login"), bytes.NewReader(body),
)
if err != nil {
tserver.t.Fatalf("login: %v", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
tserver.t.Fatalf("login: status %d", resp.StatusCode)
}
for _, cookie := range resp.Cookies() {
if cookie.Name == authCookieName {
return cookie.Value
}
}
tserver.t.Fatal("no auth cookie in login response")
return ""
}
// postConcurrently POSTs body to path once with each of
// tokens, all at once.
func (tserver *testServer) postConcurrently(
path string, body []byte, tokens ...string,
) {
tserver.t.Helper()
start := make(chan struct{})
var waitGroup sync.WaitGroup
for _, token := range tokens {
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
<-start
resp, err := doRequestAuth(
tserver.t, http.MethodPost, tserver.url(path),
token, bytes.NewReader(body),
)
if err != nil {
tserver.t.Error(err)
return
}
_ = resp.Body.Close()
}()
}
close(start)
waitGroup.Wait()
}
func TestUnknownCommand(t *testing.T) {
tserver := newTestServer(t)
token := tserver.createSession("cmdtest")