| 1 | package boot |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "io" |
| 6 | "reasonix/internal/control" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestRebuildBackgroundCandidateFailurePreservesOwner(t *testing.T) { |
| 11 | isolateConfigHome(t) |
| 12 | root := robustTempDir(t) |
| 13 | t.Chdir(root) |
| 14 | writeRuntimeFixture(t, root) |
| 15 | old := buildRuntimeFixture(t).Controller |
| 16 | scope := old.BackgroundScope() |
| 17 | initialGeneration := old.RuntimeOwner().Gate.Published() |
| 18 | entered := make(chan struct{}) |
| 19 | job := scope.Manager.StartSessionProcess("", "bash", "gateway", func(ctx context.Context, _ io.Writer) (string, error) { |
| 20 | close(entered) |
| 21 | <-ctx.Done() |
| 22 | return "", ctx.Err() |
| 23 | }) |
| 24 | <-entered |
| 25 | if _, err := Rebuild(t.Context(), old, Options{Model: "missing/provider"}); err == nil { |
| 26 | t.Fatal("expected invalid model failure") |
| 27 | } |
| 28 | if len(scope.Manager.Running()) != 1 { |
| 29 | t.Fatal("failed build killed gateway") |
| 30 | } |
| 31 | candidate, err := Rebuild(t.Context(), old, Options{}) |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | candidate.Controller.ReleaseResources() |
| 36 | if old.RuntimeOwner().Gate.Published() != initialGeneration { |
| 37 | t.Fatal("discarded candidate retired outgoing extension generation") |
| 38 | } |
| 39 | if len(scope.Manager.Running()) != 1 || scope.Manager.ReplacementInProgress() { |
| 40 | t.Fatal("discard did not restore outgoing ownership") |
| 41 | } |
| 42 | next, err := Rebuild(t.Context(), old, Options{}) |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | if err := control.ActivateControllerReplacement(old, next.Controller); err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | if old.RuntimeOwner().Gate.Published() != next.Controller.RuntimeGeneration() { |
| 50 | t.Fatal("activation did not publish extension generation") |
| 51 | } |
| 52 | old.ReleaseResources() |
| 53 | if len(scope.Manager.Running()) != 1 { |
| 54 | t.Fatal("old controller close killed gateway") |
| 55 | } |
| 56 | next.Controller.Close() |
| 57 | scope.Manager.Wait(t.Context(), []string{job.ID}, 10) |
| 58 | if len(scope.Manager.Running()) != 0 { |
| 59 | t.Fatal("session shutdown left gateway running") |
| 60 | } |
| 61 | } |
| 62 |