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