Last active
March 19, 2019 11:46
-
-
Save aklaswad/edf8659d876b082e8c18ca4619f4e909 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Test::More; | |
use Data::ObjectDriver::Driver::DBI; | |
use Parallel::ForkManager; | |
sub main { | |
# Init dbh and make sure it was connected by parent process | |
my $driver = Data::ObjectDriver::Driver::DBI->new( | |
dsn => 'dbi:mysql:database=test', | |
username => 'root', | |
reuse_dbh => 1, | |
); | |
my $dbh = $driver->rw_handle; | |
$dbh->do('SELECT 1'); | |
$driver->begin_work; | |
ok( $driver->txn_active, 'txn active' ); | |
# Prepare fork manager | |
my $pm = Parallel::ForkManager->new( 10 ); | |
$pm->run_on_finish( sub { | |
my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data) = @_; | |
if (defined $data) { | |
my ($ret,$i, $txn) = @$data; | |
is( $ret, $i, "Can get client unique value from ${i}th child" ); | |
ok( !$txn, 'In child process, txn is not active just after forked'); | |
} | |
}); | |
for my $i (1..10) { | |
$pm->start and next; | |
my $txn = $driver->txn_active; | |
my $dbh = $driver->rw_handle; | |
$dbh->begin_work; | |
$dbh->do('SELECT LAST_INSERT_ID(' . $i . ');'); | |
sleep 1; | |
my ($ret) = $dbh->selectrow_array('SELECT LAST_INSERT_ID();'); | |
$dbh->rollback; | |
$pm->finish(0, [$ret,$i, $txn]); | |
} | |
$pm->wait_all_children; | |
ok( $driver->txn_active, 'txn is still active for parent' ); | |
is( $dbh->selectrow_array('SELECT 2'), 2, 'parent dbh is still alive'); | |
$dbh->commit; | |
} | |
if ( $Data::ObjectDriver::Driver::DBI::HasWeaken | |
&& $Data::ObjectDriver::Driver::DBI::HasAtFork ) { | |
plan tests => 23; | |
main(); | |
} | |
else { | |
plan skip_all => 'POSIX::AtFork and/or Scalar::Util was not installed. Skip fork-safe test.'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment