fix: add open progress staging and forced key evidence to display_diag (#3)
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -162,6 +167,190 @@ func TestFormatDiagnosticRendersStructuredFields(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// harnessOpener mirrors the real opener's diagnostic contract: it emits
|
||||
// the role's open record at Open and hands back a session that emits the
|
||||
// first-redraw record on its first flush and the summary on close — all
|
||||
// through the one shared emitter under test.
|
||||
type harnessOpener struct {
|
||||
diag *display.Diag
|
||||
role string
|
||||
session *harnessSession
|
||||
}
|
||||
|
||||
func (o harnessOpener) Open(context.Context) (display.Session, error) {
|
||||
o.diag.Emit("open", o.role, "window=0x1")
|
||||
return o.session, nil
|
||||
}
|
||||
|
||||
type harnessSession struct {
|
||||
diag *display.Diag
|
||||
role string
|
||||
firstRed sync.Once
|
||||
}
|
||||
|
||||
func (s *harnessSession) Canvas() display.Canvas { return s }
|
||||
func (s *harnessSession) Size() (int, int) { return 800, 600 }
|
||||
func (s *harnessSession) Clear() {}
|
||||
func (s *harnessSession) Text(int, int, string) {}
|
||||
func (s *harnessSession) Flush() {
|
||||
s.firstRed.Do(func() { s.diag.Emit("first_redraw", s.role, "trigger=initial") })
|
||||
}
|
||||
|
||||
func (s *harnessSession) Draw(display.Canvas) {
|
||||
s.Flush()
|
||||
}
|
||||
|
||||
func (s *harnessSession) WaitEvent(ctx context.Context) (bool, error) {
|
||||
<-ctx.Done()
|
||||
return false, ctx.Err()
|
||||
}
|
||||
|
||||
func (s *harnessSession) Close() error {
|
||||
s.diag.EmitForced("summary", s.role, "flushes=1")
|
||||
return nil
|
||||
}
|
||||
|
||||
// stalledOpener reproduces the device evidence: the open sequence emits
|
||||
// its progress markers (open_begin, the stage marker for the next
|
||||
// potentially blocking operation) and then never returns, while the other
|
||||
// layer completes normally through the same shared emitter.
|
||||
type stalledOpener struct {
|
||||
diag *display.Diag
|
||||
role string
|
||||
stall chan struct{}
|
||||
stages func(*display.Diag, string)
|
||||
}
|
||||
|
||||
func (o stalledOpener) Open(ctx context.Context) (display.Session, error) {
|
||||
o.diag.Emit("open_begin", o.role)
|
||||
o.stages(o.diag, o.role)
|
||||
<-o.stall
|
||||
return nil, fmt.Errorf("stalled open never returned")
|
||||
}
|
||||
|
||||
func TestRunLayersEmitsProgressRecordsBeforeAStalledOpen(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
diag := display.NewDiag(&buf)
|
||||
|
||||
stall := make(chan struct{})
|
||||
stalled := &harnessSession{diag: diag, role: "stalled"}
|
||||
live := &harnessSession{diag: diag, role: "canvas"}
|
||||
|
||||
progress := func(d *display.Diag, role string) {
|
||||
d.Emit("open_begin", role)
|
||||
d.Emit("open_stage", role, "stage=map_confirm")
|
||||
}
|
||||
layers := []display.Layer{
|
||||
{
|
||||
Name: "stalled-osd",
|
||||
Opener: stalledOpener{diag: diag, role: "osd", stall: stall, stages: progress},
|
||||
Template: stalled,
|
||||
},
|
||||
{
|
||||
Name: "fallback-canvas",
|
||||
Opener: harnessOpener{diag: diag, role: "canvas", session: live},
|
||||
Template: live,
|
||||
},
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
display.RunLayers(ctx, layers)
|
||||
close(done)
|
||||
}()
|
||||
|
||||
// The stall evidence must already be visible while the stalled open is
|
||||
// in progress, and the collector must stay bounded.
|
||||
waitForRecords(t, diag, func(records []string) bool {
|
||||
return hasDiag(records, "kind=open_begin", "role=osd") &&
|
||||
hasDiag(records, "kind=open_stage", "role=osd", "stage=map_confirm") &&
|
||||
hasDiag(records, "kind=open", "role=canvas") &&
|
||||
hasDiag(records, "kind=first_redraw", "role=canvas")
|
||||
})
|
||||
if len(diag.Records()) > 70 {
|
||||
t.Fatalf("collector unbounded: %d distinct records", len(diag.Records()))
|
||||
}
|
||||
|
||||
close(stall)
|
||||
cancel()
|
||||
<-done
|
||||
}
|
||||
|
||||
func TestRunLayersCarriesOneSharedDiagToBothRoles(t *testing.T) {
|
||||
// The value-copy regression pin: the same emitter wired into both
|
||||
// real target layer structs must survive the RunLayers boundary so
|
||||
// both roles' open, first-redraw and summary records reach one
|
||||
// writer. Mirrors the real layer flow: the template draws through the
|
||||
// session canvas, so one session instance owns the diagnostics.
|
||||
var buf bytes.Buffer
|
||||
diag := display.NewDiag(&buf)
|
||||
|
||||
canvas := &harnessSession{diag: diag, role: "canvas"}
|
||||
osd := &harnessSession{diag: diag, role: "osd"}
|
||||
layers := []display.Layer{
|
||||
{
|
||||
Name: "fallback-canvas",
|
||||
Opener: harnessOpener{diag: diag, role: "canvas", session: canvas},
|
||||
Template: canvas,
|
||||
},
|
||||
{
|
||||
Name: "osd-overlay",
|
||||
Opener: harnessOpener{diag: diag, role: "osd", session: osd},
|
||||
Template: osd,
|
||||
},
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
display.RunLayers(ctx, layers)
|
||||
close(done)
|
||||
}()
|
||||
|
||||
waitForRecords(t, diag, func(records []string) bool {
|
||||
return hasDiag(records, "kind=open", "role=canvas") &&
|
||||
hasDiag(records, "kind=open", "role=osd") &&
|
||||
hasDiag(records, "kind=first_redraw", "role=canvas") &&
|
||||
hasDiag(records, "kind=first_redraw", "role=osd")
|
||||
})
|
||||
cancel()
|
||||
<-done
|
||||
|
||||
waitForRecords(t, diag, func(records []string) bool {
|
||||
return hasDiag(records, "kind=summary", "role=canvas") &&
|
||||
hasDiag(records, "kind=summary", "role=osd")
|
||||
})
|
||||
}
|
||||
|
||||
// hasDiag reports whether one captured record line carries both tokens.
|
||||
func hasDiag(records []string, tokens ...string) bool {
|
||||
for _, line := range records {
|
||||
match := true
|
||||
for _, token := range tokens {
|
||||
if !strings.Contains(line, token) {
|
||||
match = false
|
||||
}
|
||||
}
|
||||
if match {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func waitForRecords(t *testing.T, diag *display.Diag, satisfied func([]string) bool) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
if satisfied(diag.Records()) {
|
||||
return
|
||||
}
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
t.Fatalf("records never reached the shared writer in the required combination: %v", diag.Records())
|
||||
}
|
||||
|
||||
func TestWireDiagnosticsTagsEveryLayerWithItsName(t *testing.T) {
|
||||
layers, err := buildTarget("x11", filepath.Join(t.TempDir(), "osd-state.bin"), nil)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user