| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestSessionOpenKeepsPublishedControllerContextAlive(t *testing.T) { |
| 10 | app, _, target, _, _ := canonicalWorkspaceOpenFixture(t) |
| 11 | appCtx, cancelApp := context.WithCancel(app.ctx) |
| 12 | t.Cleanup(cancelApp) |
| 13 | app.ctx = appCtx |
| 14 | var buildCtx context.Context |
| 15 | app.sessionOpenBuildHook = func(ctx context.Context) { buildCtx = ctx } |
| 16 | if _, err := app.OpenSession(target.Ref()); err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | if buildCtx == nil || buildCtx.Err() != nil { |
| 20 | t.Fatalf("successful open cancelled the controller lifetime: %v", buildCtx) |
| 21 | } |
| 22 | app.cancelSessionNavigation() |
| 23 | if err := buildCtx.Err(); err != nil { |
| 24 | t.Fatalf("later navigation cancellation stopped the published controller: %v", err) |
| 25 | } |
| 26 | cancelApp() |
| 27 | if !errors.Is(buildCtx.Err(), context.Canceled) { |
| 28 | t.Fatal("published controller lost the application lifetime") |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestSessionOpenRejectsAdmissionAfterShutdownCancellation(t *testing.T) { |
| 33 | app, tab, target, _, _ := canonicalWorkspaceOpenFixture(t) |
| 34 | previous := tab.Ctrl |
| 35 | app.shuttingDown.Store(true) |
| 36 | app.cancelSessionNavigation() |
| 37 | if _, err := app.OpenSession(target.Ref()); err == nil { |
| 38 | t.Fatal("session open was admitted after shutdown cancellation") |
| 39 | } |
| 40 | if tab.Ctrl != previous { |
| 41 | t.Fatal("late navigation replaced the shutdown controller") |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func TestStaleSessionResumeDoesNotCancelCurrentNavigation(t *testing.T) { |
| 46 | app, tab, target, _, _ := canonicalWorkspaceOpenFixture(t) |
| 47 | stale := app.desktopSessions.navigationSeq.Add(1) |
| 48 | app.desktopSessions.navigationSeq.Add(1) |
| 49 | ctx, finish := app.beginSessionNavigationContext() |
| 50 | defer finish() |
| 51 | _, err := app.resumeCanonicalSessionForTranscript(tab, tab.Ctrl, sessionRoute(target.Ref().SessionID), 32, false, stale) |
| 52 | if !errors.Is(err, errSessionNavigationSuperseded) { |
| 53 | t.Fatalf("stale resume = %v", err) |
| 54 | } |
| 55 | if err := ctx.Err(); err != nil { |
| 56 | t.Fatalf("stale request cancelled the newer navigation: %v", err) |
| 57 | } |
| 58 | } |
| 59 |