| 1 | package jobs |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "io" |
| 6 | "sync" |
| 7 | "testing" |
| 8 | |
| 9 | "reasonix/internal/event" |
| 10 | ) |
| 11 | |
| 12 | // TestManagerConcurrentAccess hammers every public Manager method from many |
| 13 | // goroutines. Any map access that escapes m.mu trips the runtime's built-in |
| 14 | // "concurrent map writes/read" fatal even without the race detector. |
| 15 | func TestManagerConcurrentAccess(t *testing.T) { |
| 16 | m := NewManager(event.Discard) |
| 17 | defer m.Close() |
| 18 | |
| 19 | const workers = 24 |
| 20 | var wg sync.WaitGroup |
| 21 | wg.Add(workers) |
| 22 | for w := 0; w < workers; w++ { |
| 23 | go func(w int) { |
| 24 | defer wg.Done() |
| 25 | for i := 0; i < 200; i++ { |
| 26 | switch (w + i) % 6 { |
| 27 | case 0: |
| 28 | j := m.Start("bash", "x", func(ctx context.Context, out io.Writer) (string, error) { |
| 29 | _, _ = out.Write([]byte("tick")) |
| 30 | return "done", nil |
| 31 | }) |
| 32 | _, _, _ = m.Output(j.ID) |
| 33 | case 1: |
| 34 | _ = m.Running() |
| 35 | case 2: |
| 36 | _ = m.DrainCompletedNote() |
| 37 | case 3: |
| 38 | _ = m.Wait(context.Background(), nil, 0) // non-blocking-ish: returns running snapshot |
| 39 | case 4: |
| 40 | m.Kill("bash-1") |
| 41 | case 5: |
| 42 | _, _, _ = m.Output("bash-2") |
| 43 | } |
| 44 | } |
| 45 | }(w) |
| 46 | } |
| 47 | wg.Wait() |
| 48 | } |
| 49 |