Skip to content

Commit

Permalink
test long polling in full application
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 1, 2013
1 parent e3245e4 commit 876e7d3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions t/mojolicious/app.t
Expand Up @@ -513,6 +513,14 @@ like $log, qr!Rendering template "foo/fun.html.ep" from DATA section\.!,
like $log, qr/200 OK/, 'right message';
$t->app->log->unsubscribe(message => $cb);

# MojoliciousTest::Foo::longpoll
my $stash;
$t->app->plugins->once(before_dispatch => sub { $stash = shift->stash });
$t->get_ok('/longpoll')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')->content_is('Poll!');
ok $stash->{finished}, 'finish event has been emitted';
ok $stash->{destroyed}, 'controller has been destroyed';

# MojoliciousTest::Foo::config
$t->get_ok('/stash_config')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')->content_is('123');
Expand Down
3 changes: 3 additions & 0 deletions t/mojolicious/lib/MojoliciousTest.pm
Expand Up @@ -137,6 +137,9 @@ sub startup {
$r->bridge('/suspended')->to('foo#suspended')->bridge->to('foo#suspended')
->route->to('foo#fun');

# /longpoll (long polling)
$r->route('/longpoll')->to('foo#longpoll');

# /shortcut/act
# /shortcut/ctrl
# /shortcut/ctrl-act (shortcuts to controller#action)
Expand Down
12 changes: 12 additions & 0 deletions t/mojolicious/lib/MojoliciousTest/Foo.pm
@@ -1,6 +1,8 @@
package MojoliciousTest::Foo;
use Mojo::Base 'Mojolicious::Controller';

sub DESTROY { shift->stash->{destroyed} = 1 }

sub config {
my $self = shift;
$self->render(text => $self->stash('config')->{test});
Expand All @@ -14,6 +16,16 @@ sub index {
$self->stash(handler => 'xpl', msg => 'Hello World!');
}

sub longpoll {
my $self = shift;
$self->on(finish => sub { shift->stash->{finished} = 1 });
$self->write_chunk(
sub {
shift->write_chunk('Poll!' => sub { shift->write_chunk('') });
}
);
}

sub plugin_camel_case {
my $self = shift;
$self->render(text => $self->some_plugin);
Expand Down

0 comments on commit 876e7d3

Please sign in to comment.