1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
#!/usr/bin/perl
use v5.14;
use warnings;
use IO::Async::Test;
use Test2::V0 0.000149;
use lib ".";
use t::TimeAbout;
use IO::Async::Timer::Periodic;
use IO::Async::Loop;
use constant AUT => $ENV{TEST_QUICK_TIMERS} ? 0.1 : 1;
my $loop = IO::Async::Loop->new_builtin;
testing_loop( $loop );
{
my $tick = 0;
my @targs;
my $timer = IO::Async::Timer::Periodic->new(
interval => 2 * AUT,
on_tick => sub { @targs = @_; $tick++ },
);
ok( defined $timer, '$timer defined' );
isa_ok( $timer, [ "IO::Async::Timer" ], '$timer isa IO::Async::Timer' );
is_oneref( $timer, '$timer has refcount 1 initially' );
$loop->add( $timer );
is_refcount( $timer, 2, '$timer has refcount 2 after adding to Loop' );
ref_is( $timer->start, $timer, '$timer->start returns $timer' );
is_refcount( $timer, 2, '$timer has refcount 2 after starting' );
ok( $timer->is_running, 'Started Timer is running' );
time_about( sub { wait_for { $tick == 1 } }, 2, 'Timer works' );
is( \@targs, [ exact_ref($timer) ], 'on_tick args' );
ok( $timer->is_running, 'Timer is still running' );
time_about( sub { wait_for { $tick == 2 } }, 2, 'Timer works a second time' );
$loop->loop_once( 1 * AUT );
$timer->stop;
$timer->stop;
ok( 1, "Timer can be stopped a second time" );
$loop->loop_once( 2 * AUT );
ok( $tick == 2, "Stopped timer doesn't tick" );
undef @targs;
is_refcount( $timer, 2, '$timer has refcount 2 before removing from Loop' );
$loop->remove( $timer );
is_oneref( $timer, '$timer has refcount 1 after removing from Loop' );
ok( !$timer->is_running, 'Removed timer not running' );
$loop->add( $timer );
$timer->configure( interval => 1 * AUT );
$timer->start;
time_about( sub { wait_for { $tick == 3 } }, 1, 'Reconfigured timer interval works' );
$timer->stop;
$timer->configure( interval => 2 * AUT, first_interval => 0 );
$timer->start;
is( $tick, 3, 'Zero first_interval start not invoked yet' );
time_about( sub { wait_for { $tick == 4 } }, 0, 'Zero first_interval invokes callback async' );
time_about( sub { wait_for { $tick == 5 } }, 2, 'Normal interval used after first invocation' );
ok( dies { $timer->configure( interval => 5 ); },
'Configure a running timer fails' );
$loop->remove( $timer );
undef @targs;
is_oneref( $timer, 'Timer has refcount 1 finally' );
}
# reschedule => "skip"
{
my $tick = 0;
my $timer = IO::Async::Timer::Periodic->new(
interval => 1 * AUT,
reschedule => "skip",
on_tick => sub { $tick++ },
);
$loop->add( $timer );
$timer->start;
time_about( sub { wait_for { $tick == 1 } }, 1, 'skip Timer works' );
ok( $timer->is_running, 'skip Timer is still running' );
time_about( sub { wait_for { $tick == 2 } }, 1, 'skip Timer ticks a second time' );
$loop->remove( $timer );
}
# reschedule => "drift"
{
my $tick = 0;
my $timer = IO::Async::Timer::Periodic->new(
interval => 1 * AUT,
reschedule => "drift",
on_tick => sub { $tick++ },
);
$loop->add( $timer );
$timer->start;
time_about( sub { wait_for { $tick == 1 } }, 1, 'drift Timer works' );
ok( $timer->is_running, 'drift Timer is still running' );
time_about( sub { wait_for { $tick == 2 } }, 1, 'drift Timer ticks a second time' );
$loop->remove( $timer );
}
# Self-stopping
{
my $count = 0;
my $timer = IO::Async::Timer::Periodic->new(
interval => 0.1 * AUT,
on_tick => sub { $count++; shift->stop if $count >= 5 },
);
$loop->add( $timer );
$timer->start;
my $timedout;
my $id = $loop->watch_time( after => 1 * AUT, code => sub { $timedout++ } );
wait_for { $timedout };
is( $count, 5, 'Self-stopping timer can stop itself' );
$loop->remove( $timer );
$loop->unwatch_time( $id );
}
# Exception in on_tick shouldn't prevent reschedule
{
my $count = 0;
my $timer = IO::Async::Timer::Periodic->new(
interval => 0.1 * AUT,
on_tick => sub { $count++; die "FAIL $count" },
);
$loop->add( $timer );
$timer->start;
like( dies { wait_for { $count > 0 } },
qr/FAIL 1/, 'on_tick death throws exception' );
like( dies { wait_for { $count > 1 } },
qr/FAIL 2/, 'on_tick death rescheduled and runs a second time' );
$loop->remove( $timer );
}
## Subclass
my $sub_tick = 0;
{
my $timer = TestTimer->new(
interval => 2 * AUT,
);
ok( defined $timer, 'subclass $timer defined' );
isa_ok( $timer, [ "IO::Async::Timer" ], 'subclass $timer isa IO::Async::Timer' );
is_oneref( $timer, 'subclass $timer has refcount 1 initially' );
$loop->add( $timer );
is_refcount( $timer, 2, 'subclass $timer has refcount 2 after adding to Loop' );
$timer->start;
is_refcount( $timer, 2, 'subclass $timer has refcount 2 after starting' );
ok( $timer->is_running, 'Started subclass Timer is running' );
time_about( sub { wait_for { $sub_tick == 1 } }, 2, 'subclass Timer works' );
is_refcount( $timer, 2, 'subclass $timer has refcount 2 before removing from Loop' );
$loop->remove( $timer );
is_oneref( $timer, 'subclass $timer has refcount 1 after removing from Loop' );
}
done_testing;
package TestTimer;
use base qw( IO::Async::Timer::Periodic );
sub on_tick { $sub_tick++ }
|