| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestTopicMutationBudgetStartsAfterStorePreparation(t *testing.T) { |
| 10 | isolateDesktopUserDirs(t) |
| 11 | manager := newTopicStateManager() |
| 12 | t.Cleanup(manager.close) |
| 13 | root := t.TempDir() |
| 14 | var operation context.Context |
| 15 | manager.operationContext = func() (context.Context, context.CancelFunc) { |
| 16 | // Simulate a cold-open phase consuming the pre-existing deadline. A |
| 17 | // budget created before the store is prepared must already be expired. |
| 18 | ctx, cancel := context.WithCancel(context.Background()) |
| 19 | operation = ctx |
| 20 | if manager.scope(root).store == nil { |
| 21 | cancel() |
| 22 | } |
| 23 | return ctx, cancel |
| 24 | } |
| 25 | if err := manager.setTitle(root, "topic-budget", "Prepared first", topicTitleSourceManual); err != nil { |
| 26 | t.Fatalf("title mutation received a pre-preparation deadline: %v", err) |
| 27 | } |
| 28 | if operation == nil || !errors.Is(operation.Err(), context.Canceled) { |
| 29 | t.Fatal("completed mutation did not release its context") |
| 30 | } |
| 31 | snapshot, err := manager.snapshot(root) |
| 32 | if err != nil || snapshot.Records["topic-budget"].Title != "Prepared first" { |
| 33 | t.Fatalf("title was not committed: %+v, %v", snapshot, err) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestTopicMutationStillHonorsExpiredOperationBudget(t *testing.T) { |
| 38 | isolateDesktopUserDirs(t) |
| 39 | manager := newTopicStateManager() |
| 40 | t.Cleanup(manager.close) |
| 41 | manager.operationContext = func() (context.Context, context.CancelFunc) { |
| 42 | ctx, cancel := context.WithCancel(context.Background()) |
| 43 | cancel() |
| 44 | return ctx, cancel |
| 45 | } |
| 46 | root := t.TempDir() |
| 47 | err := manager.setTitle(root, "cancelled", "must not commit", topicTitleSourceManual) |
| 48 | if !errors.Is(err, context.Canceled) { |
| 49 | t.Fatalf("expired mutation budget was ignored: %v", err) |
| 50 | } |
| 51 | snapshot, err := manager.snapshot(root) |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | if _, exists := snapshot.Records["cancelled"]; exists { |
| 56 | t.Fatal("cancelled mutation committed a title") |
| 57 | } |
| 58 | } |
| 59 |