Commit | Line | Data |
ba2735b6 |
1 | #!perl |
2 | use strict; |
3 | use warnings; |
4 | |
5 | use FindBin; |
6 | use lib "$FindBin::Bin/lib"; |
7 | use Test::More 'no_plan'; |
8 | use Storable qw/lock_store lock_retrieve/; |
9 | use Catalyst::Test 'TestApp'; |
10 | |
11 | our $HOME = "$FindBin::Bin/lib/TestApp"; |
12 | our $STATE = "$HOME/scheduler.state"; |
13 | our $URL = 'http://localhost/'; |
14 | our $BASE = 'Catalyst::Plugin::Scheduler::Base'; |
15 | our $Error = 'oops'; |
16 | our @Map = ( |
17 | # event # prio # label |
18 | [ '/cron/every_minute', -10, ], |
19 | [ TestApp::Controller::Cron->can('every_minute'), 10, "EM" ], |
20 | ); |
21 | |
22 | ### clean up |
23 | END { 1 while unlink $STATE } |
24 | |
25 | ### set up some schedules |
26 | { for my $aref ( @Map ) { |
27 | |
28 | my %args; |
29 | my $i = 0; |
30 | for my $key ( qw[event priority label] ) { |
31 | my $val = $aref->[ $i++ ] or next; |
32 | $args{ $key } = $val; |
33 | }; |
34 | |
35 | TestApp->schedule( at => '@always', %args ); |
36 | } |
37 | } |
38 | |
39 | ### get events, inspect them |
40 | { my @events = TestApp->scheduler->list_events; |
41 | ok( scalar(@events), "Found events" ); |
42 | is( scalar(@events), scalar(@Map), |
43 | " All events retrieved" ); |
44 | isa_ok( $_, "Catalyst::Plugin::Scheduler::Event" ) for @events; |
45 | |
46 | { # hack the last event check to make all events execute immediately |
47 | $BASE->_last_check_time( 0 ); |
48 | |
49 | my @pending_events = TestApp->scheduler->list_pending_events; |
50 | is( scalar( @pending_events ), scalar( @events ), |
51 | " All pending events retrieved" ); |
52 | |
53 | ### key our template on event name. |
54 | my %map = map { $_->[0] => $_ } @Map; |
55 | |
56 | my $prio; |
57 | for my $event ( @pending_events ) { |
58 | |
59 | ### check our accessors |
60 | { my $meth = 'ls_accessors'; |
61 | can_ok( $event, $meth ); |
62 | can_ok( $event, $event->$meth ); |
63 | } |
64 | |
65 | ### check caller |
66 | { my $re = __PACKAGE__ # package that sheduled |
67 | . '.+?' |
68 | . quotemeta($0) # this file |
69 | . ':\d+'; # the line number |
70 | my $by = $event->scheduled_by; |
71 | like( $by, qr/$re/, |
72 | " Caller registered: '$by'" ); |
73 | } |
74 | |
75 | ### check activity |
76 | ok( $event->active, " Event is active" ); |
77 | |
78 | ### check prio |
79 | cmp_ok( $prio, '>=', $event->priority, |
80 | " Sorted in right order" |
81 | ) unless defined $prio; # first in the chain. |
82 | $prio = $event->priority; |
83 | |
84 | ### check some properties |
85 | my $aref = $map{ $event->event }; |
86 | ok( $aref, " Event retrieved from template" ); |
87 | is( $event->event, $aref->[0], |
88 | " Right event: ".$event->event ); |
89 | is( $event->priority, $aref->[1], |
90 | " Right priority: ".$event->priority ); |
91 | is( $event->label, ($aref->[2] || $aref->[0]), |
92 | " Right label: ".$event->label ); |
93 | |
94 | |
95 | ### this should fail, our $c is not an object |
96 | eval { $event->run }; |
97 | ok( $@, " Can not run event" ); |
98 | like( $@, qr/not an object/, |
99 | ' $c is not an object' ); |
100 | |
101 | |
102 | } |
103 | |
104 | # hack the last event check to make all events execute immediately |
105 | $BASE->_last_check_time( 0 ); |
106 | { $_->active(0) for @events; |
107 | ok( !scalar(TestApp->scheduler->list_pending_events), |
108 | " All events disabled" ); |
109 | $_->active(1) for @events; |
110 | } |
111 | } |
112 | } |
113 | |