aboutsummaryrefslogtreecommitdiff
path: root/internal/game/quit_state_race_test.go
blob: 68f477057910d377837a974f8e9dbc6d051588c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package game

// Regression: network input may arrive on a connection goroutine at any
// moment, including while the tick-driven logout countdown (owner goroutine)
// transitions the session to the character menu. The dispatch read
// sess.State as a plain field, which the race detector flagged; the state is
// now an atomic on net.Session. This test drives both sides concurrently and
// must run clean under -race.

import (
	"testing"
	"time"
)

func TestInputDuringLogoutCountdownIsRaceFree(t *testing.T) {
	h := newHarness(t)
	h.seedAccount("alice")
	h.seedCharacter("alice", "alice")
	sess, _ := h.connect("alice")

	h.g.RunOnOwner(func() {
		h.g.beginQuitCountdown(sess)
	})

	// Pump input while the countdown ticks toward the state change to the
	// character menu (and past it, when the countdown completes early).
	deadline := time.Now().Add(100 * time.Millisecond)
	for time.Now().Before(deadline) {
		h.g.HandleSessionOnOwner(sess, "notacommand")
	}
	h.barrier()
}