| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "sync" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestManualCreationShutdownFailureRetainsResourcesAndRetries(t *testing.T) { |
| 10 | a := newManualSessionTestApp(t) |
| 11 | tracker := lifecycleTrackerForTest(t, t.TempDir(), 4243, "manual-creation-stop") |
| 12 | if err := tracker.start(); err != nil { |
| 13 | t.Fatal(err) |
| 14 | } |
| 15 | a.lifecycle.tracker = tracker |
| 16 | t.Cleanup(tracker.stopWriter) |
| 17 | v, _ := seedManualCreation(t, a, "shutdown-creation-owner", "starting") |
| 18 | m := a.creationManager() |
| 19 | entered, release := make(chan struct{}), make(chan struct{}) |
| 20 | var once sync.Once |
| 21 | defer once.Do(func() { close(release) }) |
| 22 | m.execute = func(context.Context, ManualSessionCreationView, func(string)) error { |
| 23 | close(entered) |
| 24 | <-release |
| 25 | return nil |
| 26 | } |
| 27 | m.Ensure(v.OperationID, "recovery", "") |
| 28 | <-entered |
| 29 | request := shutdownRequest{RequestID: "manual-creation-stop", Reason: shutdownReasonUserQuit} |
| 30 | status, err := a.requestShutdown(context.Background(), request) |
| 31 | if err == nil || status.Completed || !status.Retryable || status.ErrorCode != "manual_creation_stop_timeout" { |
| 32 | t.Fatalf("shutdown=%+v %v", status, err) |
| 33 | } |
| 34 | coordinator := a.shutdownState() |
| 35 | coordinator.mu.Lock() |
| 36 | closed := coordinator.finished["session-ui"] || coordinator.finished["manual-session-creation"] |
| 37 | coordinator.mu.Unlock() |
| 38 | if closed { |
| 39 | t.Fatal("unfinished teardown was checkpointed") |
| 40 | } |
| 41 | if _, err := a.sessionUIStore().Get(t.Context(), "creation", v.OperationID); err != nil { |
| 42 | t.Fatal("database closed before worker termination", err) |
| 43 | } |
| 44 | once.Do(func() { close(release) }) |
| 45 | status, err = a.requestShutdown(context.Background(), request) |
| 46 | if err != nil || !status.Completed { |
| 47 | t.Fatalf("retry=%+v %v", status, err) |
| 48 | } |
| 49 | } |
| 50 |