File Coverage

blib/lib/Test/RedisDB.pm
Criterion Covered Total %
statement 23 46 50.0
branch 1 10 10.0
condition n/a
subroutine 7 14 50.0
pod 7 7 100.0
total 38 77 49.3


line stmt bran cond sub pod time code
1             package Test::RedisDB;
2 9     9   841840 use strict;
  9         86  
  9         286  
3 9     9   40 use warnings;
  9         12  
  9         406  
4             our $VERSION = "2.55";
5             $VERSION = eval $VERSION;
6              
7             =head1 NAME
8              
9             Test::RedisDB - start redis-server for testing
10              
11             =head1 SYNOPSIS
12              
13             use Test::RedisDB;
14              
15             my $test_server = Test::RedisDB->new;
16             my $redis = $test_server->redisdb_client;
17             $redis->set('foo', 1);
18             my $res = $redis->get('foo');
19              
20             =head1 DESCRIPTION
21              
22             This module allows you to start an instance of redis-server for testing your
23             modules that use RedisDB.
24              
25             =head1 METHODS
26              
27             =cut
28              
29 9     9   41 use File::Spec;
  9         13  
  9         148  
30 9     9   5630 use File::Temp;
  9         139964  
  9         569  
31 9     9   4065 use RedisDB;
  9         31  
  9         307  
32 9     9   4231 use Test::TCP;
  9         212717  
  9         4375  
33              
34             my $REDIS_SERVER = 'redis-server';
35             $REDIS_SERVER .= '.exe' if $^O eq 'MSWin32';
36              
37             =head2 $class->new(%options)
38              
39             start a new redis-server instance, return Test::RedisDB object tied to this
40             instance. Accepts the following options:
41              
42             =head3 password
43              
44             server should require a password to connect
45              
46             =cut
47              
48             sub new {
49 9     9 1 5591 my $class = shift;
50 9         30 my %args = @_;
51              
52             # check if we have redis-server
53 9 50       224 return unless grep { -x } map { File::Spec->catfile( $_, $REDIS_SERVER ) } File::Spec->path;
  81         823  
  81         429  
54              
55 0 0         my $requirepass = $args{password} ? "requirepass $args{password}" : "";
56 0           $args{dir} = File::Temp::tempdir( 'test_redisXXXXXX', TMPDIR => 1, CLEANUP => 0 );
57              
58 0           my $self = bless \%args, $class;
59             $self->{_t_tcp} = Test::TCP->new(
60             code => sub {
61 0     0     my $port = shift;
62 0           my $logfile = File::Spec->catfile( $args{dir}, "redis_test.log" );
63 0           my $cfg = <
64             daemonize no
65             port $port
66             timeout 0
67             loglevel notice
68             logfile $logfile
69             databases 2
70             dbfilename dump_test.rdb
71             dir $args{dir}
72             $requirepass
73             EOC
74 0 0         open my $cfg_fd, ">", File::Spec->catfile( $args{dir}, "redis.cfg" ) or die $!;
75 0           print $cfg_fd $cfg;
76 0           close $cfg_fd;
77 0           exec $REDIS_SERVER, File::Spec->catfile( $self->{dir}, "redis.cfg" );
78             },
79 0           );
80 0           $self->{port} = $self->{_t_tcp}->port;
81 0           return $self;
82             }
83              
84             =head2 $self->start
85              
86             start the server. You only need to call this method if you stopped the server
87             using the I method
88              
89             =cut
90              
91             sub start {
92 0     0 1   shift->{_t_tcp}->start;
93             }
94              
95             =head2 $self->stop
96              
97             stop the server
98              
99             =cut
100              
101             sub stop {
102 0     0 1   shift->{_t_tcp}->stop;
103             }
104              
105             =head2 $self->host
106              
107             return hostname or address, at the moment always 'localhost'
108              
109             =cut
110              
111             sub host {
112 0     0 1   'localhost';
113             }
114              
115             =head2 $self->port
116              
117             return port number on which server accept connections
118              
119             =cut
120              
121             sub port {
122 0     0 1   shift->{port};
123             }
124              
125             =head2 $self->redisdb_client(%options)
126              
127             return a new RedisDB client object connected to the test server, I<%options>
128             passed to RedisDB constructor
129              
130             =cut
131              
132             sub redisdb_client {
133 0     0 1   my $self = shift;
134              
135 0 0         if($self->{password}) {
136 0           unshift @_, password => $self->{password};
137             }
138              
139 0           return RedisDB->new(host => 'localhost', port => $self->{port}, @_);
140             }
141              
142             =head2 $self->url
143              
144             return URL for the server
145              
146             =cut
147              
148             sub url {
149 0     0 1   my $self = shift;
150              
151 0 0         return 'redis://' . ($self->{password} ? ":$self->{password}@" : "") . "localhost:$self->{port}";
152             }
153              
154             1;
155              
156             __END__