prep for release
[catagits/Catalyst-Plugin-Scheduler.git] / t / 04schedule.t
CommitLineData
cbf1ecfe 1#!perl
bec4be8c 2
cbf1ecfe 3use strict;
4use warnings;
5
6use FindBin;
bec4be8c 7use lib "$FindBin::Bin/lib";
8use Test::More;
9use Storable qw/lock_store lock_retrieve/;
cbf1ecfe 10
bec4be8c 11plan tests => 10;
12use Catalyst::Test 'TestApp';
cbf1ecfe 13
bec4be8c 14our $STATE = "$FindBin::Bin/lib/TestApp/scheduler.state";
ba2735b6 15
bec4be8c 16TestApp->schedule(
17 at => '* * * * *',
18 event => '/cron/every_minute',
19);
ba2735b6 20
bec4be8c 21TestApp->schedule(
22 at => '@hourly',
23 event => \&every_hour,
24);
ba2735b6 25
bec4be8c 26# events with errors to test the error handling
27TestApp->schedule(
28 at => '*/2 * * * *',
29 event => '/cron/test_errors',
30);
cbf1ecfe 31
bec4be8c 32TestApp->schedule(
33 at => '0 * * * *',
34 event => \&broken_event,
35);
cbf1ecfe 36
37# hack the last event check to make all events execute immediately
bec4be8c 38my $state = { last_check => 0 };
39lock_store $state, $STATE;
40
41# test that all events execute, and that the error test doesn't break the app
42{
43 open STDERR, '>/dev/null';
44 ok( my $res = request('http://localhost/'), 'request ok' );
45 is( $res->content, 'default', 'response ok' );
46 is( -e "$FindBin::Bin/lib/TestApp/every_minute.log", 1, 'every_minute executed ok' );
47 unlink "$FindBin::Bin/lib/TestApp/every_minute.log";
48 is( -e "$FindBin::Bin/lib/TestApp/every_hour.log", 1, 'every_hour executed ok' );
49 unlink "$FindBin::Bin/lib/TestApp/every_hour.log";
cbf1ecfe 50}
51
bec4be8c 52# run again, the events should not execute
53{
54 ok( my $res = request('http://localhost/'), 'request ok' );
55 is( -e "$FindBin::Bin/lib/TestApp/every_minute.log", undef, 'every_minute did not execute, ok' );
56 unlink "$FindBin::Bin/lib/TestApp/every_minute.log";
57 is( -e "$FindBin::Bin/lib/TestApp/every_hour.log", undef, 'every_hour did not execute, ok' );
58 unlink "$FindBin::Bin/lib/TestApp/every_hour.log";
cbf1ecfe 59}
60
bec4be8c 61# jump back in time by 2 hours, make sure both events run
62{
63 my $state = lock_retrieve $STATE;
64 $state->{last_check} -= 60 * 120;
65 lock_store $state, $STATE;
cbf1ecfe 66
bec4be8c 67 ok( my $res = request('http://localhost/'), 'request ok' );
68 is( -e "$FindBin::Bin/lib/TestApp/every_minute.log", 1, 'every_minute executed ok' );
69 unlink "$FindBin::Bin/lib/TestApp/every_minute.log";
70 is( -e "$FindBin::Bin/lib/TestApp/every_hour.log", 1, 'every_hour executed ok' );
71 unlink "$FindBin::Bin/lib/TestApp/every_hour.log";
cbf1ecfe 72}
73
bec4be8c 74###
ba2735b6 75
bec4be8c 76sub every_hour {
77 my $c = shift;
cbf1ecfe 78
bec4be8c 79 # write out a file so the test knows we did something
80 my $fh = IO::File->new( $c->path_to( 'every_hour.log' ), 'w' )
81 or die "Unable to write log file: $!";
82 close $fh;
ba2735b6 83}
84
bec4be8c 85sub broken_event {
86 my $c = shift;
87
88 die 'oops';
cbf1ecfe 89}