Make state single-owner and stabilize race detector
Frame now carries view state (scale/focus/query) to the main goroutine, which reads only the frame-receiver-stored snapshot. Renderer owns Gio widget editors (registered by ID) and the draw scale. Autosave timer sends a token to the logic goroutine instead of touching state; Shutdown waits for the owner to exit before FlushAll. Tests access state only through owner-side Inspect/WithState helpers (harness + in-package). Fixed double-Harness.Run race in two e2e tests, made TestWorkerPool_PriorityPreemption deterministic, and raised the lazy-loading test timeout that was too short under -race. go test -race ./... is now green.
This commit is contained in:
@@ -156,19 +156,39 @@ func TestWorkerPool_PriorityPreemption(t *testing.T) {
|
||||
var lowExecuted atomic.Bool
|
||||
var highExecuted atomic.Bool
|
||||
|
||||
// Low priority task that takes time
|
||||
// Gate task: occupies the single worker until we release it, so that the
|
||||
// low- and high-priority test tasks both sit in their queues first.
|
||||
gateStarted := make(chan struct{})
|
||||
release := make(chan struct{})
|
||||
gateTask := &stubTask{
|
||||
id: "gate",
|
||||
taskType: TypeSaveState,
|
||||
priority: HighPriority,
|
||||
execute: func() (any, error) {
|
||||
close(gateStarted)
|
||||
<-release
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
pool.Dispatch(gateTask)
|
||||
|
||||
// Wait until the worker is inside the gate task.
|
||||
select {
|
||||
case <-gateStarted:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("Timed out waiting for gate task to start")
|
||||
}
|
||||
|
||||
// Queue a low-priority task, then a high-priority one.
|
||||
lowTask := &stubTask{
|
||||
id: "low",
|
||||
taskType: TypeSaveState,
|
||||
priority: LowPriority,
|
||||
execute: func() (any, error) {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
lowExecuted.Store(true)
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
|
||||
// High priority task that completes quickly
|
||||
highTask := &stubTask{
|
||||
id: "high",
|
||||
taskType: TypeReadFile,
|
||||
@@ -178,34 +198,43 @@ func TestWorkerPool_PriorityPreemption(t *testing.T) {
|
||||
return "fast", nil
|
||||
},
|
||||
}
|
||||
|
||||
// Dispatch low priority first
|
||||
pool.Dispatch(lowTask)
|
||||
|
||||
// Immediately dispatch high priority
|
||||
pool.Dispatch(highTask)
|
||||
|
||||
// Wait for high priority result (should complete before low)
|
||||
// Release the worker; it must pick the high-priority task first.
|
||||
close(release)
|
||||
|
||||
// Gate result first.
|
||||
select {
|
||||
case result := <-pool.resultChan:
|
||||
if result.TaskID != "gate" {
|
||||
t.Fatalf("First result TaskID = %q, want %q", result.TaskID, "gate")
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("Timed out waiting for gate result")
|
||||
}
|
||||
|
||||
// High priority result before low priority.
|
||||
select {
|
||||
case result := <-pool.resultChan:
|
||||
if result.TaskID != "high" {
|
||||
t.Errorf("First result TaskID = %q, want %q", result.TaskID, "high")
|
||||
t.Errorf("Second result TaskID = %q, want %q", result.TaskID, "high")
|
||||
}
|
||||
if !highExecuted.Load() {
|
||||
t.Error("High priority task should have executed first")
|
||||
t.Error("High priority task should have executed before low")
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("Timed out waiting for high priority result")
|
||||
}
|
||||
|
||||
// Wait for low priority result
|
||||
// Low priority result last.
|
||||
select {
|
||||
case result := <-pool.resultChan:
|
||||
if result.TaskID != "low" {
|
||||
t.Errorf("Second result TaskID = %q, want %q", result.TaskID, "low")
|
||||
t.Errorf("Third result TaskID = %q, want %q", result.TaskID, "low")
|
||||
}
|
||||
if !lowExecuted.Load() {
|
||||
t.Error("Low priority task should have executed second")
|
||||
t.Error("Low priority task should have executed last")
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("Timed out waiting for low priority result")
|
||||
|
||||
Reference in New Issue
Block a user