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