| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | func TestBlankReuseWaitsForPendingCanonicalCreateInsteadOfCancellingIt(t *testing.T) { |
| 9 | isolateDesktopUserDirs(t) |
| 10 | app := NewApp() |
| 11 | t.Cleanup(app.closeSessionServices) |
| 12 | done, cancelled := make(chan struct{}), make(chan struct{}, 1) |
| 13 | tab := &WorkspaceTab{ID: "starting-blank", Scope: "global", model: "fixture/model", buildDone: done, |
| 14 | buildGeneration: 1, buildDoneGen: 1, buildCancel: func() { cancelled <- struct{}{} }} |
| 15 | app.tabs[tab.ID], app.tabOrder, app.activeTabID = tab, []string{tab.ID}, tab.ID |
| 16 | result := make(chan error, 1) |
| 17 | go func() { result <- app.alignReusableBlankTabModel(tab, "fixture/model") }() |
| 18 | // Keep the original publisher at the durable-content -> registry boundary. |
| 19 | // It must finish before reuse can return; cancellation here reproduces the |
| 20 | // pending create that used to become an extra empty row after restart. |
| 21 | select { |
| 22 | case <-cancelled: |
| 23 | t.Error("reuse cancelled an identical in-flight canonical create") |
| 24 | case err := <-result: |
| 25 | t.Fatalf("reuse returned before its original publisher completed: %v", err) |
| 26 | case <-time.After(100 * time.Millisecond): |
| 27 | } |
| 28 | app.mu.Lock() |
| 29 | tab.Ctrl, tab.SessionID, tab.Ready = &activationStubController{}, "original-pending-session", true |
| 30 | if tab.buildDone == done { |
| 31 | close(done) |
| 32 | tab.buildDone = nil |
| 33 | } |
| 34 | app.mu.Unlock() |
| 35 | select { |
| 36 | case err := <-result: |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | case <-time.After(5 * time.Second): |
| 41 | t.Fatal("reuse did not resume after canonical publication") |
| 42 | } |
| 43 | if tab.SessionID != "original-pending-session" || tab.buildGeneration != 1 { |
| 44 | t.Fatalf("reuse replaced the original create: session=%s generation=%d", tab.SessionID, tab.buildGeneration) |
| 45 | } |
| 46 | } |
| 47 |