File: Retry.t

package info (click to toggle)
percona-toolkit 3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 68,916 kB
  • sloc: perl: 241,287; sql: 22,868; sh: 19,746; javascript: 6,799; makefile: 353; awk: 38; python: 30; sed: 1
file content (125 lines) | stat: -rw-r--r-- 2,089 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/perl

BEGIN {
   die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
      unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
   unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
};

use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More tests => 6;

use Retry;
use PerconaTest;

my $rt = new Retry();

my @called = ();
my @retry  = ();
my @die    = ();

my $try = sub {
   push @called, 'try';
   die if shift @die;
};
my $fail = sub {
   push @called, 'fail';
   return shift @retry;
};
my $wait = sub {
   push @called, 'wait';
};
my $final_fail = sub {
   push @called, 'final_fail';
   return;
};

sub try_it {
   return $rt->retry(
      try        => $try,
      fail       => $fail,
      wait       => $wait,
      final_fail => $final_fail,
   );
}

# Success on first try;
@called = ();
@retry  = ();
@die    = ();
try_it();
is_deeply(
   \@called,
   ['try'],
   'Success on first try'
);

# Success on 2nd try.
@called = ();
@retry  = (1),
@die    = (1);
try_it();
is_deeply(
   \@called,
   ['try', 'fail', 'wait',
    'try'
   ],
   'Success on second try'
);

# Success on 3rd, last try.
@called = ();
@retry  = (1, 1),
@die    = (1, 1);
try_it();
is_deeply(
   \@called,
   ['try', 'fail', 'wait',
    'try', 'fail', 'wait',
    'try'
   ],
   'Success on third, final try'
);

# Failure.
@called = ();
@retry  = (1, 1, 1);
@die    = (1, 1, 1);
try_it();
is_deeply(
   \@called,
   ['try', 'fail', 'wait',
    'try', 'fail', 'wait',
    'try', 'final_fail',
   ],
   'Failure'
);

# Fail and no retry.
@called = ();
@retry  = (0);
@die    = (1);
try_it();
is_deeply(
   \@called,
   ['try', 'fail', 'final_fail'],
   "Fail, don't retry"
);

# #############################################################################
# Done.
# #############################################################################
my $output = '';
{
   local *STDERR;
   open STDERR, '>', \$output;
   $rt->_d('Complete test coverage');
}
like(
   $output,
   qr/Complete test coverage/,
   '_d() works'
);
exit;