File: delay.t

package info (click to toggle)
libmojolicious-perl 2.98%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,968 kB
  • sloc: perl: 10,178; sh: 48; makefile: 8
file content (55 lines) | stat: -rw-r--r-- 1,376 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
use Mojo::Base -strict;

# Disable IPv6 and libev
BEGIN {
  $ENV{MOJO_NO_IPV6} = 1;
  $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}

use Test::More tests => 6;

# "And now to create an unstoppable army of between one million and two
#  million zombies!"
use Mojo::IOLoop;
use Mojo::IOLoop::Delay;

# Minimal
my $delay = Mojo::IOLoop::Delay->new;
my @results;
for my $i (0, 0) {
  $delay->begin;
  Mojo::IOLoop->timer(0 => sub { push @results, $i; $delay->end });
}
$delay->wait;
is_deeply \@results, [0, 0], 'right results';

# Everything
$delay = Mojo::IOLoop::Delay->new;
my $finished;
$delay->on(finish => sub { shift; $finished = [@_, 'works!'] });
for my $i (0, 0) {
  $delay->begin;
  Mojo::IOLoop->timer(0 => sub { $delay->end($i) });
}
@results = $delay->wait;
is_deeply $finished, [0, 0, 'works!'], 'right results';
is_deeply \@results, [0, 0], 'right results';

# Context
$delay = Mojo::IOLoop::Delay->new;
for my $i (3, 3) {
  $delay->begin;
  Mojo::IOLoop->timer(0 => sub { $delay->end($i) });
}
is $delay->wait, 3, 'right results';

# Mojo::IOLoop
$finished = undef;
$delay = Mojo::IOLoop->delay(sub { shift; $finished = [@_, 'too!'] });
for my $i (1, 1) {
  my $cb = $delay->begin;
  Mojo::IOLoop->timer(0 => sub { $delay->$cb($i) });
}
@results = $delay->wait;
is_deeply $finished, [1, 1, 'too!'], 'right results';
is_deeply \@results, [1, 1], 'right results';