Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fixed a few edge cases in Mojo::IOLoop::Delay
  • Loading branch information
kraih committed Aug 17, 2012
1 parent aa5ef2b commit 6c16c46
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
11 changes: 8 additions & 3 deletions lib/Mojo/IOLoop/Delay.pm
Expand Up @@ -34,18 +34,23 @@ sub wait {
sub _step {
my ($self, $id) = (shift, shift);

# Arguments
my $ordered = $self->{ordered} ||= [];
my $unordered = $self->{unordered} ||= [];
if (defined $id) { $ordered->[$id] = [@_] }
else { push @$unordered, @_ }

# Wait for more events
return $self->{counter} unless --$self->{counter} <= 0;

# Next step
my $cb = shift @{$self->{steps} ||= []};
$self->{$_} = [] for qw(ordered unordered);
my @ordered = map {@$_} grep {defined} @$ordered;
$self->$cb(@ordered, @$unordered) if $cb;
$self->emit('finish', @ordered, @$unordered) unless @{$self->{steps}};
my @args = ((map {@$_} grep {defined} @$ordered), @$unordered);
$self->$cb(@args) if $cb;

# Finished
$self->emit('finish', @args) unless @{$self->{steps}} || $self->{finished}++;

return 0;
}
Expand Down
21 changes: 15 additions & 6 deletions t/mojo/delay.t
Expand Up @@ -6,7 +6,7 @@ BEGIN {
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}

use Test::More tests => 12;
use Test::More tests => 14;

# "And now to create an unstoppable army of between one million and two
# million zombies!"
Expand Down Expand Up @@ -49,21 +49,28 @@ is $delay->wait, 3, 'right results';

# Steps
my $result;
$delay = Mojo::IOLoop::Delay->new;
$finished = undef;
$delay = Mojo::IOLoop::Delay->new;
$delay->on(finish => sub { $finished++ });
$delay->steps(
sub {
my $delay = shift;
my $cb = $delay->begin;
$delay->begin->(3, 2, 1);
Mojo::IOLoop->timer(0 => sub { $cb->(1, 2, 3) });
},
sub { shift->begin->(@_, 4) },
sub {
my ($delay, @numbers) = @_;
my $cb = $delay->begin;
Mojo::IOLoop->timer(0 => sub { $cb->(undef, @numbers, 4) });
},
sub {
my ($delay, @numbers) = @_;
$result = \@numbers;
}
);
is_deeply [$delay->wait], [2, 3, 2, 1, 4], 'right numbers';
is $finished, 1, 'finish event has been emitted once';
is_deeply $result, [2, 3, 2, 1, 4], 'right numbers';

# Event loop
Expand All @@ -78,10 +85,11 @@ is_deeply $finished, [1, 1, 'too!'], 'right results';
is_deeply \@results, [1, 1], 'right results';

# Nested delays
$result = undef;
$delay = Mojo::IOLoop->delay(
($result, $finished) = undef;
$delay = Mojo::IOLoop->delay(
sub {
my $first = shift;
my $first = shift;
$first->on(finish => sub { $finished++ });
my $second = Mojo::IOLoop->delay($first->begin);
Mojo::IOLoop->timer(0 => $second->begin);
Mojo::IOLoop->timer(0 => $first->begin);
Expand All @@ -105,4 +113,5 @@ $delay = Mojo::IOLoop->delay(
}
);
is_deeply [$delay->wait], [2, 3, 2, 1, 4, 5, 6], 'right numbers';
is $finished, 1, 'finish event has been emitted once';
is_deeply $result, [1, 2, 3, 2, 3, 2, 1, 4, 5, 6], 'right numbers';

0 comments on commit 6c16c46

Please sign in to comment.