| 1 | package acp |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "sync" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "reasonix/internal/control" |
| 10 | "reasonix/internal/event" |
| 11 | "reasonix/internal/jobs" |
| 12 | ) |
| 13 | |
| 14 | type teardownFactory struct { |
| 15 | dir string |
| 16 | grace time.Duration |
| 17 | mu sync.Mutex |
| 18 | manager *jobs.Manager |
| 19 | timeoutDetails []string |
| 20 | } |
| 21 | |
| 22 | func (f *teardownFactory) SessionDir() string { return f.dir } |
| 23 | |
| 24 | func (f *teardownFactory) NewSession(_ context.Context, p SessionParams) (*control.Controller, error) { |
| 25 | sink := event.FuncSink(func(e event.Event) { |
| 26 | if e.Text != "Background job teardown timed out." { |
| 27 | return |
| 28 | } |
| 29 | f.mu.Lock() |
| 30 | f.timeoutDetails = append(f.timeoutDetails, e.Detail) |
| 31 | f.mu.Unlock() |
| 32 | }) |
| 33 | jm := jobs.NewManager(sink, jobs.WithTeardownGrace(f.grace)) |
| 34 | f.mu.Lock() |
| 35 | f.manager = jm |
| 36 | f.mu.Unlock() |
| 37 | runner := &fakeRunner{ |
| 38 | sink: p.Sink, |
| 39 | behavior: func(context.Context, event.Sink, string) error { return nil }, |
| 40 | } |
| 41 | return control.New(control.Options{ |
| 42 | Runner: runner, |
| 43 | Sink: p.Sink, |
| 44 | SessionDir: f.dir, |
| 45 | Jobs: jm, |
| 46 | }), nil |
| 47 | } |
| 48 | |
| 49 | func (f *teardownFactory) lastManager(t *testing.T) *jobs.Manager { |
| 50 | t.Helper() |
| 51 | f.mu.Lock() |
| 52 | defer f.mu.Unlock() |
| 53 | if f.manager == nil { |
| 54 | t.Fatal("session manager was not created") |
| 55 | } |
| 56 | return f.manager |
| 57 | } |
| 58 | |
| 59 | func (f *teardownFactory) teardownTimeoutDetails() []string { |
| 60 | f.mu.Lock() |
| 61 | defer f.mu.Unlock() |
| 62 | return append([]string(nil), f.timeoutDetails...) |
| 63 | } |
| 64 |