Skip to content

Commit

Permalink
added clear method to Mojo::IOLoop::Delay
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 3, 2013
1 parent 1ef5a56 commit 230421e
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 5 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

3.93 2013-04-04
- Added clear method to Mojo::IOLoop::Delay.
- Improved documentation.

3.92 2013-04-03
Expand Down
12 changes: 12 additions & 0 deletions lib/Mojo/IOLoop/Delay.pm
Expand Up @@ -11,6 +11,12 @@ sub begin {
return sub { shift; $self->_step($id, @_) };
}

sub clear {
my $self = shift;
$self->{steps} = [];
return $self;
}

sub end { shift->_step(undef, @_) }

sub steps {
Expand Down Expand Up @@ -151,6 +157,12 @@ that the first argument passed to the callback will be ignored.
Mojo::UserAgent->new->get('mojolicio.us' => $delay->begin);
my $tx = $delay->wait;
=head2 clear
$delay = $delay->clear;
Clear all remaining steps.
=head2 end
my $remaining = $delay->end;
Expand Down
38 changes: 38 additions & 0 deletions t/mojo/delay.t
Expand Up @@ -69,6 +69,44 @@ 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';

# Clear all remaining steps
($finished, $result) = ();
$delay = Mojo::IOLoop::Delay->new;
$delay->on(finish => sub { $finished++ });
$delay->steps(
sub {
my $delay = shift;
Mojo::IOLoop->timer(0 => $delay->begin);
},
sub {
my $delay = shift;
$delay->clear;
Mojo::IOLoop->timer(0 => $delay->begin);
},
sub {
my $delay = shift;
$result = 'fail';
Mojo::IOLoop->timer(0 => $delay->begin);
},
sub { $result = 'fail' }
);
$delay->wait;
is $finished, 1, 'finish event has been emitted once';
ok !$result, 'no result';

# Clear all steps (except for the first)
$result = undef;
$delay = Mojo::IOLoop::Delay->new;
$delay->steps(
sub {
my $delay = shift;
Mojo::IOLoop->timer(0 => $delay->begin);
},
sub { $result = 'fail' }
);
$delay->clear->wait;
ok !$result, 'no result';

# Finish steps with event
$result = undef;
$delay = Mojo::IOLoop::Delay->new;
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/eventemitter.t
Expand Up @@ -40,7 +40,7 @@ $e->on(test2 => $cb);
$e->emit_safe('test2', 'works!');
is $echo, 'echo: works!echo2: works!', 'right echo';
is $err, qq{Event "test2" failed: test2: works!\n}, 'right error';
$echo = $err = undef;
($echo, $err) = ();
is scalar @{$e->subscribers('test2')}, 3, 'three subscribers';
$e->unsubscribe(test2 => $cb);
is scalar @{$e->subscribers('test2')}, 2, 'two subscribers';
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/user_agent.t
Expand Up @@ -63,7 +63,7 @@ post '/echo' => sub {
ok !$ua->need_proxy('dummy.mojolicio.us'), 'no proxy needed';
ok $ua->need_proxy('icio.us'), 'proxy needed';
ok $ua->need_proxy('localhost'), 'proxy needed';
$ENV{HTTP_PROXY} = $ENV{HTTPS_PROXY} = $ENV{NO_PROXY} = undef;
($ENV{HTTP_PROXY}, $ENV{HTTPS_PROXY}, $ENV{NO_PROXY}) = ();
local $ENV{http_proxy} = 'proxy.kraih.com';
local $ENV{https_proxy} = 'tunnel.kraih.com';
local $ENV{no_proxy} = 'localhost,localdomain,foo.com,kraih.com';
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/util.t
Expand Up @@ -414,7 +414,7 @@ is MojoMonkeyTest::yang(), 'yang', 'right result';
is Mojo::DeprecationTest::foo(), 'bar', 'right result';
like $warn, qr/foo is DEPRECATED at .*util\.t line \d+/, 'right warning';
ok !$die, 'no exception';
$warn = $die = undef;
($warn, $die) = ();
local $ENV{MOJO_FATAL_DEPRECATIONS} = 1;
ok !eval { Mojo::DeprecationTest::foo() }, 'no result';
ok !$warn, 'no warning';
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/websocket.t
Expand Up @@ -258,7 +258,7 @@ is $result, 'test3test2', 'right result';
is $client, 3, 'finish event has been emitted';

# Connection denied
$code = $ws = undef;
($code, $ws) = ();
$ua->websocket(
'/denied' => sub {
my ($ua, $tx) = @_;
Expand Down Expand Up @@ -436,7 +436,7 @@ Mojo::IOLoop->start;
is $result, 'foo bar', 'right result';

# Dies
$finished = $code = undef;
($finished, $code) = ();
my ($websocket, $msg);
$ua->websocket(
'/dead' => sub {
Expand Down

0 comments on commit 230421e

Please sign in to comment.