TestCompactionInputConcurrent pins the data-race fix for the compactor: CompactionInput must read s.Messages under s.mu (via snapshotItems) so it stays safe against concurrent AddMessage and ApplyCompaction calls. Run with -race; without the lock the slice header read aliases the live backing array
(t *testing.T)
| 30 | // header read aliases the live backing array and the race detector |
| 31 | // flags the AddMessage append. |
| 32 | func TestCompactionInputConcurrent(t *testing.T) { |
| 33 | t.Parallel() |
| 34 | |
| 35 | s := New() |
| 36 | var wg sync.WaitGroup |
| 37 | for range 100 { |
| 38 | wg.Go(func() { |
| 39 | s.AddMessage(&Message{Message: chat.Message{Role: chat.MessageRoleUser, Content: "u"}}) |
| 40 | }) |
| 41 | wg.Go(func() { |
| 42 | _, _ = s.CompactionInput() |
| 43 | }) |
| 44 | } |
| 45 | // One concurrent ApplyCompaction-shaped write to exercise the same |
| 46 | // lock from a writer that also bumps the cumulative token counts. |
| 47 | wg.Go(func() { |
| 48 | s.ApplyCompaction(0, 0, Item{Summary: "snap"}) |
| 49 | }) |
| 50 | wg.Wait() |
| 51 | } |
nothing calls this directly
no test coverage detected