From 0f4805561ff3f09115de068a2679f728f01a4992 Mon Sep 17 00:00:00 2001
From: "Dmitry E. Oboukhov" <unera@debian.org>
Date: Wed, 4 Jul 2012 11:39:52 +0400
Subject: [PATCH] replace onsecondary_retry by second_retry_delay

---
 connector/perl/lib/MR/Pending.pm | 33 ++++++++-----------
 connector/perl/t/05-pending.t    | 54 +++++++++++---------------------
 2 files changed, 31 insertions(+), 56 deletions(-)

diff --git a/connector/perl/lib/MR/Pending.pm b/connector/perl/lib/MR/Pending.pm
index a874b78eb6..7300447963 100644
--- a/connector/perl/lib/MR/Pending.pm
+++ b/connector/perl/lib/MR/Pending.pm
@@ -200,7 +200,7 @@ sub send {
             next unless $pend->is_timeout;
 
             # don't repead request that have secondary retry
-            next if $pend->_has_onsecondary_retry and $pend->try;
+            next if $pend->_has_second_retry_delay and $pend->try;
 
             my $cont = $self->runcatch($pend->onretry,
                 ($pend->id, $pend, $self));
@@ -227,9 +227,9 @@ sub _check_if_second_restart {
     my ($self, $pend) = @_;
 
     return unless $pend->is_pending;
-    return unless $pend->_has_onsecondary_retry;
+    return unless $pend->_has_second_retry_delay;
     return unless $pend->is_secondarytimeout;
-    return if $pend->is_second_pend;
+    return if $pend->is_second;
 
 
     my $id = $pend->id;
@@ -245,9 +245,9 @@ sub _check_if_second_restart {
         timeout         => $pend->timeout,
         retry_delay     => $pend->retry_delay,
         retry           => $pend->retry,
-        is_second_pend  => 1,
+        is_second       => 1,
 
-        onretry         => $pend->onsecondary_retry,
+        onretry         => $pend->onretry,
         _time           => $pend->_time,
         onok            => sub {
 
@@ -333,7 +333,7 @@ sub recv {
             }
         } elsif (vec($ein, $fileno, 1)) {
             $pend->close("connection reset (".$pend->last_error.")");
-        } elsif ($pend->_has_onsecondary_retry) {
+        } elsif ($pend->_has_second_retry_delay) {
             $self->_check_if_second_restart( $pend );
         } elsif ($pend->is_timeout) {
             $pend->close("timeout (".$pend->last_error.")");
@@ -448,13 +448,13 @@ has id => (
 );
 
 
-has is_second_pend => (
+has is_second => (
     is          => 'ro',
     isa         => 'Bool',
     default     => 0,
 );
 
-=head2 onok onerror onretry onsecondary_retry
+=head2 onok onerror onretry
 
 functions that are called on different stages
 
@@ -470,13 +470,6 @@ has $_ => (
     required  => 1,
 ) for qw/onok onerror onretry/;
 
-has $_ => (
-    is        => 'ro',
-    isa       => 'CodeRef',
-    predicate => "_has_$_",
-    clearer   => '_clear__onsecondary_retry',
-) for qw{onsecondary_retry};
-
 has $_ => (
     is        => 'rw',
     isa       => 'Num',
@@ -543,9 +536,9 @@ has try => (
 );
 
 has second_retry_delay => (
-    is      => 'ro',
-    isa     => 'Num',
-    default => .1,
+    is          => 'ro',
+    isa         => 'Num',
+    predicate   => '_has_second_retry_delay',
 );
 
 # has bornat => (
@@ -587,10 +580,10 @@ sub is_timeout {
 
     if ($self->is_pending) {
         # second pends is never timeout
-        return 0 if $self->is_second_pend;
+        return 0 if $self->is_second;
 
         # if pend has second_retry it is never timeout
-        return 0 if $self->_has_onsecondary_retry;
+        return 0 if $self->_has_second_retry_delay;
 
         $timeout ||= $self->timeout;
     } else {
diff --git a/connector/perl/t/05-pending.t b/connector/perl/t/05-pending.t
index 5b9edbcfd8..1d6a4fd1ae 100644
--- a/connector/perl/t/05-pending.t
+++ b/connector/perl/t/05-pending.t
@@ -15,7 +15,7 @@ use lib "$Bin/../lib";
 
 use Carp qw/confess/;
 
-use Test::More tests => 59;
+use Test::More tests => 61;
 use Test::Exception;
 
 use List::MoreUtils qw/zip/;
@@ -205,7 +205,8 @@ my $started = time;
 my @done;
 
 $ontry = sub {
-    my ($i) = @_;
+    my ($i, $pend) = @_;
+    $i = 0 if $pend->is_second;
     return $box[$i]->Call(
         tst_pending_server_pause => [],
         {
@@ -243,16 +244,6 @@ cmp_ok $done[1], '<', 1.1, 'first server response time less than 1.1 seconds';
 cmp_ok $done[1], '>', 1, 'first server response time more than 1 seconds';
 
 
-my $onsecondary_retry = sub {
-    my ($i) = @_;
-    return $box[0]->Call(
-        tst_pending_server_pause => [],
-        {
-            return_fh       => 1,
-            unpack_format   => '$'
-        }
-    );
-};
 
 
 note '** check onsecondary_retry';
@@ -297,11 +288,10 @@ for ( 1 .. 2) {
                     },
                     onerror             => $onerror,
                     onretry             => $ontry,
-                    onsecondary_retry   => $onsecondary_retry,
                     timeout             => 0.9,
                     second_retry_delay  => 0.4,
-                    retry_delay  => 0.4,
-                    retry        => 3,
+                    retry_delay         => 0.4,
+                    retry               => 3,
                 ),
             ]
         )->work;
@@ -315,23 +305,13 @@ for ( 1 .. 2) {
 }
 
 
+my $onsecondary_retry_touched = 0;
 $ontry = sub {
-    my ($i) = @_;
+    my ($i, $pend) = @_;
+    $i = 0 if $pend->is_second;
+    $onsecondary_retry_touched++ if $pend->is_second;
     return $box[$i]->Call(
-        tst_pending_server_pause => [ 1 ],
-        {
-            return_fh       => 1,
-            unpack_format   => '$'
-        }
-    );
-};
-
-
-my $onsecondary_retry_touched = 0;
-$onsecondary_retry = sub {
-    $onsecondary_retry_touched++;
-    return $box->Call(
-        tst_pending_server_pause => [ 3 ],
+        tst_pending_server_pause => [ $pend->is_second ? 3 : 1 ],
         {
             return_fh       => 1,
             unpack_format   => '$'
@@ -384,21 +364,23 @@ my @res = ([], []);
                 },
                 onerror             => $onerror,
                 onretry             => $ontry,
-                onsecondary_retry   => $onsecondary_retry,
                 timeout             => 3,
                 second_retry_delay  => 0.1,
-                retry_delay  => 0.4,
-                retry        => 3,
+                retry_delay         => 0.4,
+                retry               => 3,
             ),
         ]
     );
     $pd->work;
     is scalar @{ $res[0] }, 1, 'first callback was touched once';
     is scalar @{ $res[1] }, 1, 'second callback was touched once';
-    is $res[0][0]{box}, 'box1', 'it was a box1';
-    is $res[1][0]{box}, 'box2', 'it was a box2';
-    cmp_ok time - $started, '<', 2, 'Both requests got less than 2 second';
+    is $res[0][0]{box}, 'box1', 'first replay was from box1';
+    cmp_ok $res[0][0]{time}, '>=', 1, 'first replay took more than 1 second';
+    cmp_ok $res[1][0]{time}, '>=', 1, 'second replay took more than 1 second';
+    is $res[1][0]{box}, 'box2', 'second replay was from box2';
+    cmp_ok time - $started, '<', 1.1, 'Both requests got less than 1.1 second';
     is $onsecondary_retry_touched, 1, 'onsecondary_retry touched once';
+
 }
 
 cmp_ok time - $started, '>', 3, 'Destructor waited for longer request';
-- 
GitLab