Skip to content

Commit 6288d46

Browse files
committed
Error "not allowed after "multi"" now raise exception
1 parent 1a5f55c commit 6288d46

File tree

5 files changed

+45
-199
lines changed

5 files changed

+45
-199
lines changed

README

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
AnyEvent-RipeRedis version 0.22
1+
AnyEvent-RipeRedis version 0.23_01
22
======================
33

44
INSTALLATION

lib/AnyEvent/RipeRedis.pm

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use strict;
55
use warnings;
66
use base qw( Exporter );
77

8-
our $VERSION = '0.22';
8+
our $VERSION = '0.23_01';
99

1010
use AnyEvent::RipeRedis::Error;
1111

@@ -506,6 +506,14 @@ sub _execute {
506506
my $self = shift;
507507
my $cmd = shift;
508508

509+
if ( $self->{_multi_mode}
510+
&& ( exists $SUBUNSUB_CMDS{ $cmd->{name} }
511+
|| exists $NEED_POSTPROCESS{ $cmd->{name} } ) )
512+
{
513+
croak qq{Command "$cmd->{name}" not allowed after "multi" command.}
514+
. ' First, the transaction must be finalized.';
515+
}
516+
509517
if ( exists $NEED_PREPROCESS{ $cmd->{name} } ) {
510518
if ( $cmd->{name} eq 'multi' ) {
511519
$self->{_multi_mode} = 1;
@@ -527,20 +535,7 @@ sub _execute {
527535
if ( exists $SUB_CMDS{ $cmd->{name} }
528536
&& !defined $cmd->{on_message} )
529537
{
530-
croak q{"on_message" callback must be specified};
531-
}
532-
533-
if ( $self->{_multi_mode} ) {
534-
AE::postpone {
535-
my $err = _new_error(
536-
qq{Command "$cmd->{name}" not allowed after "multi" command.}
537-
. ' First, the transaction must be finalized.',
538-
E_OPRN_ERROR
539-
);
540-
$cmd->{on_reply}->( undef, $err );
541-
};
542-
543-
return;
538+
croak '"on_message" callback must be specified';
544539
}
545540

546541
if ( @{ $cmd->{args} } ) {

lib/AnyEvent/RipeRedis/Error.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use 5.008000;
44
use strict;
55
use warnings;
66

7-
our $VERSION = '0.22';
7+
our $VERSION = '0.23_01';
88

99
our %ERROR_CODES = (
1010
E_CANT_CONN => 1,

t/06-subs.t

Lines changed: 1 addition & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ my $server_info = run_redis_instance();
1010
if ( !defined $server_info ) {
1111
plan skip_all => 'redis-server is required for this test';
1212
}
13-
plan tests => 13;
13+
plan tests => 8;
1414

1515
my $r_consum = AnyEvent::RipeRedis->new(
1616
host => $server_info->{host},
@@ -35,10 +35,6 @@ my $redis = AnyEvent::RipeRedis->new(
3535
},
3636
);
3737

38-
t_sub_after_multi($redis);
39-
t_sub_after_exec($redis);
40-
t_sub_after_discard($redis);
41-
4238
$redis->disconnect;
4339

4440

@@ -297,179 +293,3 @@ sub t_psubunsub {
297293

298294
return;
299295
}
300-
301-
sub t_sub_after_multi {
302-
my $redis = shift;
303-
304-
my $t_err;
305-
306-
ev_loop(
307-
sub {
308-
my $cv = shift;
309-
310-
$redis->multi;
311-
$redis->subscribe( 'channel',
312-
{ on_reply => sub {
313-
my $reply = shift;
314-
$t_err = shift;
315-
316-
$cv->send;
317-
},
318-
319-
on_message => sub {},
320-
}
321-
);
322-
}
323-
);
324-
325-
$redis->disconnect;
326-
327-
my $t_pname = 'subscription after MULTI command';
328-
isa_ok( $t_err, 'AnyEvent::RipeRedis::Error' );
329-
is( $t_err->message, q{Command "subscribe" not allowed}
330-
. q{ after "multi" command. First, the transaction must be finalized.},
331-
"$t_pname; error message" );
332-
is( $t_err->code, E_OPRN_ERROR, "$t_pname; error code" );
333-
334-
return;
335-
}
336-
337-
sub t_sub_after_exec {
338-
my $redis = shift;
339-
340-
my $t_reply;
341-
342-
ev_loop(
343-
sub {
344-
my $cv = shift;
345-
346-
$redis->set( 'foo', "some\r\nstring" );
347-
348-
$redis->multi;
349-
$redis->get( 'foo' );
350-
$redis->incr( 'bar' );
351-
$redis->exec(
352-
sub {
353-
my $reply = shift;
354-
my $err = shift;
355-
356-
if ( defined $err ) {
357-
diag( $err->message );
358-
}
359-
}
360-
);
361-
362-
$redis->subscribe( 'channel',
363-
{ on_reply => sub {
364-
$t_reply = shift;
365-
my $err = shift;
366-
367-
if ( defined $err ) {
368-
diag( $err->message );
369-
}
370-
371-
$redis->unsubscribe( 'channel',
372-
sub {
373-
my $reply = shift;
374-
my $err = shift;
375-
376-
if ( defined $err ) {
377-
diag( $err->message );
378-
}
379-
380-
$redis->del( qw( foo bar ),
381-
sub {
382-
my $reply = shift;
383-
my $err = shift;
384-
385-
if ( defined $err ) {
386-
diag( $err->message );
387-
}
388-
389-
$cv->send;
390-
}
391-
);
392-
}
393-
);
394-
},
395-
396-
on_message => sub {},
397-
}
398-
);
399-
}
400-
);
401-
402-
is( $t_reply, 1, 'subscription after EXEC command' );
403-
404-
return;
405-
}
406-
407-
sub t_sub_after_discard {
408-
my $redis = shift;
409-
410-
my $t_reply;
411-
412-
ev_loop(
413-
sub {
414-
my $cv = shift;
415-
416-
$redis->set( 'foo', "some\r\nstring" );
417-
418-
$redis->multi;
419-
$redis->get( 'foo' );
420-
$redis->incr( 'bar' );
421-
$redis->discard(
422-
sub {
423-
my $reply = shift;
424-
my $err = shift;
425-
426-
if ( defined $err ) {
427-
diag( $err->message );
428-
}
429-
}
430-
);
431-
432-
$redis->subscribe( 'channel',
433-
{ on_reply => sub {
434-
$t_reply = shift;
435-
my $err = shift;
436-
437-
if ( defined $err ) {
438-
diag( $err->message );
439-
}
440-
441-
$redis->unsubscribe( 'channel',
442-
sub {
443-
my $reply = shift;
444-
my $err = shift;
445-
446-
if ( defined $err ) {
447-
diag( $err->message );
448-
}
449-
450-
$redis->del( 'foo',
451-
sub {
452-
my $reply = shift;
453-
my $err = shift;
454-
455-
if ( defined $err ) {
456-
diag( $err->message );
457-
}
458-
459-
$cv->send;
460-
}
461-
);
462-
}
463-
);
464-
},
465-
466-
on_message => sub {},
467-
}
468-
);
469-
}
470-
);
471-
472-
is( $t_reply, 1, 'subscription after DISCARD command' );
473-
474-
return;
475-
}

t/10-exceptions.t

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ use 5.008000;
22
use strict;
33
use warnings;
44

5-
use Test::More tests => 13;
5+
use Test::More tests => 15;
66
use Test::Fatal;
77
use AnyEvent::RipeRedis;
88

99
t_conn_timeout();
1010
t_read_timeout();
1111
t_min_reconnect_interval();
12+
t_not_allowed_after_multi();
1213
t_on_message();
1314

1415

@@ -138,6 +139,36 @@ sub t_min_reconnect_interval {
138139
return;
139140
}
140141

142+
sub t_not_allowed_after_multi {
143+
my $redis = AnyEvent::RipeRedis->new(
144+
on_error => sub {
145+
# do not print error
146+
}
147+
);
148+
149+
$redis->multi;
150+
151+
like(
152+
exception {
153+
$redis->subscribe('channel');
154+
},
155+
qr/Command "subscribe" not allowed after "multi" command\./,
156+
"t_not_allowed_after_multi; SUBSCRIBE",
157+
);
158+
159+
like(
160+
exception {
161+
$redis->select(2);
162+
},
163+
qr/Command "select" not allowed after "multi" command\./,
164+
"t_not_allowed_after_multi; SELECT",
165+
);
166+
167+
$redis->disconnect;
168+
169+
return;
170+
}
171+
141172
sub t_on_message {
142173
my $redis = AnyEvent::RipeRedis->new();
143174

0 commit comments

Comments
 (0)