File: 53-signal.t

package info (click to toggle)
libredis-fast-perl 0.34%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 492 kB
  • sloc: perl: 2,630; makefile: 7
file content (45 lines) | stat: -rw-r--r-- 1,271 bytes parent folder | download
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
use strict;
use warnings;
use Test::More;
use Test::Fatal;
use Redis::Fast;
use lib 't/tlib';
use Test::SpawnRedisServer;
use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);

my ($c, $srv) = redis();
END { $c->() if $c }


my $redis;
is(
  exception { $redis = Redis::Fast->new(server => $srv, name => 'my_name_is_glorious') },
  undef, 'connected to our test redis-server',
);
ok($redis->ping, 'ping');

$redis->select(2);

subtest 'signal' => sub {
    my $start_time = clock_gettime(CLOCK_MONOTONIC);
    my $sig_time;
    local $SIG{ALRM} = sub { $sig_time = clock_gettime(CLOCK_MONOTONIC); };
    alarm 1;
    $redis->blpop('abc', 5);
    my $end_time = clock_gettime(CLOCK_MONOTONIC);
    cmp_ok $sig_time - $start_time, '<=', 2, 'the signal handler is executed as soon as possible';
    cmp_ok $end_time - $start_time, '>=', 4, 'the signal does not unblock the Redis command';
};

subtest 'die in signal' => sub {
    my $start_time = clock_gettime(CLOCK_MONOTONIC);
    local $SIG{ALRM} = sub { die "ALARM\n"; };
    alarm 1;
    is exception {  $redis->blpop('abc', 20); }, "ALARM\n";
    my $end_time = clock_gettime(CLOCK_MONOTONIC);
    cmp_ok $end_time - $start_time, '<=', 2, 'the signal unblocks the Redis command';
    diag $start_time;
};

done_testing;