fixed problem when using Test::Aggregate and forking
[catagits/Catalyst-Runtime.git] / t / aggregate / unit_core_script_server.t
CommitLineData
60b86a55 1use strict;
2use warnings;
3
4use FindBin qw/$Bin/;
04f4497c 5use lib "$Bin/../lib";
60b86a55 6
6ec45f37 7use Test::More;
1a3dd976 8use Try::Tiny;
60b86a55 9
10use Catalyst::Script::Server;
11
f3bf2976 12my $testopts;
13
14# Test default (no opts/args behaviour)
b1bfeea6 15# Note undef for host means we bind to all interfaces.
16testOption( [ qw// ], ['3000', undef, opthash()] );
f3bf2976 17
18# Old version supports long format opts with either one or two dashes. New version only supports two.
19# Old New
fb533ac3 20# help -? -help --help -? --help
f3bf2976 21# debug -d -debug --debug -d --debug
22# host -host --host --host
23testOption( [ qw/--host testhost/ ], ['3000', 'testhost', opthash()] );
24testOption( [ qw/-h testhost/ ], ['3000', 'testhost', opthash()] );
25
26# port -p -port --port -l --listen
b1bfeea6 27testOption( [ qw/-p 3001/ ], ['3001', undef, opthash()] );
28testOption( [ qw/--port 3001/ ], ['3001', undef, opthash()] );
b89d8b98 29{
30 local $ENV{TESTAPPTOTESTSCRIPTS_PORT} = 5000;
31 testOption( [ qw// ], [5000, undef, opthash()] );
32}
56eb9db4 33{
34 local $ENV{CATALYST_PORT} = 5000;
35 testOption( [ qw// ], [5000, undef, opthash()] );
36}
f3bf2976 37
1a3dd976 38if (try { require Starman; 1; }) {
39 # fork -f -fork --fork -f --fork
40 testOption( [ qw/--fork/ ], ['3000', undef, opthash(fork => 1)] );
41 testOption( [ qw/-f/ ], ['3000', undef, opthash(fork => 1)] );
42}
f3bf2976 43
1a3dd976 44if (try { require MooseX::Daemonize; 1; }) {
45 # pidfile -pidfile --pid --pidfile
46 testOption( [ qw/--pidfile cat.pid/ ], ['3000', undef, opthash(pidfile => "cat.pid")] );
47 testOption( [ qw/--pid cat.pid/ ], ['3000', undef, opthash(pidfile => "cat.pid")] );
48}
f3bf2976 49
1a3dd976 50if (try { require Starman; 1; }) {
51 # keepalive -k -keepalive --keepalive -k --keepalive
52 testOption( [ qw/-k/ ], ['3000', undef, opthash(keepalive => 1)] );
53 testOption( [ qw/--keepalive/ ], ['3000', undef, opthash(keepalive => 1)] );
54}
f3bf2976 55
56# symlinks -follow_symlinks --sym --follow_symlinks
29b0a615 57#
b1bfeea6 58testOption( [ qw/--sym/ ], ['3000', undef, opthash(follow_symlinks => 1)] );
29b0a615 59testOption( [ qw/--follow_symlinks/ ], ['3000', undef, opthash(follow_symlinks => 1)] );
f3bf2976 60
1a3dd976 61if (try { require MooseX::Daemonize; 1; }) {
62 # background -background --bg --background
06208537 63 testBackgroundOptionWithFork( [ qw/--background/ ]);
64 testBackgroundOptionWithFork( [ qw/--bg/ ]);
1a3dd976 65}
29b0a615 66
f3bf2976 67# restart -r -restart --restart -R --restart
2c298960 68testRestart( ['-r'], restartopthash() );
56eb9db4 69{
70 local $ENV{TESTAPPTOTESTSCRIPTS_RELOAD} = 1;
71 testRestart( [], restartopthash() );
72}
73{
74 local $ENV{CATALYST_RELOAD} = 1;
75 testRestart( [], restartopthash() );
76}
77
2c298960 78# restart dly -rd -restartdelay --rd --restart_delay
79testRestart( ['-r', '--rd', 30], restartopthash(sleep_interval => 30) );
80testRestart( ['-r', '--restart_delay', 30], restartopthash(sleep_interval => 30) );
81
f3bf2976 82# restart dir -restartdirectory --rdir --restart_directory
2c298960 83testRestart( ['-r', '--rdir', 'root'], restartopthash(directories => ['root']) );
84testRestart( ['-r', '--rdir', 'root', '--rdir', 'lib'], restartopthash(directories => ['root', 'lib']) );
85testRestart( ['-r', '--restart_directory', 'root'], restartopthash(directories => ['root']) );
86
87# restart regex -rr -restartregex --rr --restart_regex
88testRestart( ['-r', '--rr', 'foo'], restartopthash(filter => qr/foo/) );
89testRestart( ['-r', '--restart_regex', 'foo'], restartopthash(filter => qr/foo/) );
f3bf2976 90
d5a8ec3c 91local $ENV{TESTAPPTOTESTSCRIPTS_RESTARTER};
92local $ENV{CATALYST_RESTARTER};
93{
94 is _build_testapp([])->restarter_class, 'Catalyst::Restarter', 'default restarter with no $ENV{CATALYST_RESTARTER}';
95}
96{
97 local $ENV{CATALYST_RESTARTER} = "CatalystX::Restarter::Other";
98 is _build_testapp([])->restarter_class, $ENV{CATALYST_RESTARTER}, 'override restarter with $ENV{CATALYST_RESTARTER}';
99}
100{
101 local $ENV{TESTAPPTOTESTSCRIPTS_RESTARTER} = "CatalystX::Restarter::Other2";
102 is _build_testapp([])->restarter_class, $ENV{TESTAPPTOTESTSCRIPTS_RESTARTER}, 'override restarter with $ENV{TESTAPPTOTESTSCRIPTS_RESTARTER}';
103}
6ec45f37 104done_testing;
f3bf2976 105
106sub testOption {
107 my ($argstring, $resultarray) = @_;
2c298960 108 my $app = _build_testapp($argstring);
1a3dd976 109 try {
2c298960 110 $app->run;
1a3dd976 111 }
112 catch {
113 fail $_;
2c298960 114 };
115 # First element of RUN_ARGS will be the script name, which we don't care about
06208537 116
2c298960 117 shift @TestAppToTestScripts::RUN_ARGS;
b17bc48c 118 my $server = pop @TestAppToTestScripts::RUN_ARGS;
119 like ref($server), qr/^Plack::Handler/, 'Is a Plack::Handler';
29b0a615 120
121 my @run_args = @TestAppToTestScripts::RUN_ARGS;
122 $run_args[-1]->{pidfile} = $run_args[-1]->{pidfile}->file->stringify
1a3dd976 123 if scalar(@run_args) && $run_args[-1]->{pidfile};
29b0a615 124
125
30b70903 126 # Mangle argv into the options..
127 $resultarray->[-1]->{argv} = $argstring;
29b0a615 128 is_deeply \@run_args, $resultarray, "is_deeply comparison " . join(' ', @$argstring);
2c298960 129}
130
06208537 131sub testBackgroundOptionWithFork {
132 my ($argstring) = @_;
133
134 ## First, make sure we can get an app
135 my $app = _build_testapp($argstring);
71782f3e 136
137 ## Sorry, don't really fork since this cause trouble in Test::Aggregate
138 $app->meta->add_around_method_modifier('daemon_fork', sub { return; });
139
06208537 140 try {
141 $app->run;
142 }
143 catch {
144 fail $_;
145 };
146
147 ## Check a few args
148 is_deeply $app->{ARGV}, $argstring;
149 is $app->{port}, '3000';
150 is($app->{background}, 1);
151}
152
2c298960 153sub testRestart {
154 my ($argstring, $resultarray) = @_;
155 my $app = _build_testapp($argstring);
56eb9db4 156 ok $app->restart, 'App is in restart mode';
2c298960 157 my $args = {$app->_restarter_args};
158 is_deeply delete $args->{argv}, $argstring, 'argv is arg string';
159 is ref(delete $args->{start_sub}), 'CODE', 'Closure to start app present';
160 is_deeply $args, $resultarray, "is_deeply comparison of restarter args " . join(' ', @$argstring);
161}
162
163sub _build_testapp {
164 my ($argstring, $resultarray) = @_;
f3bf2976 165
6ec45f37 166 local @ARGV = @$argstring;
167 local @TestAppToTestScripts::RUN_ARGS;
2c298960 168 my $i;
1a3dd976 169 try {
2c298960 170 $i = Catalyst::Script::Server->new_with_options(application_name => 'TestAppToTestScripts');
1a3dd976 171 pass "new_with_options " . join(' ', @$argstring);
172 }
173 catch {
174 fail "new_with_options " . join(' ', @$argstring) . " " . $_;
175 };
2c298960 176 ok $i;
177 return $i;
60b86a55 178}
179
f3bf2976 180# Returns the hash expected when no flags are passed
181sub opthash {
960bf5b0 182 return {
183 'pidfile' => undef,
184 'fork' => 0,
185 'follow_symlinks' => 0,
186 'background' => 0,
187 'keepalive' => 0,
188 @_,
189 };
60b86a55 190}
2c298960 191
192sub restartopthash {
44cf11e1 193 my $opthash = opthash(@_);
194 my $val = {
195 application_name => 'TestAppToTestScripts',
196 port => '3000',
197 debug => undef,
198 host => undef,
199 %$opthash,
2c298960 200 };
44cf11e1 201 return $val;
2c298960 202}
ed7e95f2 203
2041;
205