Scheduler: Fixed under warning, added some additional YAML tests
[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{
07305803 43 open STDERR, '>/dev/null';
cbf1ecfe 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' );
f9d8e3cf 56 unlink "$FindBin::Bin/lib/TestApp/every_minute.log";
cbf1ecfe 57 is( -e "$FindBin::Bin/lib/TestApp/every_hour.log", undef, 'every_hour did not execute, ok' );
f9d8e3cf 58 unlink "$FindBin::Bin/lib/TestApp/every_hour.log";
cbf1ecfe 59}
60
61# jump back in time by 2 hours, make sure both events run
62{
f9d8e3cf 63 my $state = lock_retrieve $STATE;
64 $state->{last_check} -= 60 * 120;
65 lock_store $state, $STATE;
cbf1ecfe 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
76sub 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
85sub broken_event {
86 my $c = shift;
87
88 die 'oops';
89}