7 use lib "$FindBin::Bin/lib";
9 use Storable qw/lock_store lock_retrieve/;
12 use Catalyst::Test 'TestApp';
14 our $STATE = "$FindBin::Bin/lib/TestApp/scheduler.state";
18 event => '/cron/every_minute',
23 event => \&every_hour,
26 # events with errors to test the error handling
29 event => '/cron/test_errors',
34 event => \&broken_event,
37 # hack the last event check to make all events execute immediately
38 my $state = { last_check => 0 };
39 lock_store $state, $STATE;
41 # test that all events execute, and that the error test doesn't break the app
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";
52 # run again, the events should not execute
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";
61 # jump back in time by 2 hours, make sure both events run
63 my $state = lock_retrieve $STATE;
64 $state->{last_check} -= 60 * 120;
65 lock_store $state, $STATE;
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";
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: $!";