1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#line 1
package Test::SharedFork;
use strict;
use warnings;
use base 'Test::Builder::Module';
our $VERSION = '0.16';
use Test::Builder 0.32; # 0.32 or later is needed
use Test::SharedFork::Scalar;
use Test::SharedFork::Array;
use Test::SharedFork::Store;
use Config;
use 5.008000;
{
package #
Test::SharedFork::Contextual;
sub call {
my $code = shift;
my $wantarray = [caller(1)]->[5];
if ($wantarray) {
my @result = $code->();
bless {result => \@result, wantarray => $wantarray}, __PACKAGE__;
} elsif (defined $wantarray) {
my $result = $code->();
bless {result => $result, wantarray => $wantarray}, __PACKAGE__;
} else {
{ ; $code->(); } # void context
bless {wantarray => $wantarray}, __PACKAGE__;
}
}
sub result {
my $self = shift;
if ($self->{wantarray}) {
return @{ $self->{result} };
} elsif (defined $self->{wantarray}) {
return $self->{result};
} else {
return;
}
}
}
my $STORE;
BEGIN {
my $builder = __PACKAGE__->builder;
if( $] >= 5.008001 && $Config{useithreads} && $INC{'threads.pm'} ) {
die "# Current version of Test::SharedFork does not supports ithreads.";
}
if (Test::Builder->VERSION > 2.00) {
# new Test::Builder
$STORE = Test::SharedFork::Store->new();
our $level = 0;
for my $class (qw/Test::Builder2::History Test::Builder2::Counter/) {
my $meta = $class->meta;
my @methods = $meta->get_method_list;
my $orig =
$class eq 'Test::Builder2::History'
? $builder->{History}
: $builder->{History}->counter;
$orig->{test_sharedfork_hacked}++;
$STORE->set($class => $orig);
for my $method (@methods) {
next if $method =~ /^_/;
next if $method eq 'meta';
next if $method eq 'create';
next if $method eq 'singleton';
$meta->add_around_method_modifier(
$method => sub {
my ($code, $orig_self, @args) = @_;
return $orig_self->$code(@args) if (! ref $orig_self) || ! $orig_self->{test_sharedfork_hacked};
my $lock = $STORE->get_lock();
local $level = $level + 1;
my $self =
$level == 1 ? $STORE->get($class) : $orig_self;
my $ret = Test::SharedFork::Contextual::call(sub { $self->$code(@args) });
$STORE->set($class => $self);
return $ret->result;
},
);
}
}
} else {
# older Test::Builder
$STORE = Test::SharedFork::Store->new(
cb => sub {
my $store = shift;
tie $builder->{Curr_Test}, 'Test::SharedFork::Scalar',
$store, 'Curr_Test';
tie @{ $builder->{Test_Results} },
'Test::SharedFork::Array', $store, 'Test_Results';
},
init => +{
Test_Results => $builder->{Test_Results},
Curr_Test => $builder->{Curr_Test},
},
);
}
# make methods atomic.
no strict 'refs';
no warnings 'redefine';
for my $name (qw/ok skip todo_skip current_test/) {
my $orig = *{"Test::Builder::${name}"}{CODE};
*{"Test::Builder::${name}"} = sub {
local $Test::Builder::Level += 3;
my $lock = $STORE->get_lock(); # RAII
$orig->(@_);
};
};
}
{
# backward compatibility method
sub parent { }
sub child { }
sub fork { fork() }
}
1;
__END__
#line 184
|