Test/mysqld/Pool.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary App/Prove/Plugin/MySQLPool.pm »
1 package Test::mysqld::Pool;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 0 ▶use Test::mysqld;
Subroutine BEGIN: called CC=1 SCAR=2.6
6 use Cache::FastMmap;
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 my ($self) = @_;
39  
40 my @instances = Test::mysqld->start_mysqlds($self->jobs, my_cnf => $self->my_cnf);
41 $self->instances( \@instances );
42  
43 my $orig = $SIG{INT};
44 $SIG{INT} = sub {
45 Test::mysqld->stop_mysqlds(grep {defined($_)} @instances);
46 $self->instances([]) if $self;
47 if ($orig) {
48 $orig->();
49 } else {
50 $SIG{INT} = 'DEFAULT';
51 kill INT => $$;
52 }
53 };
54  
55 if ($self->preparer) {
56 $self->preparer->($_) for @instances;
57 }
58  
59 $self->cache->clear;
60 $self->cache->set( dsns => {
61 map { $_->dsn => 0 } @instances
62 });
63 }
64  
65 sub alloc {
66 my ($self) = @_;
67  
68 my $ret_dsn;
69 do {
70 $self->cache->get_and_set( dsns => sub {
71 my ($key, $val) = @_;
72  
73 for my $dsn (keys %$val) {
74 if ( $val->{ $dsn } == 0 ) {
75 # alloc one from unused
76 $ret_dsn = $dsn;
77 $val->{ $dsn } = $$; # record pid
78 return $val;
79 }
80 }
81  
82 return $val;
83 });
84  
85 return $ret_dsn if $ret_dsn;
86  
87 sleep 1;
88  
89 } while ( ! $ret_dsn );
90 }
91  
92 sub dealloc_unused {
93 my ($self) = @_;
94  
95 $self->cache->get_and_set( dsns => sub {
96 my ($key, $val) = @_;
97 for my $dsn (keys %$val) {
98  
99 my $pid = $val->{ $dsn }
100 or next;
101  
102 if ( ! $self->_pid_lives( $pid ) ) {
103 $val->{ $dsn } = 0; # dealloc
104 }
105 }
106  
107 return $val;
108 });
109 }
110  
111 sub _pid_lives {
112 my ($self, $pid) = @_;
113  
114 my $command = "ps -o pid -p $pid | grep $pid";
115 my @lines = qx{$command};
116 return scalar @lines;
117 }
118  
119 sub DESTROY {
120 my $self = shift;
121 Test::mysqld->stop_mysqlds(grep { defined($_) } @{$self->instances})
122 if $self->instances && $$ == $self->_owner_pid;
123 }
124  
125 1;
126  
127 __END__
↑ Summary App/Prove/Plugin/MySQLPool.pm »