bunch of stuff
[gitmo/MooseX-Daemonize.git] / t / 01.filecreate.t
CommitLineData
5b9ebe08 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use Cwd;
6use File::Spec::Functions;
7
8use Test::More tests => 9;
3543c999 9use MooseX::Daemonize;
a4952679 10
5b9ebe08 11use constant DEBUG => 0;
12
13my $CWD = Cwd::cwd;
14my $FILENAME = "$CWD/im_alive";
15$ENV{MX_DAEMON_STDOUT} = catfile($CWD, 'Out.txt');
16$ENV{MX_DAEMON_STDERR} = catfile($CWD, 'Err.txt');
a4952679 17
18{
19
20 package FileMaker;
21 use Moose;
22 with qw(MooseX::Daemonize);
23
7d7115e5 24 has filename => ( isa => 'Str', is => 'ro' );
2eced271 25
43d819aa 26 after start => sub {
27 my $self = shift;
5b9ebe08 28 if ($self->is_daemon) {
29 $self->create_file( $self->filename );
30 }
43d819aa 31 };
7d7115e5 32
a4952679 33 sub create_file {
34 my ( $self, $file ) = @_;
3543c999 35 open( my $FILE, ">$file" ) || die $!;
36 close($FILE);
a4952679 37 }
a4952679 38}
39
3543c999 40my $app = FileMaker->new(
5b9ebe08 41 pidbase => $CWD,
42 filename => $FILENAME,
3543c999 43);
5b9ebe08 44
45ok(!$app->status, '... the daemon is running');
46
47diag $$ if DEBUG;
48
49ok($app->start, '... daemon started');
50sleep(1); # give it a second ...
51
52ok($app->status, '... the daemon is running');
53
54my $pid = $app->pidfile->pid;
55isnt($pid, $$, '... the pid in our pidfile is correct (and not us)');
56
57if (DEBUG) {
58 diag `ps $pid`;
59 diag "Status is: " . $app->status_message;
60}
61
b916501e 62ok( -e $app->filename, "file exists" );
5b9ebe08 63ok($app->status, '... the daemon is still running');
64
65if (DEBUG) {
66 diag `ps $pid`;
67 diag "Status is: " . $app->status_message;
68}
69
70ok( $app->stop, 'app stopped' );
71ok(!$app->status, '... the daemon is no longer running');
72
73if (DEBUG) {
74 diag `ps $pid`;
75 diag "Status is: " . $app->status_message;
76}
77
78ok( not(-e $app->pidfile->file) , 'pidfile gone' );
79
80unlink $FILENAME;
81unlink $ENV{MX_DAEMON_STDOUT};
82unlink $ENV{MX_DAEMON_STDERR};