| 1 | package extension |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "sync/atomic" |
| 7 | "testing" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | func TestEffectScopeDisposeIdempotentAndReverse(t *testing.T) { |
| 12 | var order []string |
| 13 | s := NewEffectScope(1) |
| 14 | for _, id := range []string{"a", "b", "c"} { |
| 15 | if err := s.Track(Effect{ |
| 16 | ID: id, Class: Reversible, |
| 17 | Dispose: func(context.Context) error { |
| 18 | order = append(order, id) |
| 19 | return nil |
| 20 | }, |
| 21 | }); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | } |
| 25 | if err := s.Dispose(context.Background()); err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | if got := order; len(got) != 3 || got[0] != "c" || got[1] != "b" || got[2] != "a" { |
| 29 | t.Fatalf("order = %v, want [c b a]", got) |
| 30 | } |
| 31 | if err := s.Dispose(context.Background()); err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | if len(order) != 3 { |
| 35 | t.Fatalf("second dispose re-ran effects: %v", order) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestEffectScopeActivationMidFailureCleanup(t *testing.T) { |
| 40 | var closed atomic.Int32 |
| 41 | s := NewEffectScope(2) |
| 42 | if err := s.Track(Effect{ |
| 43 | ID: "sidecar", Class: Reversible, |
| 44 | Dispose: func(context.Context) error { |
| 45 | closed.Add(1) |
| 46 | return nil |
| 47 | }, |
| 48 | }); err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | // Simulate activation failure: dispose the partially activated scope. |
| 52 | if err := s.Dispose(context.Background()); err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | if closed.Load() != 1 { |
| 56 | t.Fatalf("closed = %d, want 1", closed.Load()) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestEffectScopeCleanupErrorAggregation(t *testing.T) { |
| 61 | boom := errors.New("boom") |
| 62 | var order []string |
| 63 | s := NewEffectScope(1) |
| 64 | _ = s.Track(Effect{ID: "ok", Class: Reversible, Dispose: func(context.Context) error { |
| 65 | order = append(order, "ok") |
| 66 | return nil |
| 67 | }}) |
| 68 | _ = s.Track(Effect{ID: "bad", Class: Reversible, Dispose: func(context.Context) error { |
| 69 | order = append(order, "bad") |
| 70 | return boom |
| 71 | }}) |
| 72 | err := s.Dispose(context.Background()) |
| 73 | if !errors.Is(err, boom) { |
| 74 | t.Fatalf("err = %v, want boom", err) |
| 75 | } |
| 76 | if len(order) != 2 { |
| 77 | t.Fatalf("order = %v, both must run", order) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestEffectScopeContextCancellation(t *testing.T) { |
| 82 | s := NewEffectScope(1) |
| 83 | ctx, cancel := context.WithCancel(context.Background()) |
| 84 | cancel() |
| 85 | started := make(chan struct{}) |
| 86 | _ = s.Track(Effect{ |
| 87 | ID: "bg", Class: Cancelable, |
| 88 | Dispose: func(c context.Context) error { |
| 89 | close(started) |
| 90 | select { |
| 91 | case <-c.Done(): |
| 92 | return c.Err() |
| 93 | case <-time.After(time.Second): |
| 94 | return errors.New("did not observe cancel") |
| 95 | } |
| 96 | }, |
| 97 | }) |
| 98 | err := s.Dispose(ctx) |
| 99 | if !errors.Is(err, context.Canceled) { |
| 100 | t.Fatalf("err = %v, want context.Canceled", err) |
| 101 | } |
| 102 | select { |
| 103 | case <-started: |
| 104 | default: |
| 105 | t.Fatal("dispose did not run") |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func TestEffectScopeIrreversibleReceipt(t *testing.T) { |
| 110 | s := NewEffectScope(9) |
| 111 | _ = s.Track(Effect{ |
| 112 | ID: "msg-send", Owner: "plugin/x", Component: "x", Class: Irreversible, |
| 113 | Dispose: func(context.Context) error { return nil }, |
| 114 | }) |
| 115 | if err := s.Dispose(context.Background()); err != nil { |
| 116 | t.Fatal(err) |
| 117 | } |
| 118 | receipts := s.Receipts() |
| 119 | if len(receipts) != 1 { |
| 120 | t.Fatalf("receipts = %d, want 1", len(receipts)) |
| 121 | } |
| 122 | r := receipts[0] |
| 123 | if r.Class != Irreversible || r.CompensationStatus != "not_applicable" || r.Generation != 9 { |
| 124 | t.Fatalf("receipt = %+v", r) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | func TestEffectScopeTrackAfterClose(t *testing.T) { |
| 129 | var closed atomic.Int32 |
| 130 | s := NewEffectScope(1) |
| 131 | _ = s.Dispose(context.Background()) |
| 132 | if err := s.Track(Effect{ |
| 133 | ID: "late", Class: Reversible, |
| 134 | Dispose: func(context.Context) error { |
| 135 | closed.Add(1) |
| 136 | return nil |
| 137 | }, |
| 138 | }); err != nil { |
| 139 | t.Fatal(err) |
| 140 | } |
| 141 | if closed.Load() != 1 { |
| 142 | t.Fatal("late track must dispose immediately") |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | func TestEffectScopeDuplicateID(t *testing.T) { |
| 147 | s := NewEffectScope(1) |
| 148 | _ = s.Track(Effect{ID: "x", Class: Reversible, Dispose: func(context.Context) error { return nil }}) |
| 149 | if err := s.Track(Effect{ID: "x", Class: Reversible, Dispose: func(context.Context) error { return nil }}); err == nil { |
| 150 | t.Fatal("duplicate id accepted") |
| 151 | } |
| 152 | } |
| 153 |