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