Skip to content

Commit

Permalink
better tests for events
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 1, 2013
1 parent 18791fd commit 36da6b8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 85 deletions.
117 changes: 36 additions & 81 deletions t/mojo/websocket.t
Expand Up @@ -31,16 +31,14 @@ get '/link' => sub {
$self->render(text => $self->url_for('index')->to_abs);
};

my $server;
websocket '/' => sub {
my $self = shift;
$self->on(finish => sub { $server += 2 });
$self->on(finish => sub { shift->stash->{finished}++ });
$self->on(
message => sub {
my ($self, $msg) = @_;
my $url = $self->url_for->to_abs;
$self->send("${msg}test2$url");
$server = 1;
}
);
} => 'index';
Expand Down Expand Up @@ -74,15 +72,13 @@ websocket '/early_start' => sub {
);
};

my ($handshake, $denied);
websocket '/denied' => sub {
my $self = shift;
$self->tx->handshake->on(finish => sub { $handshake += 1 });
$self->on(finish => sub { $denied += 1 });
$self->tx->handshake->on(finish => sub { $self->stash->{handshake}++ });
$self->on(finish => sub { shift->stash->{finished}++ });
$self->render(text => 'denied', status => 403);
};

my $subreq;
websocket '/subreq' => sub {
my $self = shift;
$self->ua->websocket(
Expand All @@ -100,14 +96,13 @@ websocket '/subreq' => sub {
}
);
$self->send('test0');
$self->on(finish => sub { $subreq += 1 });
$self->on(finish => sub { shift->stash->{finished}++ });
};

websocket '/echo' => sub {
shift->on(message => sub { shift->send(shift) });
};

my $buffer = '';
websocket '/double_echo' => sub {
shift->on(
message => sub {
Expand All @@ -130,11 +125,10 @@ websocket '/close' => sub {
shift->on(message => sub { Mojo::IOLoop->remove(shift->tx->connection) });
};

my $timeout;
websocket '/timeout' => sub {
my $self = shift;
Mojo::IOLoop->stream($self->tx->connection)->timeout(0.25);
$self->on(finish => sub { $timeout = 'works!' });
$self->on(finish => sub { shift->stash->{finished}++ });
};

# URL for WebSocket
Expand All @@ -149,7 +143,8 @@ is $res->code, 404, 'right status';
like $res->body, qr/Page not found/, 'right content';

# Plain WebSocket
my $result;
my ($stash, $result);
app->plugins->once(before_dispatch => sub { $stash = shift->stash });
$ua->websocket(
'/' => sub {
my ($ua, $tx) = @_;
Expand All @@ -165,6 +160,8 @@ $ua->websocket(
}
);
Mojo::IOLoop->start;
Mojo::IOLoop->one_tick until exists $stash->{finished};
is $stash->{finished}, 1, 'finish event has been emitted once';
like $result, qr!test1test2ws://localhost:\d+/!, 'right result';

# Failed WebSocket connection
Expand Down Expand Up @@ -211,7 +208,7 @@ $ua->start(
}
);
Mojo::IOLoop->start;
is $finished, 1, 'finish event has been emitted';
is $finished, 1, 'finish event has been emitted once';
is $early, 1, 'finish event has been emitted at the right time';
is $status, 1000, 'right status';
is $msg, 'I ♥ Mojolicious!', 'right message';
Expand All @@ -223,32 +220,25 @@ is(Mojo::IOLoop->stream($tx->connection)->handle, $sock,

# Server directly sends a message
$result = undef;
my $client;
$ua->websocket(
'/early_start' => sub {
my ($ua, $tx) = @_;
$tx->on(
finish => sub {
$client += 2;
Mojo::IOLoop->stop;
}
);
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(
message => sub {
my ($tx, $msg) = @_;
$result = $msg;
$tx->send('test3');
$client = 1;
}
);
}
);
Mojo::IOLoop->start;
is $result, 'test3test2', 'right result';
is $client, 3, 'finish event has been emitted';

# Connection denied
($code, $ws) = ();
($stash, $code, $ws) = ();
app->plugins->once(before_dispatch => sub { $stash = shift->stash });
$ua->websocket(
'/denied' => sub {
my ($ua, $tx) = @_;
Expand All @@ -258,36 +248,29 @@ $ua->websocket(
}
);
Mojo::IOLoop->start;
is $stash->{handshake}, 1, 'finish event has been emitted once for handshake';
is $stash->{finished}, 1, 'finish event has been emitted once';
ok !$ws, 'not a WebSocket';
is $code, 403, 'right status';
is $handshake, 1, 'finished handshake';
is $denied, 1, 'finished websocket';
is $code, 403, 'right status';

# Subrequests
$finished = 0;
($code, $result) = ();
($stash, $code, $result) = ();
app->plugins->once(before_dispatch => sub { $stash = shift->stash });
$ua->websocket(
'/subreq' => sub {
my ($ua, $tx) = @_;
$code = $tx->res->code;
$tx->on(message => sub { $result .= pop });
$tx->on(
finish => sub {
$finished += 4;
Mojo::IOLoop->stop;
}
);
$tx->on(finish => sub { Mojo::IOLoop->stop });
}
);
Mojo::IOLoop->start;
is $code, 101, 'right status';
is $result, 'test0test1', 'right result';
is $finished, 4, 'finished client websocket';
is $subreq, 1, 'finished server websocket';
is $stash->{finished}, 1, 'finish event has been emitted once';
is $code, 101, 'right status';
is $result, 'test0test1', 'right result';

# Parallel subrequests
my $delay = Mojo::IOLoop->delay;
$finished = 0;
($code, $result) = ();
my ($code2, $result2);
my $end = $delay->begin;
Expand All @@ -302,12 +285,7 @@ $ua->websocket(
$tx->finish if $msg eq 'test1';
}
);
$tx->on(
finish => sub {
$finished += 1;
$end->();
}
);
$tx->on(finish => sub { $end->() });
}
);
my $end2 = $delay->begin;
Expand All @@ -316,43 +294,29 @@ $ua->websocket(
my ($ua, $tx) = @_;
$code2 = $tx->res->code;
$tx->on(message => sub { $result2 .= pop });
$tx->on(
finish => sub {
$finished += 2;
$end2->();
}
);
$tx->on(finish => sub { $end2->() });
}
);
$delay->wait;
is $code, 101, 'right status';
is $result, 'test0test1', 'right result';
is $code2, 101, 'right status';
is $result2, 'test0test1', 'right result';
is $finished, 3, 'finished client websocket';
is $subreq, 3, 'finished server websocket';
is $code, 101, 'right status';
is $result, 'test0test1', 'right result';
is $code2, 101, 'right status';
is $result2, 'test0test1', 'right result';

# Client-side drain callback
$result = '';
$client = 0;
my ($drain, $counter);
$ua->websocket(
'/echo' => sub {
my ($ua, $tx) = @_;
$tx->on(
finish => sub {
$client += 2;
Mojo::IOLoop->stop;
}
);
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(
message => sub {
my ($tx, $msg) = @_;
$result .= $msg;
$tx->finish if ++$counter == 2;
}
);
$client = 1;
$tx->send(
'hi!' => sub {
shift->send('there!');
Expand All @@ -364,35 +328,27 @@ $ua->websocket(
);
Mojo::IOLoop->start;
is $result, 'hi!there!', 'right result';
is $client, 3, 'finish event has been emitted';
is $drain, 1, 'no leaking subscribers';

# Server-side drain callback
$result = '';
$counter = $client = 0;
$result = '';
$counter = 0;
$ua->websocket(
'/double_echo' => sub {
my ($ua, $tx) = @_;
$tx->on(
finish => sub {
$client += 2;
Mojo::IOLoop->stop;
}
);
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(
message => sub {
my ($tx, $msg) = @_;
$result .= $msg;
$tx->finish if ++$counter == 2;
}
);
$client = 1;
$tx->send('hi!');
}
);
Mojo::IOLoop->start;
is $result, 'hi!hi!', 'right result';
is $client, 3, 'finish event has been emitted';

# Sending objects
$result = undef;
Expand Down Expand Up @@ -487,15 +443,17 @@ is $result, 'hi!' x 100, 'right result';
# Timeout
my $log = '';
$msg = app->log->on(message => sub { $log .= pop });
$stash = undef;
app->plugins->once(before_dispatch => sub { $stash = shift->stash });
$ua->websocket(
'/timeout' => sub {
pop->on(finish => sub { Mojo::IOLoop->stop });
}
);
Mojo::IOLoop->start;
app->log->unsubscribe(message => $msg);
is $timeout, 'works!', 'finish event has been emitted';
is $stash->{finished}, 1, 'finish event has been emitted once';
like $log, qr/Inactivity timeout\./, 'right log message';
app->log->unsubscribe(message => $msg);

# Ping/pong
my $pong;
Expand All @@ -515,7 +473,4 @@ $ua->websocket(
Mojo::IOLoop->start;
is $pong, 'test', 'received pong with payload';

# The "finish" event has been emitted on the server side too
is $server, 3, 'finish event has been emitted';

done_testing();
9 changes: 5 additions & 4 deletions t/mojolicious/lite_app.t
Expand Up @@ -165,11 +165,10 @@ get '/stream' => sub {
$self->rendered;
};

my $finished;
get '/finished' => sub {
my $self = shift;
$self->on(finish => sub { $finished += 2 });
$finished = 1;
$self->on(finish => sub { shift->stash->{finished} *= 2 });
$self->stash->{finished} = 1;
$self->render(text => 'so far so good!');
};

Expand Down Expand Up @@ -666,9 +665,11 @@ $t->get_ok('/maybe/ajax' => {'X-Requested-With' => 'XMLHttpRequest'})
->content_is('is ajax');

# With finish event
my $stash;
$t->app->plugins->once(before_dispatch => sub { $stash = shift->stash });
$t->get_ok('/finished')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')->content_is('so far so good!');
is $finished, 3, 'finished';
is $stash->{finished}, 2, 'finish event has been emitted once';

# IRI
$t->get_ok('/привет/мир')->status_is(200)
Expand Down

0 comments on commit 36da6b8

Please sign in to comment.