Files
magpie/internal/gui/warm_turns_test.go
T
yetone 647323409e gui tests: TestColdTurnsTaken asks once the warm-up holds its turn (#4510)
The test closed `asked` and asked at once, while the warm-up's
goroutine might not have taken its turn yet. The page then took the
turn first, a right order, and the test called it "answered while the
warm-up's step read". Its t.Fatal left the step blocked holding the
turn, so with -count above 1 every later run waited to the timeout.

The step now closes `holding` once it has its turn, and the page asks
after that. sessions.go is unchanged.

Run (mote): go test -tags nogui ./internal/gui -run
'TestColdTurnsTaken$' -count=200 -cpu 1,2,8 -race ok (35.7s). Broken
by hand: coldTurn not taking a turn turns it red at once.
2026-11-01 15:33:35 +08:00

77 lines
1.8 KiB
Go

package gui
import (
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
)
// Until the warm-up is over, a page's first ask and each of its reads
// take turns: none holds a first read of the history beside another
// (#2278). Once it is over, a page asks at once.
func TestColdTurnsTaken(t *testing.T) {
coldTurnsTaken = true
coldTurns.over.Store(false)
t.Cleanup(func() { coldTurnsTaken = false; coldTurns.over.Store(false) })
var inStep, inPage, both atomic.Int32
// the warm-up holds its turn before the page asks: asked at once, the
// page could take the turn before the warm-up's goroutine ran, a
// right order the test called wrong (#4510)
holding, release := make(chan struct{}), make(chan struct{})
page := coldTurn(func(http.ResponseWriter, *http.Request) {
inPage.Add(1)
if inStep.Load() > 0 {
both.Add(1)
}
inPage.Add(-1)
})
ask := func() {
page(httptest.NewRecorder(), httptest.NewRequest("GET", "/api/usage/heatmap", nil))
}
warmed := make(chan struct{})
go func() {
defer close(warmed)
warmUp(func() {
inStep.Add(1)
defer inStep.Add(-1)
close(holding)
<-release
}, func() {
inStep.Add(1)
defer inStep.Add(-1)
if inPage.Load() > 0 {
both.Add(1)
}
})
}()
<-holding
answered := make(chan struct{})
go func() {
ask()
close(answered)
}()
select {
case <-answered:
t.Fatal("the page was answered while the warm-up's step read")
case <-time.After(50 * time.Millisecond):
}
close(release)
<-answered
<-warmed
if both.Load() != 0 {
t.Fatal("a page and a warm-up step read at once")
}
// once the warm-up is over, nothing waits
coldTurns.turn <- struct{}{}
defer func() { <-coldTurns.turn }()
done := make(chan struct{})
go func() { ask(); close(done) }()
select {
case <-done:
case <-time.After(10 * time.Second):
t.Fatal("a page after the warm-up waited for a turn")
}
}