Permalink
Browse files

Extract wait_one_response() from wait_all_responses()

Signed-off-by: Pedro Melo <melo@simplicidade.org>
  • Loading branch information...
1 parent b306bfc commit a2e8e9e0a4871b418153cef678e102a373abee00 @melo melo committed Jan 23, 2013
Showing with 55 additions and 8 deletions.
  1. +27 −8 lib/Redis.pm
  2. +28 −0 t/04-pipeline.t
View
@@ -179,10 +179,20 @@ sub __run_cmd {
sub wait_all_responses {
my ($self) = @_;
- for my $handler (splice @{ $self->{queue} }) {
- my ($command, $cb, $collect_errors) = @$handler;
- $cb->($self->__read_response($command, $collect_errors));
- }
+ my $queue = $self->{queue};
+ $self->wait_one_response while @$queue;
+
+ return;
+}
+
+sub wait_one_response {
+ my ($self) = @_;
+
+ my $handler = shift @{ $self->{queue} };
+ return unless $handler;
+
+ my ($command, $cb, $collect_errors) = @$handler;
+ $cb->($self->__read_response($command, $collect_errors));
return;
}
@@ -747,6 +757,8 @@ __END__
});
long_computation();
$redis->wait_all_responses;
+ ## or
+ $redis->wait_one_response();
## Or run a large batch of commands in a pipeline
my %hash = _get_large_batch_of_commands();
@@ -810,23 +822,25 @@ soon as at least one of the following conditions holds:
=item *
-A non-pipelined (synchronous) command has been sent on the same connection
+A non-pipelined (synchronous) command is called on the same connection
=item *
A pub/sub subscription command (one of C<subscribe>, C<unsubscribe>,
-C<psubscribe>, or C<punsubscribe>) is about to be sent on the same connection.
+C<psubscribe>, or C<punsubscribe>) is about to be called on the same
+connection.
=item *
-The L</wait_all_responses> method is called explicitly.
+One of L</wait_all_responses> or L</wait_one_response> methods is called
+explicitly.
=back
The coderef you supply to a pipelined command method is invoked once the
response is available. It takes two arguments, C<$reply> and C<$error>. If
C<$error> is defined, it contains the text of an error reply sent by the Redis
-server. Otherwise, C<$reply> is the non-error reply. For almost all commands,
+server. Otherwise, C<$reply> is the non-error reply. For almost all commands,
that means it's C<undef>, or a defined but non-reference scalar, or an array
ref of any of those; but see L</keys>, L</info>, and L</exec>.
@@ -991,6 +1005,11 @@ connection name string. The most important one: no spaces.
Waits until all pending pipelined responses have been received, and invokes the
pipeline callback for each one. See L</PIPELINING>.
+=head3 wait_one_response
+
+Waits until the first pending pipelined response has been received, and invokes
+its callback. See L</PIPELINING>.
+
=head2 Transaction-handling commands
View
@@ -85,4 +85,32 @@ subtest 'transaction with error and no pipeline' => sub {
);
};
+
+subtest 'wait_one_response' => sub {
+ my $first;
+ my $second;
+
+ $r->get('a', sub { $first++ });
+ $r->get('a', sub { $second++ });
+ $r->get('a', sub { $first++ });
+ $r->get('a', sub { $second++ });
+
+ $r->wait_one_response();
+ is($first, 1, 'after first wait_one_response(), first callback called');
+ is($second, undef, '... but not the second one');
+
+ $r->wait_one_response();
+ is($first, 1, 'after second wait_one_response(), first callback was not called again');
+ is($second, 1, '... but the second one was called');
+
+ $r->wait_all_responses();
+ is($first, 2, 'after final wait_all_responses(), first callback was called again');
+ is($second, 2, '... the second one was also called');
+
+ $r->wait_one_response();
+ is($first, 2, 'after final wait_one_response(), first callback was not called again');
+ is($second, 2, '... nor was the second one');
+};
+
+
done_testing();

0 comments on commit a2e8e9e

Please sign in to comment.