Files
zapret-autotune/cmd/zapret-autotune/player_cancellation_test.go
T
ZeusinaandGPT-6 Bot e270810f33 refactor: execute adapter plans through shared core
Execute the private YouTube qualification plan through core-owned scheduling while retaining worker cleanup, proof sealing and legacy policy boundaries. Preserve unsafe collector failures alongside concurrent cancellation or deadlines.

Send bounded Player JSON through stdin instead of argv and update all shared runner callers. Generic controller cutover and remote runtime acceptance remain pending.

Co-authored-by: GPT-6 <gpt-6@openai.com>
2026-10-15 17:25:12 +03:00

201 lines
7.3 KiB
Go

package main
import (
"bytes"
"context"
"errors"
"io"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"testing"
"time"
)
// A real child, with no network: readiness triggers parent cancellation only
// after exec has started, so the request returns a genuine signal ExitError.
func TestPlayerCancellationChild(t *testing.T) {
if os.Getenv("AUTOTUNE_PLAYER_CANCEL_CHILD") != "1" {
return
}
io.WriteString(os.Stdout, "ready")
time.Sleep(time.Hour)
os.Exit(0)
}
type playerCancelWriter struct{ cancel context.CancelFunc }
func (w playerCancelWriter) Write(p []byte) (int, error) {
w.cancel()
return len(p), nil
}
func canceledPlayerProcess(t *testing.T, cancel context.CancelFunc, extra error, overflow string) playerRunner {
t.Helper()
return func(ctx context.Context, _ []string, _ io.Reader, out, diagnostics io.Writer) error {
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestPlayerCancellationChild$")
cmd.Env = append(os.Environ(), "AUTOTUNE_PLAYER_CANCEL_CHILD=1")
cmd.Stdout = playerCancelWriter{cancel: cancel}
cmd.Stderr = diagnostics
if overflow == "body" {
out.Write(make([]byte, playerResponseLimit+1))
} else if overflow == "diagnostics" {
diagnostics.Write(make([]byte, (64<<10)+1))
}
return errors.Join(cmd.Run(), extra)
}
}
func TestPlayerRequestCancellationCause(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
runner := canceledPlayerProcess(t, cancel, nil, "")
_, err := requestPlayer("abcdefghijk", "socks5://127.0.0.1:11080", func(_ context.Context, args []string, stdin io.Reader, out, diagnostics io.Writer) error {
return runner(ctx, args, stdin, out, diagnostics)
})
var child *exec.ExitError
if !errors.As(err, &child) || !managedRecoveryTimeoutSafe(err) {
t.Fatalf("child cause lost: %v", err)
}
if !managedCancellationOnly(managedRecoveryContextError(ctx, err), context.Canceled) {
t.Fatal("clean canceled child classified as unsafe")
}
}
func TestPlayerRequestPublicErrorsOpaque(t *testing.T) {
const secret = "https://user:password@example.com/video?signature=private"
cause := errors.New(secret)
runner := func(_ context.Context, args []string, stdin io.Reader, _, diagnostics io.Writer) error {
payload, err := io.ReadAll(stdin)
if err != nil || !bytes.Contains(payload, []byte("abcdefghijk")) || strings.Contains(strings.Join(args, " "), "abcdefghijk") {
t.Fatal("private POST body missing from stdin or exposed in argv")
}
io.WriteString(diagnostics, secret)
diagnostics.Write(payload)
return errors.Join(cause, errors.New(string(payload)))
}
_, err := requestPlayer("abcdefghijk", "socks5://127.0.0.1:11080", runner)
if !errors.Is(err, cause) || strings.Contains(err.Error(), secret) || strings.Contains(err.Error(), "abcdefghijk") || managedRecoveryTimeoutSafe(err) {
t.Fatal("opaque error lost its unsafe cause or exposed it")
}
t.Setenv("ZAPRET_AUTOTUNE_VIDEO_ID", "abcdefghijk")
for _, command := range []string{"health", "discovery"} {
var out, diagnostics bytes.Buffer
code := 0
if command == "health" {
code = healthYoutube(&out, &diagnostics, runner)
} else {
code = discoverYoutube([]string{"--video-id", "abcdefghijk", "--proxy", "socks5://127.0.0.1:11080"}, &out, &diagnostics, runner)
}
if code != 1 || out.Len() != 0 || strings.Contains(diagnostics.String(), secret) || strings.Contains(diagnostics.String(), "abcdefghijk") || !strings.Contains(diagnostics.String(), "player request failed") {
t.Fatalf("%s: code=%d diagnostics=%q", command, code, diagnostics.String())
}
}
}
func TestManagedMonitorPlayerCancellationExit(t *testing.T) {
for _, active := range []bool{false, true} {
for _, failure := range []string{"", "body", "diagnostics", "mixed", "guard"} {
t.Run(strings.Join([]string{map[bool]string{false: "baseline", true: "active"}[active], failure}, "/"), func(t *testing.T) {
d, env, _ := managedCLIFixture(t)
initManagedCLI(t, d)
env["ZAPRET_AUTOTUNE_DB"] = managedTargetDirectory + "/candidates.db"
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
var extra error
if failure == "mixed" {
extra = errors.New("private I/O failure")
}
d.player = canceledPlayerProcess(t, cancel, extra, failure)
var output bytes.Buffer
code := managedMonitorCLIContext(ctx, nil, &output, &output, d, func(ctx context.Context, m managedMonitorDeps) error {
if active {
id := managedCandidateFixture(t, m.db, "frontend", "youtube.com", "--lua-desync=fake", true)
transaction := managedTransaction{Dir: d.directory, Name: "config", Reload: func() error { return nil }, Health: func() (string, error) { return "OK", nil }}
if err := transaction.Apply(m.db, []int64{id}); err != nil {
t.Fatal(err)
}
}
if failure == "guard" {
cancel()
delete(env, "ZAPRET_CONFIG")
}
return m.probe(ctx)
})
want := 0
if failure != "" {
want = 78
}
if code != want || strings.Contains(output.String(), "private I/O failure") {
t.Fatalf("exit=%d want=%d output=%q", code, want, output.String())
}
})
}
}
}
func TestManagedMonitorSIGTERMChild(t *testing.T) {
mode := os.Getenv("AUTOTUNE_MONITOR_SIGTERM_CHILD")
if mode == "" {
return
}
d, env, _ := managedCLIFixture(t)
initManagedCLI(t, d)
env["ZAPRET_AUTOTUNE_DB"] = managedTargetDirectory + "/candidates.db"
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM)
defer cancel()
d.player = func(ctx context.Context, _ []string, _ io.Reader, _, diagnostics io.Writer) error {
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestPlayerCancellationChild$")
cmd.Env = append(os.Environ(), "AUTOTUNE_PLAYER_CANCEL_CHILD=1")
cmd.Stdout, cmd.Stderr = os.Stdout, diagnostics
return cmd.Run()
}
code := managedMonitorCLIContext(ctx, nil, os.Stdout, os.Stderr, d, func(ctx context.Context, m managedMonitorDeps) error {
if mode == "active" {
id := managedCandidateFixture(t, m.db, "frontend", "youtube.com", "--lua-desync=fake", true)
transaction := managedTransaction{Dir: d.directory, Name: "config", Reload: func() error { return nil }, Health: func() (string, error) { return "OK", nil }}
if err := transaction.Apply(m.db, []int64{id}); err != nil {
t.Fatal(err)
}
}
return m.probe(ctx)
})
if code != 0 {
t.Fatalf("SIGTERM exit=%d", code)
}
}
type playerSignalWriter struct{ command *exec.Cmd }
func (w playerSignalWriter) Write(p []byte) (int, error) {
if strings.Contains(string(p), "ready") {
if err := w.command.Process.Signal(syscall.SIGTERM); err != nil {
return 0, err
}
}
return len(p), nil
}
func TestManagedMonitorExternalSIGTERM(t *testing.T) {
for _, mode := range []string{"baseline", "active"} {
t.Run(mode, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestManagedMonitorSIGTERMChild$")
cmd.Env = append(os.Environ(), "AUTOTUNE_MONITOR_SIGTERM_CHILD="+mode)
var diagnostics bytes.Buffer
cmd.Stderr = &diagnostics
// The writer observes readiness after Start has assigned Process.
cmd.Stdout = playerSignalWriter{command: cmd}
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
if err := cmd.Wait(); err != nil {
t.Fatalf("SIGTERM failed: %v %s", err, diagnostics.String())
}
})
}
}