File Coverage

blib/lib/Test/mysqld/Pool.pm
Criterion Covered Total %
statement 15 51 29.4
branch 0 12 0.0
condition 0 3 0.0
subroutine 5 12 41.6
pod 0 3 0.0
total 20 81 24.6


line stmt bran cond sub pod time code
1             package Test::mysqld::Pool;
2 1     1   9 use strict;
  1         3  
  1         32  
3 1     1   6 use warnings;
  1         2  
  1         29  
4 1     1   343 use Mouse;
  1         19285  
  1         5  
5 1     1   678 use Test::mysqld;
  1         44437  
  1         48  
6 1     1   624 use Cache::FastMmap;
  1         5964  
  1         504  
7              
8             has jobs => ( is => 'rw', isa => 'Int', );
9             has share_file => ( is => 'rw', isa => 'Str', required => 1 );
10             has cache => ( is => 'rw', lazy => 1,
11             default => sub {
12             my ($self) = @_;
13              
14             # only need this for atomical get_and_set
15             # is there anything better?
16              
17             # dont let Cache::FastMmap delete the share_file,
18             # File::Temp does that
19             return Cache::FastMmap->new(
20             share_file => $self->share_file,
21             init_file => 0,
22             empty_on_exit => 0,
23             unlink_on_exit => 0,
24             cache_size => '1k',
25             );
26             });
27             has preparer => ( is => 'rw', isa => 'Maybe[CodeRef]' );
28             has my_cnf => ( is => 'rw', isa => 'HashRef',
29             default => sub {
30             {
31             'skip-networking' => '', # no TCP socket
32             };
33             } );
34             has instances => ( is => 'rw', isa => 'ArrayRef' );
35             has _owner_pid => ( is => 'ro', isa => 'Int', default => sub { $$ } );
36              
37             sub prepare {
38 0     0 0   my ($self) = @_;
39              
40 0           my @instances = Test::mysqld->start_mysqlds($self->jobs, my_cnf => $self->my_cnf);
41 0           $self->instances( \@instances );
42 0 0         if ($self->preparer) {
43 0           $self->preparer->($_) for @instances;
44             }
45              
46 0           $self->cache->clear;
47             $self->cache->set( dsns => {
48 0           map { $_->dsn => 0 } @instances
  0            
49             });
50             }
51              
52             sub alloc {
53 0     0 0   my ($self) = @_;
54              
55 0           my $ret_dsn;
56 0           do {
57             $self->cache->get_and_set( dsns => sub {
58 0     0     my ($key, $val) = @_;
59              
60 0           for my $dsn (keys %$val) {
61 0 0         if ( $val->{ $dsn } == 0 ) {
62             # alloc one from unused
63 0           $ret_dsn = $dsn;
64 0           $val->{ $dsn } = $$; # record pid
65 0           return $val;
66             }
67             }
68              
69 0           return $val;
70 0           });
71              
72 0 0         return $ret_dsn if $ret_dsn;
73              
74 0           sleep 1;
75              
76             } while ( ! $ret_dsn );
77             }
78              
79             sub dealloc_unused {
80 0     0 0   my ($self) = @_;
81              
82             $self->cache->get_and_set( dsns => sub {
83 0     0     my ($key, $val) = @_;
84 0           for my $dsn (keys %$val) {
85              
86 0 0         my $pid = $val->{ $dsn }
87             or next;
88              
89 0 0         if ( ! $self->_pid_lives( $pid ) ) {
90 0           $val->{ $dsn } = 0; # dealloc
91             }
92             }
93              
94 0           return $val;
95 0           });
96             }
97              
98             sub _pid_lives {
99 0     0     my ($self, $pid) = @_;
100              
101 0           my $command = "ps -o pid -p $pid | grep $pid";
102 0           my @lines = qx{$command};
103 0           return scalar @lines;
104             }
105              
106             sub DESTROY {
107 0     0     my $self = shift;
108 0 0 0       Test::mysqld->stop_mysqlds(@{$self->instances})
  0            
109             if $self->instances && $$ == $self->_owner_pid;
110             }
111              
112             1;
113              
114             __END__