~ubuntu-branches/ubuntu/oneiric/libanyevent-redis-perl/oneiric

« back to all changes in this revision

Viewing changes to t/Redis.pm

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghedini
  • Date: 2011-02-09 19:31:48 UTC
  • Revision ID: james.westby@ubuntu.com-20110209193148-asa3jeyaol6nikd2
Tags: upstream-0.23
ImportĀ upstreamĀ versionĀ 0.23

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package t::Redis;
 
2
use strict;
 
3
use Test::TCP;
 
4
use Test::More;
 
5
use AnyEvent::Redis;
 
6
use FindBin;
 
7
 
 
8
use base qw(Exporter);
 
9
our @EXPORT = qw(test_redis);
 
10
 
 
11
sub test_redis(&;$) {
 
12
    my $cb = shift;
 
13
    my $args = shift;
 
14
 
 
15
    chomp(my $redis_server = `which redis-server`);
 
16
    unless ($redis_server && -e $redis_server && -x _) {
 
17
        plan skip_all => 'redis-server not found in your PATH';
 
18
    }
 
19
 
 
20
    test_tcp
 
21
        server => sub {
 
22
            my $port = shift;
 
23
            rewrite_redis_conf($port);
 
24
            exec "redis-server", "t/redis.conf";
 
25
        },
 
26
        client => sub {
 
27
            my $port = shift;
 
28
            my $r = AnyEvent::Redis->new(
 
29
                host => "127.0.0.1",
 
30
                port => $port,
 
31
                ref $args ? %$args : ()
 
32
            );
 
33
            $cb->($r, $port);
 
34
        };
 
35
}
 
36
 
 
37
sub rewrite_redis_conf {
 
38
    my $port = shift;
 
39
    my $dir  = $FindBin::Bin;
 
40
 
 
41
    open my $in, "<", "t/redis.conf.base" or die $!;
 
42
    open my $out, ">", "t/redis.conf" or die $!;
 
43
 
 
44
    while (<$in>) {
 
45
        s/__PORT__/$port/;
 
46
        s/__DIR__/$dir/;
 
47
        print $out $_;
 
48
    }
 
49
}
 
50
 
 
51
END { unlink $_ for "t/redis.conf", "t/dump.rdb" }
 
52
 
 
53
1;
 
54