package coordinator import ( "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/runnerproto" "context" "testing" "time" ) type stoppingLauncher struct { *runtimeLauncher started, closing, contained chan struct{} } func (l *stoppingLauncher) StartTurn(ctx context.Context, s provider.Session, v provider.Turn) error { err := l.runtimeLauncher.StartTurn(ctx, s, v) close(l.started) return err } func (l *stoppingLauncher) Close(ctx context.Context, s provider.Session) error { close(l.closing) select { case <-l.contained: return nil case <-ctx.Done(): return ctx.Err() } } func TestCoordinatorStopContains(t *testing.T) { l := &stoppingLauncher{runtimeLauncher: &runtimeLauncher{hold: true}, started: make(chan struct{}), closing: make(chan struct{}), contained: make(chan struct{})} r, err := New(Config{Launcher: l, Tools: runtimeTools{}, OpenLaunch: func(_ context.Context, c runnerproto.CoordinatorClaim) (provider.Launch, error) { return runtimeLaunch(c), nil }, Record: func(context.Context, provider.Event) error { return nil }}) if err != nil { t.Fatal(err) } done := make(chan error, 1) go func() { done <- r.RunTurn(context.Background(), runtimeClaim(t)) }() <-l.started stopped := make(chan error, 1) go func() { stopped <- r.Stop(context.Background()) }() <-l.closing select { case <-stopped: t.Fatal("stop returned before containment") case <-time.After(20 * time.Millisecond): } close(l.contained) if err = <-stopped; err != nil { t.Fatal(err) } if err = <-done; err == nil { t.Fatal("interruption reported complete") } if err = r.RunTurn(context.Background(), runtimeClaim(t)); err == nil { t.Fatal("stopped runtime accepted late work") } }