File: 05-server.t

package info (click to toggle)
libtest-mock-redis-perl 0.08-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 180 kB
  • sloc: perl: 1,577; makefile: 2
file content (141 lines) | stat: -rw-r--r-- 3,214 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
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
#!perl -T

use strict;
use warnings;
use lib 't/tlib';
use Test::More;
use Test::Exception;
use Test::Mock::Redis;


=pod
x   AUTH  
x   ECHO
x   PING
x   QUIT
o   SELECT  <-- TODO: complain about invalid values?

    BGREWRITEAOF
    BGSAVE
    CONFIG GET
    CONFIG RESETSTAT
    CONFIG SET
    DBSIZE
    DEBUG OBJECT
    DEBUG SEGFAULT
x   FLUSHALL
x   FLUSHDB
o   INFO
x   LASTSAVE
    MONITOR
x   SAVE
o   SHUTDOWN
    SLAVEOF
    SYNC
=cut

ok(my $r = Test::Mock::Redis->new, 'pretended to connect to our test redis-server');
my @redi = ($r);

my ( $guard, $srv );
if( $ENV{RELEASE_TESTING} ){
    use_ok("Redis");
    use_ok("Test::SpawnRedisServer");
    ($guard, $srv) = redis();
    ok(my $r = Redis->new(server => $srv), 'connected to our test redis-server');
    $r->flushall;
    unshift @redi, $r
}

foreach my $r (@redi){
    diag("testing $r") if $ENV{RELEASE_TESTING};

    ok($r->ping, 'ping returns PONG');
    ok($r->select($_), "select returns true for $_") for 0..15;

    $r->select(0);

    # TODO: do we care?
    eval{ $r->auth };
    like($@, qr/^\Q[auth] ERR wrong number of arguments for 'auth' command\E/, 'auth without a password dies');

    ok($r->auth('foo'), 'auth with anything else returns true');


    for(0..15){
        $r->select($_);
        $r->set('foo', "foobar $_");
        is($r->get('foo'), "foobar $_");
    }

    ok($r->flushall);

    for(0..15){
        $r->select($_);
        ok(! $r->exists('foo'), "foo flushed from db$_");
    }

    for my $flush_db (0..15){
        for(0..15){
            $r->select($_);
            $r->set('foo', "foobar $_");
            is($r->get('foo'), "foobar $_");
        }

        $r->select($flush_db);
        $r->flushdb;

        ok(! $r->exists('foo'), "foo flushed from db$flush_db");

        for(0..15){
            next if $_ == $flush_db;
            $r->select($_);
            ok($r->exists('foo'), "foo not flushed from db$_");
        }
    }

    $r->select(0);  # go back to db0

    like($r->lastsave, qr/^\d+$/, 'lastsave returns digits');

    ok($r->save, 'save returns true');
    like($r->lastsave, qr/^\d+$/, 'lastsave returns digits');

    my $info = $r->info;
    is(ref $info, 'HASH', 'info returned a hash');

    #use Data::Dumper; diag Dumper $info;

    like($info->{last_save_time}, qr/^\d+$/, 'last save time is some digits');

    for(0..14){
        is($info->{"db$_"}, 'keys=1,expires=0', "db$_ info is correct");
    }
    # db15 was left with nothing in it, since it was the last one flushed
    is($info->{"db15"}, undef, 'info returns no data about databases with no keys');

    $r->setex("volitile-key-$_", 15, 'some value') for (1..5);


    is($r->info->{'db0'}, 'keys=6,expires=5', 'db0 info now has six keys and five expire');


    ok($r->quit, 'quit returns true');
    throws_ok { $r->quit } qr/^\QNot connected to any server\E\b/, '...even if we call it again';

    ok(! $r->ping, 'ping returns false after we quit');

    my $type = ref $r;
    my $r2 = $type->new(server => $srv);

    ok($r2->ping, 'we can ping our new redis client');

    $r2->shutdown;  # doesn't return anything

    ok(! $r2->ping, 'ping returns false after we shutdown');
}


done_testing();