Scheduler, basic features working, still lots todo
[catagits/Catalyst-Plugin-Scheduler.git] / t / 04schedule.t
diff --git a/t/04schedule.t b/t/04schedule.t
new file mode 100644 (file)
index 0000000..82c8965
--- /dev/null
@@ -0,0 +1,82 @@
+#!perl
+
+use strict;
+use warnings;
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+use Test::More;
+
+plan tests => 10;
+
+use Catalyst::Test 'TestApp';
+
+TestApp->schedule(
+    at    => '* * * * *',
+    event => '/cron/every_minute',
+);
+
+TestApp->schedule(
+    at    => '@hourly',
+    event => \&every_hour,
+);
+
+# events with errors to test the error handling
+TestApp->schedule(
+    at    => '*/2 * * * *',
+    event => '/cron/test_errors',
+);
+
+TestApp->schedule(
+    at    => '0 * * * *',
+    event => \&broken_event,
+);
+
+# hack the last event check to make all events execute immediately
+TestApp->_last_event_check( 0 );
+
+# test that all events execute, and that the error test doesn't break the app
+{
+    ok( my $res = request('http://localhost/'), 'request ok' );
+    is( $res->content, 'default', 'response ok' );
+    is( -e "$FindBin::Bin/lib/TestApp/every_minute.log", 1, 'every_minute executed ok' );
+    unlink "$FindBin::Bin/lib/TestApp/every_minute.log";
+    is( -e "$FindBin::Bin/lib/TestApp/every_hour.log", 1, 'every_hour executed ok' );
+    unlink "$FindBin::Bin/lib/TestApp/every_hour.log";
+}
+
+# run again, the events should not execute
+{
+    ok( my $res = request('http://localhost/'), 'request ok' );
+    is( -e "$FindBin::Bin/lib/TestApp/every_minute.log", undef, 'every_minute did not execute, ok' );
+    is( -e "$FindBin::Bin/lib/TestApp/every_hour.log", undef, 'every_hour did not execute, ok' );
+}
+
+# jump back in time by 2 hours, make sure both events run
+{
+    my $last = TestApp->_last_event_check;
+    TestApp->_last_event_check( $last - ( 60 * 120 ) );
+    
+    ok( my $res = request('http://localhost/'), 'request ok' );
+    is( -e "$FindBin::Bin/lib/TestApp/every_minute.log", 1, 'every_minute executed ok' );
+    unlink "$FindBin::Bin/lib/TestApp/every_minute.log";
+    is( -e "$FindBin::Bin/lib/TestApp/every_hour.log", 1, 'every_hour executed ok' );
+    unlink "$FindBin::Bin/lib/TestApp/every_hour.log";
+}
+
+###
+
+sub every_hour {
+    my $c = shift;
+    
+    # write out a file so the test knows we did something
+    my $fh = IO::File->new( $c->path_to( 'every_hour.log' ), 'w' )
+        or die "Unable to write log file: $!";
+    close $fh;
+}
+
+sub broken_event {
+    my $c = shift;
+    
+    die 'oops';
+}