| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::Prove::Plugin::MySQLPool; | 
| 2 | 1 |  |  | 1 |  | 70110 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 29 |  | 
| 3 | 1 |  |  | 1 |  | 6 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 85 |  | 
| 4 | 1 |  |  | 1 |  | 757 | use File::Temp; | 
|  | 1 |  |  |  |  | 22487 |  | 
|  | 1 |  |  |  |  | 83 |  | 
| 5 | 1 |  |  | 1 |  | 469 | use POSIX::AtFork; | 
|  | 1 |  |  |  |  | 520 |  | 
|  | 1 |  |  |  |  | 48 |  | 
| 6 | 1 |  |  | 1 |  | 448 | use Test::mysqld::Pool; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 547 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | our $VERSION = '0.10'; | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | sub load { | 
| 11 | 0 |  |  | 0 | 0 |  | my ($class, $prove) = @_; | 
| 12 | 0 |  |  |  |  |  | my @args     = @{ $prove->{args} }; | 
|  | 0 |  |  |  |  |  |  | 
| 13 | 0 |  |  |  |  |  | my $preparer = $args[ 0 ]; | 
| 14 | 0 |  | 0 |  |  |  | my $jobs     = $prove->{ app_prove }->jobs || 1; | 
| 15 | 0 |  |  |  |  |  | my $lib      = $prove->{ app_prove }->lib; | 
| 16 | 0 |  |  |  |  |  | my $blib     = $prove->{ app_prove }->blib; | 
| 17 | 0 |  |  |  |  |  | my $includes = $prove->{ app_prove }->includes; | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 0 |  |  |  |  |  | my $share_file = File::Temp->new(); # deleted when DESTROYed | 
| 20 |  |  |  |  |  |  | my $pool       = Test::mysqld::Pool->new( | 
| 21 |  |  |  |  |  |  | jobs       => $jobs, | 
| 22 |  |  |  |  |  |  | share_file => $share_file->filename, | 
| 23 | 0 | 0 |  |  |  |  | ($preparer ? do { | 
| 24 | 0 |  |  |  |  |  | my @libs; | 
| 25 | 0 | 0 |  |  |  |  | push( @libs, 'lib' ) if $lib; | 
| 26 | 0 | 0 |  |  |  |  | push( @libs, 'blib/lib', 'blib/arch' ) if $blib; | 
| 27 | 0 | 0 |  |  |  |  | push( @libs, @$includes ) if @$includes; | 
| 28 | 0 |  |  |  |  |  | @libs = map { File::Spec->rel2abs($_) } @libs; | 
|  | 0 |  |  |  |  |  |  | 
| 29 | 0 |  |  |  |  |  | push( @INC, @libs ); | 
| 30 | 0 | 0 |  |  |  |  | eval "require $preparer" ## no critic | 
| 31 |  |  |  |  |  |  | or die "$@"; | 
| 32 |  |  |  |  |  |  | ( | 
| 33 |  |  |  |  |  |  | preparer => sub { | 
| 34 | 0 |  |  | 0 |  |  | my ($mysqld) = @_; | 
| 35 | 0 |  |  |  |  |  | $preparer->prepare( $mysqld ); | 
| 36 |  |  |  |  |  |  | }, | 
| 37 | 0 | 0 |  |  |  |  | $preparer->can('my_cnf') ? ( my_cnf => $preparer->my_cnf ) : (), | 
| 38 |  |  |  |  |  |  | ) | 
| 39 |  |  |  |  |  |  | } : ()), | 
| 40 |  |  |  |  |  |  | ); | 
| 41 | 0 |  |  |  |  |  | $pool->prepare; | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 0 |  |  |  |  |  | $prove->{ app_prove }{ __PACKAGE__ } = [ $pool, $share_file ]; # ref++ | 
| 44 | 0 |  |  |  |  |  | $prove->{ app_prove }->formatter('TAP::Formatter::MySQLPool'); | 
| 45 |  |  |  |  |  |  |  | 
| 46 | 0 |  |  |  |  |  | $ENV{ PERL_APP_PROVE_PLUGIN_MYSQLPOOL_SHARE_FILE } = $share_file->filename; | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 0 |  |  |  |  |  | POSIX::AtFork->add_to_child(create_child_hook($$)); | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 0 |  |  |  |  |  | 1; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | sub create_child_hook { | 
| 54 | 0 |  |  | 0 | 0 |  | my ($ppid) = @_; | 
| 55 |  |  |  |  |  |  | return sub { | 
| 56 | 0 |  |  | 0 |  |  | my ($call) = @_; | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | # we're in the test process | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | # prove uses 'fork' to create child processes | 
| 61 |  |  |  |  |  |  | # our own 'ps -o pid ...' uses 'backtick' | 
| 62 |  |  |  |  |  |  | # only hook 'fork' | 
| 63 | 0 | 0 |  |  |  |  | ($call eq 'fork') | 
| 64 |  |  |  |  |  |  | or return; | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | # restrict only direct child of prove | 
| 67 | 0 | 0 |  |  |  |  | (getppid() == $ppid) | 
| 68 |  |  |  |  |  |  | or return; | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | my $share_file = $ENV{ PERL_APP_PROVE_PLUGIN_MYSQLPOOL_SHARE_FILE } | 
| 71 | 0 | 0 |  |  |  |  | or return; | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 0 |  |  |  |  |  | my $dsn = Test::mysqld::Pool->new( share_file => $share_file )->alloc; | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | # use this in tests | 
| 76 | 0 |  |  |  |  |  | $ENV{ PERL_TEST_MYSQLPOOL_DSN } = $dsn; | 
| 77 | 0 |  |  |  |  |  | }; | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | { | 
| 81 |  |  |  |  |  |  | package TAP::Formatter::MySQLPool::Session; | 
| 82 | 1 |  |  | 1 |  | 9 | use parent 'TAP::Formatter::Console::Session'; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 9 |  | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | sub close_test { | 
| 85 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | my $share_file = $ENV{ PERL_APP_PROVE_PLUGIN_MYSQLPOOL_SHARE_FILE } | 
| 88 | 0 | 0 |  |  |  |  | or return; | 
| 89 | 0 |  |  |  |  |  | Test::mysqld::Pool->new( share_file => $share_file )->dealloc_unused; | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 0 |  |  |  |  |  | $self->SUPER::close_test(@_); | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | { | 
| 96 |  |  |  |  |  |  | package TAP::Formatter::MySQLPool; | 
| 97 | 1 |  |  | 1 |  | 5735 | use parent 'TAP::Formatter::Console'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | sub open_test { | 
| 100 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 101 |  |  |  |  |  |  |  | 
| 102 | 0 |  |  |  |  |  | bless $self->SUPER::open_test(@_), 'TAP::Formatter::MySQLPool::Session'; | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | 1; | 
| 107 |  |  |  |  |  |  | __END__ |