diff --git a/cpanfile b/cpanfile index 11f7d7f..404c07a 100644 --- a/cpanfile +++ b/cpanfile @@ -3,7 +3,7 @@ requires 'Cache::FastMmap'; requires 'File::Temp'; requires 'Mouse'; requires 'POSIX::AtFork'; -requires 'Test::mysqld'; +requires 'Test::mysqld', '0.20'; requires 'parent'; on build => sub { diff --git a/lib/Test/mysqld/Pool.pm b/lib/Test/mysqld/Pool.pm index 5a35e9e..9d92db8 100644 --- a/lib/Test/mysqld/Pool.pm +++ b/lib/Test/mysqld/Pool.pm @@ -32,14 +32,16 @@ has my_cnf => ( is => 'rw', isa => 'HashRef', }; } ); has instances => ( is => 'rw', isa => 'ArrayRef' ); +has _owner_pid => ( is => 'ro', isa => 'Int', default => sub { $$ } ); sub prepare { my ($self) = @_; - my @instances = map { - $self->_launch_instance; - } (1 .. $self->jobs); + my @instances = Test::mysqld->start_mysqlds($self->jobs, my_cnf => $self->my_cnf); $self->instances( \@instances ); + if ($self->preparer) { + $self->preparer->($_) for @instances; + } $self->cache->clear; $self->cache->set( dsns => { @@ -47,20 +49,6 @@ sub prepare { }); } -sub _launch_instance { - my ($self) = @_; - - # auto start - my $mysqld = Test::mysqld->new( my_cnf => $self->my_cnf ) - or die $Test::mysqld::errstr; - - # user code to prepare database before test - $self->preparer->( $mysqld ) - if $self->preparer; - - return $mysqld; -} - sub alloc { my ($self) = @_; @@ -115,6 +103,12 @@ sub _pid_lives { return scalar @lines; } +sub DESTROY { + my $self = shift; + Test::mysqld->stop_mysqlds(@{$self->instances}) + if $self->instances && $$ == $self->_owner_pid; +} + 1; __END__