From d35dda5074f28a65c8b653e7020f0eb9e1f7cfca Mon Sep 17 00:00:00 2001 From: Serhii Zasenko Date: Tue, 3 Sep 2024 00:03:47 +0300 Subject: [PATCH 1/3] Add unknown version test to protocol redis testing suite --- lib/Protocol/Redis.pm | 2 +- lib/Protocol/Redis/Test.pm | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/Protocol/Redis.pm b/lib/Protocol/Redis.pm index 88c1484..fc25bf7 100644 --- a/lib/Protocol/Redis.pm +++ b/lib/Protocol/Redis.pm @@ -289,7 +289,7 @@ Yaroslav Korshak (yko) =head1 COPYRIGHT AND LICENSE -Copyright (C) 2011-2019, Sergey Zasenko. +Copyright (C) 2011-2024, Sergey Zasenko. This program is free software, you can redistribute it and/or modify it under the same terms as Perl 5.10. diff --git a/lib/Protocol/Redis/Test.pm b/lib/Protocol/Redis/Test.pm index d5ca4bb..41ca4e6 100644 --- a/lib/Protocol/Redis/Test.pm +++ b/lib/Protocol/Redis/Test.pm @@ -26,10 +26,11 @@ sub _apiv1_ok { my $redis_class = shift; subtest 'Protocol::Redis APIv1 ok' => sub { - plan tests => 43; + plan tests => 44; use_ok $redis_class; + # Version 1 protocol my $redis = new_ok $redis_class, [api => 1]; can_ok $redis, 'parse', 'api', 'on_message', 'encode'; @@ -47,6 +48,12 @@ sub _apiv1_ok { # Encoding method tests _encode_ok($redis); + + # Unknown version should raise an exception + eval { + new($redis_class, api => 0); + }; + ok($@, 'unknown version raises an exception'); } } @@ -354,7 +361,7 @@ L =head1 COPYRIGHT AND LICENSE -Copyright (C) 2010-2011, Sergey Zasenko +Copyright (C) 2010-2024, Sergey Zasenko This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0. From fa4744ee66cc784fee0e7b500d9a0467d2c1b9ec Mon Sep 17 00:00:00 2001 From: Serhii Zasenko Date: Tue, 3 Sep 2024 00:11:03 +0300 Subject: [PATCH 2/3] refactor tests --- lib/Protocol/Redis/Test.pm | 77 +++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/lib/Protocol/Redis/Test.pm b/lib/Protocol/Redis/Test.pm index 41ca4e6..3f54487 100644 --- a/lib/Protocol/Redis/Test.pm +++ b/lib/Protocol/Redis/Test.pm @@ -11,7 +11,7 @@ our @EXPORT = qw(protocol_redis_ok); use Test::More; require Carp; -sub protocol_redis_ok($$) { +sub protocol_redis_ok { my ($redis_class, $api_version) = @_; if ($api_version == 1) { @@ -30,31 +30,32 @@ sub _apiv1_ok { use_ok $redis_class; - # Version 1 protocol - my $redis = new_ok $redis_class, [api => 1]; + _test_version_1($redis_class); - can_ok $redis, 'parse', 'api', 'on_message', 'encode'; + _test_unknown_version($redis_class); + } +} + +sub _test_version_1 { + my $redis_class = shift; + + my $redis = new_ok $redis_class, [api => 1]; - is $redis->api, 1, '$redis->api'; + can_ok $redis, 'parse', 'api', 'on_message', 'encode'; - # Parsing method tests - $redis->on_message(undef); - _parse_string_ok($redis); - _parse_bulk_ok($redis); - _parse_multi_bulk_ok($redis); + is $redis->api, 1, '$redis->api'; - # on_message works - _on_message_ok($redis); + # Parsing method tests + $redis->on_message(undef); + _parse_string_ok($redis); + _parse_bulk_ok($redis); + _parse_multi_bulk_ok($redis); - # Encoding method tests - _encode_ok($redis); + # on_message works + _on_message_ok($redis); - # Unknown version should raise an exception - eval { - new($redis_class, api => 0); - }; - ok($@, 'unknown version raises an exception'); - } + # Encoding method tests + _encode_ok($redis); } sub _parse_string_ok { @@ -124,8 +125,7 @@ sub _parse_bulk_ok { $redis->parse("\$-1\r\n"); my $message = $redis->get_message; - ok defined($message) && !defined($message->{data}), - 'nil bulk message'; + ok defined($message) && !defined($message->{data}), 'nil bulk message'; # Two chunked bulk messages $redis->parse(join("\r\n", '$4', 'test', '+OK')); @@ -156,8 +156,8 @@ sub _parse_multi_bulk_ok { $redis->parse("\$5\r\ntest2\r\n"); $redis->parse("\$5\r\ntest3\r\n"); - is_deeply $redis->get_message, - { type => '*', + is_deeply $redis->get_message, { + type => '*', data => [ {type => '$', data => 'test1'}, {type => '$', data => 'test2'}, @@ -184,8 +184,8 @@ sub _parse_multi_bulk_ok { # Multi bulk message with status items $redis->parse(join("\r\n", ('*2', '+OK', '$4', 'test'), '')); - is_deeply $redis->get_message, - { type => '*', + is_deeply $redis->get_message, { + type => '*', data => [{type => '+', data => 'OK'}, {type => '$', data => 'test'}] }; @@ -210,7 +210,7 @@ sub _parse_multi_bulk_ok { {type => '$', data => 'test2'}, {type => '$', data => 'test3'} ] - }; + }; # Complex string $redis->parse("\*4\r\n"); @@ -224,11 +224,11 @@ sub _parse_multi_bulk_ok { {type => ':', data => 42}, {type => '+', data => 'test3'} ] - }; + }; is_deeply $redis->get_message, { type => '$', data => '12345', - }; + }; # pipelined multi-bulk $redis->parse( @@ -237,8 +237,8 @@ sub _parse_multi_bulk_ok { ('*1', '$3', 'ok3'), '') ); - is_deeply $redis->get_message, - { type => '*', + is_deeply $redis->get_message, { + type => '*', data => [{type => '$', data => 'ok1'}, {type => '$', data => 'ok2'}] }; is_deeply $redis->get_message, @@ -299,8 +299,8 @@ sub _encode_ok { join("\r\n", ('*1', '$4', 'test'), ''), 'encode multi-bulk'; - is $redis->encode( - { type => '*', + is $redis->encode({ + type => '*', data => [ {type => '$', data => 'test1'}, {type => '$', data => 'test2'} ] @@ -315,8 +315,8 @@ sub _encode_ok { is $redis->encode({type => '*', data => undef}), "\*-1\r\n", 'encode nil multi-bulk'; - is $redis->encode( - { type => '*', + is $redis->encode({ + type => '*', data => [ {type => '$', data => 'foo'}, {type => '$', data => undef}, @@ -328,6 +328,13 @@ sub _encode_ok { 'encode multi-bulk with nil element'; } +sub _test_unknown_version { + my $redis_class = shift; + + eval { new($redis_class, api => 0); }; + ok($@, 'unknown version raises an exception'); +} + 1; __END__ From 864517d5adcd8be6a64da2d53e35fc8221de8c53 Mon Sep 17 00:00:00 2001 From: Serhii Zasenko Date: Tue, 3 Sep 2024 00:13:15 +0300 Subject: [PATCH 3/3] cleanup --- lib/Protocol/Redis.pm | 2 +- lib/Protocol/Redis/Test.pm | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Protocol/Redis.pm b/lib/Protocol/Redis.pm index fc25bf7..a0da55d 100644 --- a/lib/Protocol/Redis.pm +++ b/lib/Protocol/Redis.pm @@ -289,7 +289,7 @@ Yaroslav Korshak (yko) =head1 COPYRIGHT AND LICENSE -Copyright (C) 2011-2024, Sergey Zasenko. +Copyright (C) 2011-2024, Serhii Zasenko. This program is free software, you can redistribute it and/or modify it under the same terms as Perl 5.10. diff --git a/lib/Protocol/Redis/Test.pm b/lib/Protocol/Redis/Test.pm index 3f54487..1ba462f 100644 --- a/lib/Protocol/Redis/Test.pm +++ b/lib/Protocol/Redis/Test.pm @@ -368,7 +368,7 @@ L =head1 COPYRIGHT AND LICENSE -Copyright (C) 2010-2024, Sergey Zasenko +Copyright (C) 2010-2024, Serhii Zasenko This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.