Revert Scheduler plugin back to 0.07 release state
[catagits/Catalyst-Plugin-Scheduler.git] / t / 04schedule.t
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use FindBin;
7 use lib "$FindBin::Bin/lib";
8 use Test::More;
9 use Storable qw/lock_store lock_retrieve/;
10
11 plan tests => 10;
12 use Catalyst::Test 'TestApp';
13
14 our $STATE = "$FindBin::Bin/lib/TestApp/scheduler.state";
15
16 TestApp->schedule(
17     at    => '* * * * *',
18     event => '/cron/every_minute',
19 );
20
21 TestApp->schedule(
22     at    => '@hourly',
23     event => \&every_hour,
24 );
25
26 # events with errors to test the error handling
27 TestApp->schedule(
28     at    => '*/2 * * * *',
29     event => '/cron/test_errors',
30 );
31
32 TestApp->schedule(
33     at    => '0 * * * *',
34     event => \&broken_event,
35 );
36
37 # hack the last event check to make all events execute immediately
38 my $state = { last_check => 0 };
39 lock_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";
50 }
51
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";
59 }
60
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;
66     
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";
72 }
73
74 ###
75
76 sub every_hour {
77     my $c = shift;
78     
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;
83 }
84
85 sub broken_event {
86     my $c = shift;
87     
88     die 'oops';
89 }