~percona-toolkit-dev/percona-toolkit/release-2.2.3

« back to all changes in this revision

Viewing changes to t/pt-query-digest/read_timeout.t

  • Committer: fraserb at gmail
  • Date: 2012-07-30 14:30:05 UTC
  • mfrom: (319.2.12 ubuntu-12-64-issues)
  • Revision ID: fraserb@gmail.com-20120730143005-7txq2wckdfwsqmke
Merged ubuntu-12-64-issues

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
use PerconaTest;
15
15
 
16
16
use Time::HiRes qw(sleep time);
 
17
use POSIX qw(mkfifo);
17
18
 
18
19
# #########################################################################
19
20
# Issue 226: Fix mk-query-digest signal handling
20
21
# #########################################################################
21
 
diag(`rm -rf /tmp/mqd.pid`);
22
 
 
23
 
my ($start, $end, $waited);
24
 
my $timeout = wait_for(
25
 
   sub {
26
 
      $start = time;
27
 
      `$trunk/bin/pt-query-digest --read-timeout 2 --pid /tmp/mqd.pid 2>/dev/null`;
28
 
      return;
29
 
   },
30
 
   4,
31
 
);
32
 
$end    = time;
33
 
$waited = $end - $start;
34
 
if ( $timeout ) {
35
 
   # mqd ran longer than --read-timeout
36
 
   my $pid = `cat /tmp/mqd.pid`;
37
 
   `kill $pid`;
 
22
my $pid_file = '/tmp/mqd.pid';
 
23
my $fifo     = '/tmp/mqd.fifo';
 
24
unlink $pid_file and diag("Unlinking existing $pid_file");
 
25
unlink $fifo and diag("Unlinking existing $fifo");
 
26
 
 
27
my ($start, $end, $waited, $timeout);
 
28
SKIP: {
 
29
    skip("Not connected to a tty won't test --read-timeout with STDIN", 1)
 
30
        if !-t STDIN;
 
31
    use IO::File;
 
32
    STDIN->blocking(1);
 
33
    $timeout = wait_for(
 
34
        sub {
 
35
            $start = time;
 
36
            `$trunk/bin/pt-query-digest --read-timeout 2 --pid $pid_file 2>/dev/null`;
 
37
            return;
 
38
        },
 
39
        5,
 
40
    );
 
41
    $end    = time;
 
42
    $waited = $end - $start;
 
43
    if ( $timeout ) {
 
44
        # mqd ran longer than --read-timeout
 
45
        chomp(my $pid = slurp_file($pid_file));
 
46
        kill SIGTERM => $pid if $pid;
 
47
    }
 
48
 
 
49
    ok(
 
50
        $waited >= 2 && int($waited) <= 4,
 
51
        sprintf("--read-timeout 2 waited %.1f seconds reading STDIN", $waited)
 
52
    );
38
53
}
39
54
 
40
 
ok(
41
 
   $waited >= 2 && $waited < 4,
42
 
   sprintf("--read-timeout 2 waited %.1f seconds reading STDIN", $waited)
43
 
);
44
 
 
45
 
diag(`rm -rf /tmp/mqd.pid`);
46
 
diag(`rm -rf /tmp/mqd.fifo; mkfifo /tmp/mqd.fifo`);
47
 
system("$trunk/t/pt-query-digest/samples/write-to-fifo.pl /tmp/mqd.fifo 4 &");
 
55
unlink $pid_file;
 
56
mkfifo $fifo, 0700;
 
57
system("$trunk/t/pt-query-digest/samples/write-to-fifo.pl $fifo 4 &");
48
58
 
49
59
$timeout = wait_for(
50
60
   sub {
51
61
      $start = time;
52
 
      `$trunk/bin/pt-query-digest --read-timeout 2 --pid /tmp/mqd.pid /tmp/mqd.fifo`;
 
62
      `$trunk/bin/pt-query-digest --read-timeout 2 --pid $pid_file $fifo`;
53
63
      return;
54
64
   },
55
 
   4,
 
65
   5,
56
66
);
57
67
$end    = time;
58
68
$waited = $end - $start;
59
69
if ( $timeout ) {
60
70
   # mqd ran longer than --read-timeout
61
 
   my $pid = `cat /tmp/mqd.pid`;
62
 
   `kill $pid`;
 
71
   chomp(my $pid = slurp_file($pid_file));
 
72
   kill SIGTERM => $pid if $pid;
63
73
}
64
74
 
65
75
ok(
66
 
   $waited >= 2 && $waited < 4,
 
76
   $waited >= 2 && int($waited) <= 4,
67
77
   sprintf("--read-timeout waited %.1f seconds reading a file", $waited)
68
78
);
69
79
 
70
 
diag(`rm -rf /tmp/mqd.pid`);
71
 
diag(`rm -rf /tmp/mqd.fifo`);
 
80
unlink $pid_file;
 
81
unlink $fifo;
72
82
 
73
83
# #############################################################################
74
84
# Done.