d96184198c52888079e5a0e7efd7f28b83f54e5f
[gitmo/MooseX-Daemonize.git] / t / 01.filecreate.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use File::Spec::Functions;
6
7 use Test::More tests => 29;
8 use Test::Moose;
9
10 use File::Temp qw(tempdir);
11
12 my $dir = tempdir( CLEANUP => 1 );
13
14 BEGIN {
15     use_ok('MooseX::Daemonize');
16 }
17
18 use constant DEBUG => 0;
19
20 my $FILENAME           = catfile($dir, "im_alive");
21 $ENV{MX_DAEMON_STDOUT} = catfile($dir, 'Out.txt');
22 $ENV{MX_DAEMON_STDERR} = catfile($dir, 'Err.txt');
23
24 {
25
26     package FileMaker;
27     use Moose;
28     with qw(MooseX::Daemonize);
29
30     has filename => ( isa => 'Str', is => 'ro' );
31     
32     after start => sub { 
33         my $self = shift;
34         if ($self->is_daemon) {
35             $self->create_file( $self->filename );
36         }
37     };
38
39     sub create_file {
40         my ( $self, $file ) = @_;
41         open( my $FILE, ">$file" ) || die $!;
42         close($FILE);
43     }
44 }
45
46 my $app = FileMaker->new(
47     pidbase  => $dir,
48     filename => $FILENAME,
49 );
50 isa_ok($app, 'FileMaker');
51 does_ok($app, 'MooseX::Daemonize');
52 does_ok($app, 'MooseX::Daemonize::WithPidFile');
53 does_ok($app, 'MooseX::Daemonize::Core');
54
55 isa_ok($app->pidfile, 'MooseX::Daemonize::Pid::File');
56
57 is($app->pidfile->file, catfile($dir, "filemaker.pid"), '... got the right PID file path');
58 ok(not(-e $app->pidfile->file), '... our pidfile does not exist');
59
60 ok(!$app->status, '... the daemon is running');
61 is($app->exit_code, MooseX::Daemonize->ERROR, '... got the right error code');
62
63 ok($app->stop, '... the app will stop cause its not running');
64 is($app->status_message, "Not running", '... got the correct status message');
65 is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code');
66
67 diag $$ if DEBUG;
68
69 ok($app->start, '... daemon started');
70 is($app->status_message, "Start succeeded", '... got the correct status message');
71 is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code');
72
73 sleep(1); # give it a second ...
74
75 ok(-e $app->pidfile->file, '... our pidfile exists' );
76
77 my $pid = $app->pidfile->pid;
78 isnt($pid, $$, '... the pid in our pidfile is correct (and not us)');
79
80 ok($app->status, '... the daemon is running');
81 is($app->status_message, "Daemon is running with pid ($pid)", '... got the correct status message');
82 is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code');
83
84 if (DEBUG) {
85     diag `ps $pid`;
86     diag "Status is: " . $app->status_message;    
87 }
88
89 ok( -e $app->filename, "file exists" );
90
91 if (DEBUG) {
92     diag `ps $pid`;
93     diag "Status is: " . $app->status_message;    
94 }
95
96 ok( $app->stop, '... app stopped' );
97 is($app->status_message, "Stop succeeded", '... got the correct status message');
98 is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code');
99
100 ok(!$app->status, '... the daemon is no longer running');
101 is($app->status_message, "Daemon is not running with pid ($pid)", '... got the correct status message');
102 is($app->exit_code, MooseX::Daemonize->ERROR, '... got the right error code');
103
104 if (DEBUG) {
105     diag `ps $pid`;
106     diag "Status is: " . $app->status_message;    
107 }
108
109 ok( not(-e $app->pidfile->file) , '... pidfile gone' );
110
111 unlink $FILENAME;
112 unlink $ENV{MX_DAEMON_STDOUT};
113 unlink $ENV{MX_DAEMON_STDERR};