Skip to content

Commit

Permalink
Merge pull request #3 from shogo82148/test-mysqld-multi
Browse files Browse the repository at this point in the history
add impl Test::mysqld::Multi
  • Loading branch information
mackee committed Dec 20, 2016
2 parents fe959bd + fac1bfc commit 3621773
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 11 additions & 17 deletions lib/Test/mysqld/Pool.pm
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,23 @@ 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 => {
map { $_->dsn => 0 } @instances
});
}

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) = @_;

Expand Down Expand Up @@ -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__
Expand Down

0 comments on commit 3621773

Please sign in to comment.