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