#!perl
# Tests for hot_restart failure paths:
# 1. Bad app_file on SIGHUP → rollback to old generation
# 2. max_requests_per_worker → worker auto-recycles
use warnings;
use strict;
use constant TIMEOUT_MULT =>
    $ENV{PERL_TEST_TIME_OUT_FACTOR} || ($ENV{AUTOMATED_TESTING} ? 3 : 1);
use Test::More tests => 18;  # 10 explicit + 8 simple_client implicit
use utf8;
use lib 't'; use Utils;
use File::Temp qw(tempfile tempdir);
use POSIX ();

my $parent_pid = $$;
END { $? = 0 if $$ != $parent_pid }

sub http_get {
    my ($port, $timeout) = @_;
    $timeout //= 3 * TIMEOUT_MULT;
    my $body;
    my $cv = AE::cv;
    my $cli; $cli = simple_client GET => '/', port => $port,
        timeout => $timeout, sub {
            my ($b, $h) = @_;
            $body = $b if $h->{Status} == 200;
            $cv->send; undef $cli;
        };
    $cv->recv;
    return $body;
}

sub extract_pid { ($_[0] // '') =~ /^pid=(\d+)/ ? $1 : undef }

###############################################################################
# Test 1: SIGHUP with bad app_file → old generation stays
###############################################################################

my $dir = tempdir(CLEANUP => 1);
my $app_file = "$dir/app.feersum";

# Write a good app first
open my $fh, '>', $app_file or die;
print $fh 'sub { my $r = shift; $r->send_response(200, ["Content-Type"=>"text/plain"], \"pid=$$\n") }';
close $fh;

my (undef, $port) = get_listen_socket();

my $master = fork;
die "fork: $!" unless defined $master;
if (!$master) {
    require Feersum::Runner;
    eval {
        Feersum::Runner->new(
            listen      => ["localhost:$port"],
            app_file    => $app_file,
            hot_restart => 1,
            quiet       => 1,
        )->run();
    };
    POSIX::_exit(0);
}

select undef, undef, undef, 1.5 * TIMEOUT_MULT;

my $body1 = http_get($port);
ok $body1, "gen1 responds";
my $gen1_pid = extract_pid($body1);
ok $gen1_pid, "got gen1 pid";

# Break the app file
open $fh, '>', $app_file or die;
print $fh 'die "intentional failure"';
close $fh;

# SIGHUP → new gen fails → old gen should still serve
kill 'HUP', $master;
select undef, undef, undef, 2.0 * TIMEOUT_MULT;

my $body2 = http_get($port);
ok $body2, "server still responds after failed reload";
my $gen2_pid = extract_pid($body2);
is $gen2_pid, $gen1_pid, "same gen serving (rollback worked)";

# Restore good app and reload again
open $fh, '>', $app_file or die;
print $fh 'sub { my $r = shift; $r->send_response(200, ["Content-Type"=>"text/plain"], \"pid=$$\n") }';
close $fh;

kill 'HUP', $master;
select undef, undef, undef, 2.0 * TIMEOUT_MULT;

my $body3 = http_get($port);
ok $body3, "new gen responds after fix";
my $gen3_pid = extract_pid($body3);
isnt $gen3_pid, $gen1_pid, "new gen has different pid";

kill 'QUIT', $master;
waitpid $master, 0;
pass "master shut down cleanly";

###############################################################################
# Test 2: max_requests_per_worker → worker auto-recycles
###############################################################################

my ($fh2, $app2) = tempfile(SUFFIX => '.feersum', UNLINK => 1);
print $fh2 'sub { my $r = shift; $r->send_response(200, ["Content-Type"=>"text/plain"], \"pid=$$\n") }';
close $fh2;

my (undef, $port2) = get_listen_socket();

my $master2 = fork;
die "fork: $!" unless defined $master2;
if (!$master2) {
    require Feersum::Runner;
    eval {
        Feersum::Runner->new(
            listen                  => ["localhost:$port2"],
            app_file                => $app2,
            pre_fork                => 1,
            max_requests_per_worker => 3,
            quiet                   => 1,
        )->run();
    };
    POSIX::_exit(0);
}

select undef, undef, undef, 1.0 * TIMEOUT_MULT;

# Send 3 requests to hit the max
my $first_pid;
for my $i (1..3) {
    my $b = http_get($port2);
    $first_pid //= extract_pid($b);
}

# Wait for worker recycle (timer checks every 1s)
select undef, undef, undef, 3.0 * TIMEOUT_MULT;

my $body4 = http_get($port2);
ok $body4, "server responds after worker recycle";
my $new_pid = extract_pid($body4);
ok $new_pid, "got new worker pid";
isnt $new_pid, $first_pid, "worker was recycled (different pid)";

kill 'QUIT', $master2;
waitpid $master2, 0;
pass "master2 shut down cleanly";
