File: 21-core-back-compat.t

package info (click to toggle)
libmoosex-daemonize-perl 0.22-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 420 kB
  • sloc: perl: 766; makefile: 2
file content (111 lines) | stat: -rw-r--r-- 2,369 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use strict;
use warnings;

use Test::More 0.88;
use Test::Fatal;
use Test::Moose;
use File::Temp qw(tempdir);
use File::Spec::Functions;

my $dir = tempdir( CLEANUP => 1 );

BEGIN {
    use_ok('MooseX::Daemonize::Core');
    use_ok('MooseX::Daemonize::Pid');
}

use constant DEBUG => 0;

$ENV{MX_DAEMON_STDOUT} = catfile($dir, 'Out.txt');
$ENV{MX_DAEMON_STDERR} = catfile($dir, 'Err.txt');

{
    package MyFooDaemon;
    use Moose;

    with 'MooseX::Daemonize::Core';

    has 'daemon_pid' => (is => 'rw', isa => 'MooseX::Daemonize::Pid');

    # capture the PID from the fork
    around 'daemon_fork' => sub {
        my $next = shift;
        my $self = shift;
        if (my $pid = $self->$next(@_)) {
            $self->daemon_pid(
                MooseX::Daemonize::Pid->new(pid => $pid)
            );
        }
    };

    sub start {
        my $self = shift;
        # tell it to ignore zombies ...
        $self->daemonize(
            ignore_zombies => 1,
            no_double_fork => 1,
        );
        return unless $self->is_daemon;
        # change to our local dir
        # so that we can debug easier
        chdir $dir;
        # make it easy to find with ps
        $0 = 'test-app';
        $SIG{INT} = sub {
            print "Got INT! Oh Noes!";
            exit;
        };
        while (1) {
            print "Hello from $$\n";
            sleep(10);
        }
        exit;
    }
}

my $d = MyFooDaemon->new;
isa_ok($d, 'MyFooDaemon');
does_ok($d, 'MooseX::Daemonize::Core');

is(
    exception { $d->start },
    undef,
    '... successfully daemonized from (' . $$ . ')',
);

my $p = $d->daemon_pid;
isa_ok($p, 'MooseX::Daemonize::Pid');

ok($p->is_running, '... the daemon process is running (' . $p->pid . ')');

my $pid = $p->pid;
if (DEBUG) {
    diag `ps $pid`;
    diag "-------";
    diag `ps -x | grep test-app`;
    diag "-------";
    diag "killing $pid";
}
kill INT => $p->pid;
diag "killed $pid" if DEBUG;

# give the process time to be killed on slow/loaded systems
for (1..10) {
    last unless kill 0 => $pid;
    # sleep a little before retrying
    sleep(2);
}

if (DEBUG) {
    diag `ps $pid`;
    diag "-------";
    diag `ps -x | grep test-app`;
}

ok(!$p->is_running, '... the daemon process is no longer running (' . $p->pid . ')');

unlink $ENV{MX_DAEMON_STDOUT};
unlink $ENV{MX_DAEMON_STDERR};

done_testing;