Skip to content

Commit

Permalink
better reactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 8, 2012
1 parent 4fa29b3 commit c4f4911
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 65 deletions.
4 changes: 4 additions & 0 deletions Changes
@@ -1,4 +1,8 @@

3.27 2012-08-09
- Improved documentation.
- Improved tests.

3.26 2012-08-09
- Improved tests.

Expand Down
7 changes: 3 additions & 4 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -481,8 +481,8 @@ Check if loop is running.
Mojo::IOLoop->one_tick;
$loop->one_tick;
Run reactor until an event occurs or no events are being watched anymore. Note
that this method can recurse back into the reactor, so you need to be careful.
Run reactor until an event occurs. Note that this method can recurse back into
the reactor, so you need to be careful.
=head2 C<recurring>
Expand Down Expand Up @@ -534,8 +534,7 @@ object from everywhere inside the process.
Mojo::IOLoop->start;
$loop->start;
Start the loop, this will block until C<stop> is called or no events are being
watched anymore.
Start the loop, this will block until C<stop> is called.
# Start loop only if it is not running already
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Expand Down
8 changes: 3 additions & 5 deletions lib/Mojo/Reactor.pm
Expand Up @@ -125,9 +125,8 @@ Check if reactor is running. Meant to be overloaded in a subclass.
$reactor->one_tick;
Run reactor until an event occurs or no events are being watched anymore. Note
that this method can recurse back into the reactor, so you need to be careful.
Meant to be overloaded in a subclass.
Run reactor until an event occurs. Note that this method can recurse back into
the reactor, so you need to be careful. Meant to be overloaded in a subclass.
# Don't block longer than 0.5 seconds
my $id = $reactor->timer(0.5 => sub {});
Expand Down Expand Up @@ -156,8 +155,7 @@ Remove handle or timer. Meant to be overloaded in a subclass.
$reactor->start;
Start watching for I/O and timer events, this will block until C<stop> is
called or no events are being watched anymore. Meant to be overloaded in a
subclass.
called. Meant to be overloaded in a subclass.
=head2 C<stop>
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious.pm
Expand Up @@ -38,7 +38,7 @@ has static => sub { Mojolicious::Static->new };
has types => sub { Mojolicious::Types->new };

our $CODENAME = 'Rainbow';
our $VERSION = '3.26';
our $VERSION = '3.27';

# "These old doomsday devices are dangerously unstable.
# I'll rest easier not knowing where they are."
Expand Down
9 changes: 6 additions & 3 deletions t/mojo/ioloop.t
Expand Up @@ -6,7 +6,7 @@ BEGIN {
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}

use Test::More tests => 32;
use Test::More tests => 33;

# "Marge, you being a cop makes you the man!
# Which makes me the woman, and I have no interest in that,
Expand Down Expand Up @@ -98,8 +98,11 @@ $loop->remove($id);
$loop->remove($id2);
isa_ok $handle, 'IO::Socket', 'right reference';

# Make sure it stops automatically when not watching for events
$loop->start;
# The poll reactor stops when there are no events being watched anymore
my $time = time;
Mojo::IOLoop->start;
Mojo::IOLoop->one_tick;
ok time < ($time + 3), 'stopped automatically';

# Stream
$port = Mojo::IOLoop->generate_port;
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/reactor_ev.t
Expand Up @@ -24,13 +24,13 @@ $reactor = Mojo::IOLoop->singleton->reactor;
is ref $reactor, 'Mojo::Reactor::EV', 'right object';

# Make sure it stops automatically when not watching for events
Mojo::IOLoop->one_tick;
my $triggered;
Mojo::IOLoop->timer(0.25 => sub { $triggered++ });
Mojo::IOLoop->start;
ok $triggered, 'reactor waited for one event';
my $time = time;
Mojo::IOLoop->start;
Mojo::IOLoop->one_tick;
ok time < ($time + 3), 'stopped automatically';

# Listen
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/reactor_poll.t
Expand Up @@ -24,13 +24,13 @@ $reactor = Mojo::IOLoop->singleton->reactor;
is ref $reactor, 'Mojo::Reactor::Poll', 'right object';

# Make sure it stops automatically when not watching for events
Mojo::IOLoop->one_tick;
my $triggered;
Mojo::IOLoop->timer(0.25 => sub { $triggered++ });
Mojo::IOLoop->start;
ok $triggered, 'reactor waited for one event';
my $time = time;
Mojo::IOLoop->start;
Mojo::IOLoop->one_tick;
ok time < ($time + 3), 'stopped automatically';

# Listen
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/user_agent.t
Expand Up @@ -130,7 +130,7 @@ undef $ua;
is $get, 'Premature connection close', 'right error';
is $post, 'Premature connection close', 'right error';

# Make sure event loop is clean and stops automatically
# The poll reactor stops when there are no events being watched anymore
my $time = time;
Mojo::IOLoop->start;
ok time < ($time + 3), 'stopped automatically';
Expand Down
54 changes: 5 additions & 49 deletions t/mojolicious/lite_app.t
Expand Up @@ -9,7 +9,7 @@ BEGIN {
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}

use Test::More tests => 713;
use Test::More tests => 707;

# "Wait you're the only friend I have...
# You really want a robot for a friend?
Expand All @@ -22,7 +22,6 @@ use Mojo::Date;
use Mojo::IOLoop;
use Mojo::JSON;
use Mojo::Transaction::HTTP;
use Mojo::UserAgent;
use Mojolicious::Lite;
use Test::Mojo;

Expand Down Expand Up @@ -416,21 +415,7 @@ get '/eperror' => sub { shift->render(handler => 'ep') } => 'eperror';
# GET /subrequest
get '/subrequest' => sub {
my $self = shift;
my $tx = $self->ua->post('/template');
$self->render_text($tx->success->body);
};

# GET /subrequest_simple
get '/subrequest_simple' => sub {
my $self = shift;
$self->render_text($self->ua->post('/template')->res->body);
};

# GET /subrequest_blocking
get '/subrequest_blocking' => sub {
my $self = shift;
$self->ua->post('/template');
$self->render_text($self->ua->post('/template')->res->body);
$self->render_text($self->ua->post('/template')->success->body);
};

# Make sure hook runs non-blocking
Expand Down Expand Up @@ -566,23 +551,6 @@ my $t = Test::Mojo->new;
is $t->app->test_helper2, 'Mojolicious::Controller', 'right class';
is $t->app, app->commands->app, 'applications are equal';

# User agent timer
my $ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton)->app(app);
my $tua = Mojo::UserAgent->new(ioloop => $ua->ioloop)->app(app);
my $timer;
$tua->ioloop->timer(
0.1 => sub {
my $nb = '';
$tua->get(
'/' => sub {
my $tx = pop;
$timer = $tx->res->body . $nb;
}
);
$nb = 'works!';
}
);

# GET /☃
$t->get_ok('/☃')->status_is(200)->content_is('/%E2%98%83/%E2%98%83');

Expand Down Expand Up @@ -1144,7 +1112,7 @@ $tx->req->method('POST');
$tx->req->url->parse('/malformed_utf8');
$tx->req->headers->content_type('application/x-www-form-urlencoded');
$tx->req->body('foo=%E1');
$ua->start($tx);
$t->ua->start($tx);
is $tx->res->code, 200, 'right status';
is scalar $tx->res->headers->server, 'Mojolicious (Perl)',
'right "Server" value';
Expand Down Expand Up @@ -1194,14 +1162,8 @@ $t->get_ok('/subrequest')->status_is(200)
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is('Just works!');

# GET /subrequest_simple
$t->get_ok('/subrequest_simple')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is('Just works!');

# GET /subrequest_blocking
$t->get_ok('/subrequest_blocking')->status_is(200)
# GET /subrequest (again)
$t->get_ok('/subrequest')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is('Just works!');
Expand Down Expand Up @@ -1392,12 +1354,6 @@ $t->get_ok('/dynamic/inline')->status_is(200)
$t->get_ok('/dynamic/inline')->status_is(200)
->content_is("dynamic inline 2\n");

# User agent timer
$tua->ioloop->one_tick;
is $timer,
"/root.html\n/root.html\n/root.html\n/root.html\n/root.html\nworks!",
'right content';

__DATA__
@@ with-format.html.ep
<%= url_for 'without-format' %>
Expand Down

0 comments on commit c4f4911

Please sign in to comment.