| 1 | package coordinator |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 5 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 6 | "context" |
| 7 | "testing" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | type stoppingLauncher struct { |
| 12 | *runtimeLauncher |
| 13 | started, closing, contained chan struct{} |
| 14 | } |
| 15 | |
| 16 | func (l *stoppingLauncher) StartTurn(ctx context.Context, s provider.Session, v provider.Turn) error { |
| 17 | err := l.runtimeLauncher.StartTurn(ctx, s, v) |
| 18 | close(l.started) |
| 19 | return err |
| 20 | } |
| 21 | func (l *stoppingLauncher) Close(ctx context.Context, s provider.Session) error { |
| 22 | close(l.closing) |
| 23 | select { |
| 24 | case <-l.contained: |
| 25 | return nil |
| 26 | case <-ctx.Done(): |
| 27 | return ctx.Err() |
| 28 | } |
| 29 | } |
| 30 | func TestCoordinatorStopContains(t *testing.T) { |
| 31 | l := &stoppingLauncher{runtimeLauncher: &runtimeLauncher{hold: true}, started: make(chan struct{}), closing: make(chan struct{}), contained: make(chan struct{})} |
| 32 | r, err := New(Config{Launcher: l, Tools: runtimeTools{}, OpenLaunch: func(_ context.Context, c runnerproto.CoordinatorClaim) (provider.Launch, error) { |
| 33 | return runtimeLaunch(c), nil |
| 34 | }, Record: func(context.Context, provider.Event) error { return nil }}) |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | done := make(chan error, 1) |
| 39 | go func() { done <- r.RunTurn(context.Background(), runtimeClaim(t)) }() |
| 40 | <-l.started |
| 41 | stopped := make(chan error, 1) |
| 42 | go func() { stopped <- r.Stop(context.Background()) }() |
| 43 | <-l.closing |
| 44 | select { |
| 45 | case <-stopped: |
| 46 | t.Fatal("stop returned before containment") |
| 47 | case <-time.After(20 * time.Millisecond): |
| 48 | } |
| 49 | close(l.contained) |
| 50 | if err = <-stopped; err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | if err = <-done; err == nil { |
| 54 | t.Fatal("interruption reported complete") |
| 55 | } |
| 56 | if err = r.RunTurn(context.Background(), runtimeClaim(t)); err == nil { |
| 57 | t.Fatal("stopped runtime accepted late work") |
| 58 | } |
| 59 | } |
| 60 | |