| 1 | package plugin |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestAcquireCodeGraphSlotCapsInstances(t *testing.T) { |
| 6 | // Drain any residual count from parallel tests by asserting a clean start. |
| 7 | if got := liveCodeGraphInstances.Load(); got != 0 { |
| 8 | t.Fatalf("expected 0 live instances at start, got %d", got) |
| 9 | } |
| 10 | |
| 11 | releases := make([]func(), 0, maxCodeGraphInstances) |
| 12 | for i := 0; i < maxCodeGraphInstances; i++ { |
| 13 | release, err := acquireCodeGraphSlot() |
| 14 | if err != nil { |
| 15 | t.Fatalf("acquire %d within cap should succeed: %v", i, err) |
| 16 | } |
| 17 | releases = append(releases, release) |
| 18 | } |
| 19 | |
| 20 | // One past the cap must be refused, and the counter must not leak upward. |
| 21 | if _, err := acquireCodeGraphSlot(); err == nil { |
| 22 | t.Fatal("acquiring past the cap should fail") |
| 23 | } |
| 24 | if got := liveCodeGraphInstances.Load(); got != int32(maxCodeGraphInstances) { |
| 25 | t.Fatalf("count must stay at cap after a refused acquire, got %d", got) |
| 26 | } |
| 27 | |
| 28 | // Releasing frees a slot, and release is idempotent. |
| 29 | releases[0]() |
| 30 | releases[0]() |
| 31 | if got := liveCodeGraphInstances.Load(); got != int32(maxCodeGraphInstances-1) { |
| 32 | t.Fatalf("double-release must free exactly one slot, got %d", got) |
| 33 | } |
| 34 | release, err := acquireCodeGraphSlot() |
| 35 | if err != nil { |
| 36 | t.Fatalf("a freed slot should be reusable: %v", err) |
| 37 | } |
| 38 | releases[0] = release |
| 39 | |
| 40 | for _, r := range releases { |
| 41 | r() |
| 42 | } |
| 43 | if got := liveCodeGraphInstances.Load(); got != 0 { |
| 44 | t.Fatalf("all slots must be freed at end, got %d", got) |
| 45 | } |
| 46 | } |
| 47 |