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