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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
#!/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 POSIX qw( ceil floor );
use English qw(-no_match_vars);
use Test::More tests => 10;
use MySQLStatusWaiter;
use PerconaTest;
my $oktorun = 1;
my @checked = ();
my $slept = 0;
my @vals = ();
sub oktorun {
return $oktorun;
}
sub get_status {
my ($var) = @_;
push @checked, $var;
my $vals = shift @vals;
if ( $vals ) {
return $vals->{$var} || 0;
}
$oktorun = 0;
return;
}
sub sleep {
$slept++;
return;
}
# #############################################################################
# _parse_spec()
# #############################################################################
throws_ok(
sub { new MySQLStatusWaiter(
max_spec => [qw(100)],
get_status => \&get_status,
sleep => \&sleep,
oktorun => \&oktorun,
) },
qr/100 is not a variable name/,
"Catch non-variable name"
);
throws_ok(
sub { new MySQLStatusWaiter(
max_spec => [qw(foo=bar)],
get_status => \&get_status,
sleep => \&sleep,
oktorun => \&oktorun,
) },
qr/value for foo must be a number/,
"Catch non-number value"
);
throws_ok(
sub { new MySQLStatusWaiter(
max_spec => [qw(foo)],
get_status => \&get_status,
sleep => \&sleep,
oktorun => \&oktorun,
) },
qr/foo does not exist/,
"Catch non-existent variable"
);
# ############################################################################
# Initial vals + 20%
# ############################################################################
@vals = (
# initial check for existence
{ Threads_connected => 9, },
{ Threads_running => 4, },
# first check, no wait
{ Threads_connected => 1, },
{ Threads_running => 1, },
# second check, wait
{ Threads_connected => 11, }, # too high
{ Threads_running => 5, }, # too high
# third check, wait
{ Threads_connected => 11, }, # too high
{ Threads_running => 4, },
# fourth check, wait
{ Threads_connected => 10, },
{ Threads_running => 5, }, # too high
# fifth check, no wait
{ Threads_connected => 10, },
{ Threads_running => 4, },
);
$oktorun = 1;
my $sw = new MySQLStatusWaiter(
oktorun => \&oktorun,
get_status => \&get_status,
sleep => \&sleep,
max_spec => [qw(Threads_connected Threads_running)],
);
SKIP: {
diag "Skipping test Threshold = ceil(InitialValue * 1.2)";
skip 'FIXME', 0;
is_deeply(
$sw->max_values(),
{
Threads_connected => ceil(9 + (9 * 0.20)),
Threads_running => ceil(4 + (4 * 0.20)),
},
"Threshold = ceil(InitialValue * 1.2)"
);
}
# first check
@checked = ();
$slept = 0;
$sw->wait();
SKIP: {
diag 'Skipping test Rechecked all variables';
skip 'FIXME', 0;
is_deeply(
\@checked,
[qw(Threads_connected Threads_running)],
"Checked both vars"
);
is(
$slept,
0,
"Vals not too high, did not sleep"
);
# second through fifth checks
@checked = ();
$slept = 0;
$sw->wait();
is_deeply(
\@checked,
[qw(
Threads_connected Threads_running
Threads_connected Threads_running
Threads_connected Threads_running
Threads_connected Threads_running
)],
"Rechecked all variables"
);
}
SKIP: {
diag "Skipping test Slept until values low enough";
skip 'FIXME', 0;
is(
$slept,
3,
"Slept until values low enough"
);
}
# ############################################################################
# Use static vals.
# ############################################################################
@vals = (
# initial check for existence
{ Threads_connected => 1, },
{ Threads_running => 1, },
# first check, no wait
{ Threads_connected => 1, },
{ Threads_running => 1, },
);
$sw = new MySQLStatusWaiter(
oktorun => \&oktorun,
get_status => \&get_status,
sleep => \&sleep,
max_spec => [qw(Threads_connected=5 Threads_running=5)],
);
is_deeply(
$sw->max_values(),
{
Threads_connected => 5,
Threads_running => 5,
},
"Static max values"
);
# first check
@checked = ();
$slept = 0;
$sw->wait();
SKIP: {
diag "Skipping test Checked both vars";
skip 'FIXME', 0;
is_deeply(
\@checked,
[qw(Threads_connected Threads_running)],
"Checked both vars"
);
}
is(
$slept,
0,
"Vals not too high, did not sleep"
);
# ############################################################################
# No spec, no wait.
# ############################################################################
@vals = (
# first check, no wait
{ Threads_connected => 1, },
{ Threads_running => 1, },
);
$sw = new MySQLStatusWaiter(
oktorun => \&oktorun,
get_status => \&get_status,
sleep => \&sleep,
max_spec => [],
);
is(
$sw->max_values(),
undef,
"No spec, no max values"
);
# first check
@checked = ();
$slept = 0;
$sw->wait();
is_deeply(
\@checked,
[],
"No spec, no vars checked"
);
is(
$slept,
0,
"No spec, no sleep"
);
# ############################################################################
# Critical thresholds (with static vals).
# ############################################################################
@vals = (
# initial check for existence
{ Threads_running => 1, },
{ Threads_running => 9, },
# first check, no wait
{ Threads_running => 1, },
{ Threads_running => 9, },
);
$sw = new MySQLStatusWaiter(
oktorun => \&oktorun,
get_status => \&get_status,
sleep => \&sleep,
max_spec => [qw(Threads_running=4)],
critical_spec => [qw(Threads_running=8)],
);
@checked = ();
$slept = 0;
$sw->wait();
is(
$slept,
0,
"Vals not critical, did not sleep"
);
SKIP: {
diag "Skipping test Die on critical threshold";
skip 'FIXME', 0;
throws_ok(
sub { $sw->wait(); },
qr/Threads_running=9 exceeds its critical threshold 8/,
"Die on critical threshold"
);
}
# #############################################################################
# Done.
# #############################################################################
my $output = '';
{
local *STDERR;
open STDERR, '>', \$output;
$sw->_d('Complete test coverage');
}
like(
$output,
qr/Complete test coverage/,
'_d() works'
);
exit;
|