File: 22timer-countdown.t

package info (click to toggle)
libio-async-perl 0.51-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,052 kB
  • sloc: perl: 11,110; makefile: 8
file content (273 lines) | stat: -rw-r--r-- 6,452 bytes parent folder | download
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/perl -w

use strict;

use IO::Async::Test;

use Test::More tests => 54;
use Test::Fatal;
use Test::Refcount;

use Time::HiRes qw( time );

use IO::Async::Timer::Countdown;

use IO::Async::Loop::Poll;

use constant AUT => $ENV{TEST_QUICK_TIMERS} ? 0.1 : 1;

# Kindof like Test::Timer only we use Time::HiRes
# We'll be quite lenient on the time taken, in case of heavy test machine load
sub time_about
{
   my ( $code, $target, $name ) = @_;

   my $lower = $target*0.75;
   my $upper = $target*1.5 + 1;

   my $now = time;
   $code->();
   my $took = (time - $now) / AUT;

   cmp_ok( $took, '>', $lower, "$name took at least $lower" );
   cmp_ok( $took, '<', $upper * 3, "$name took no more than $upper" );
   if( $took > $upper and $took <= $upper * 3 ) {
      diag( "$name took longer than $upper - this may just be an indication of a busy testing machine rather than a bug" );
   }
}

my $loop = IO::Async::Loop::Poll->new;

testing_loop( $loop );

{
   my $expired;
   my @eargs;

   my $timer = IO::Async::Timer::Countdown->new(
      delay => 2 * AUT,

      on_expire => sub { @eargs = @_; $expired = 1 },
   );

   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' );

   ok( !$timer->is_running, 'New Timer is no yet running' );
   ok( !$timer->is_expired, 'New Timer is no yet expired' );

   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' );
   ok( !$timer->is_expired, 'Started Timer not yet expired' );

   time_about( sub { wait_for { $expired } }, 2, 'Timer works' );
   is_deeply( \@eargs, [ $timer ], 'on_expire args' );

   ok( !$timer->is_running, 'Expired Timer is no longer running' );
   ok(  $timer->is_expired, 'Expired Timer now expired' );

   undef @eargs;

   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' );

   undef $expired;

   is( $timer->start, $timer, '$timer->start out of a Loop returns $timer' );

   $loop->add( $timer );

   ok(  $timer->is_running, 'Re-started Timer is running' );
   ok( !$timer->is_expired, 'Re-started Timer not yet expired' );

   time_about( sub { wait_for { $expired } }, 2, 'Timer works a second time' );

   ok( !$timer->is_running, '2nd-time expired Timer is no longer running' );
   ok(  $timer->is_expired, '2nd-time expired Timer now expired' );

   undef $expired;
   $timer->start;

   $loop->loop_once( 1 * AUT );

   $timer->stop;

   $timer->stop;

   ok( 1, "Timer can be stopped a second time" );

   $loop->loop_once( 2 * AUT );

   ok( !$expired, "Stopped timer doesn't expire" );

   undef $expired;
   $timer->start;

   $loop->loop_once( 1 * AUT );

   my $now = time;
   $timer->reset;

   $loop->loop_once( 1.5 * AUT );

   ok( !$expired, "Reset Timer hasn't expired yet" );

   wait_for { $expired };
   my $took = (time - $now) / AUT;

   cmp_ok( $took, '>', 1.5, "Timer has now expired took at least 1.5" );
   cmp_ok( $took, '<', 2.5, "Timer has now expired took no more than 2.5" );

   $loop->remove( $timer );

   undef @eargs;

   is_oneref( $timer, 'Timer has refcount 1 finally' );
}

{
   my $timer = IO::Async::Timer::Countdown->new(
      delay => 2 * AUT,
      on_expire => sub { },
   );

   $loop->add( $timer );

   $timer->start;

   $loop->remove( $timer );

   $loop->loop_once( 3 * AUT );

   ok( !$timer->is_expired, "Removed Timer does not expire" );
}

{
   my $timer = IO::Async::Timer::Countdown->new(
      delay => 2 * AUT,
      on_expire => sub { },
   );

   $timer->start;

   $loop->add( $timer );

   ok( $timer->is_running, 'Pre-started Timer is running after adding' );

   time_about( sub { wait_for { $timer->is_expired } }, 2, 'Pre-started Timer works' );

   $loop->remove( $timer );
}

{
   my $timer = IO::Async::Timer::Countdown->new(
      delay => 2 * AUT,
      on_expire => sub { },
   );

   $timer->start;
   $timer->stop;

   $loop->add( $timer );

   $loop->loop_once( 3 * AUT );

   ok( !$timer->is_expired, "start/stopped Timer doesn't expire" );

   $loop->remove( $timer );
}

{
   my $timer = IO::Async::Timer::Countdown->new(
      delay => 2 * AUT,
      on_expire => sub { },
   );

   $loop->add( $timer );

   $timer->configure( delay => 1 * AUT );

   $timer->start;

   time_about( sub { wait_for { $timer->is_expired } }, 1, 'Reconfigured timer delay works' );

   my $expired;
   $timer->configure( on_expire => sub { $expired = 1 } );

   $timer->start;

   time_about( sub { wait_for { $expired } }, 1, 'Reconfigured timer on_expire works' );

   $timer->start;
   ok( exception { $timer->configure( delay => 5 ); },
       'Configure a running timer fails' );

   $loop->remove( $timer );
}

{
   my $timer = IO::Async::Timer::Countdown->new(
      delay => 1 * AUT,
      remove_on_expire => 1,

      on_expire => sub { },
   );

   $loop->add( $timer );
   $timer->start;

   time_about( sub { wait_for { $timer->is_expired } }, 1, 'remove_on_expire Timer' );

   is( $timer->get_loop, undef, 'remove_on_expire Timer removed from Loop after expire' );
}

## Subclass

my $sub_expired;
{
   my $timer = TestTimer->new(
      delay => 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_expired } }, 2, 'subclass Timer works' );

   ok( !$timer->is_running, 'Expired subclass Timer is no longer running' );

   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' );
}

package TestTimer;
use base qw( IO::Async::Timer::Countdown );

sub on_expire { $sub_expired = 1 }