Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

threads and subtests #145

Closed
chorankates opened this issue Jul 7, 2011 · 4 comments
Closed

threads and subtests #145

chorankates opened this issue Jul 7, 2011 · 4 comments

Comments

@chorankates
Copy link

sample code:

#!/usr/bin/perl -w

use strict;
use warnings;
use threads;
use Test::More qw(no_plan);

sub foo { 
    my $bar = 1;
    my $baz = 2;

    subtest 'barbaz' => sub {
         is ($bar, $baz, 'bar equals baz');
         is ($bar, $bar, 'bar equals bar');
    };

}

my $pid = fork();

if ($pid) { 
    foo();
    waitpid($pid, 0);
} 

if ($pid == 0) { 
    foo();
    exit(0);
}

If run as is, output is:

$ perl foo.pl
lock can only be used on shared values at /usr/local/share/perl/5.10.1/Test/Builder.pm line 775.
    # Child (barbaz) exited without calling finalize()
lock can only be used on shared values at /usr/local/share/perl/5.10.1/Test/Builder.pm line 775.
        (in cleanup) lock can only be used on shared values at /usr/local/share/perl/5.10.1/Test/Builder.pm line 775.
# Looks like your test exited with 29 before it could output anything.
    # Child (barbaz) exited without calling finalize()
        (in cleanup) lock can only be used on shared values at /usr/local/share/perl/5.10.1/Test/Builder.pm line 775.

If you comment out the 'subtest' line and the corresponding '};' line, you get:

$ perl foo.pl
not ok 1 - bar equals baz
not ok 1 - bar equals baz
#   Failed test 'bar equals baz'
#   Failed test 'bar equals baz'
#   at foo.pl line 11.
#   at foo.pl line 11.
#          got: '1'
#     expected: '2'
#          got: '1'
#     expected: '2'
ok 2 - bar equals bar
ok 2 - bar equals bar
1..2

As the documentation specifies, 'use threads' is before 'use Test::More', but if a subtest is involved, tests do not run as expected

@schwern
Copy link
Contributor

schwern commented Aug 13, 2011

@tokuhirom #147 was a duplicate.

@schwern
Copy link
Contributor

schwern commented Aug 13, 2011

Yep, it's a bug. Looks like something in the subtest code didn't get declared shared. Feel free to dig in.

@schwern
Copy link
Contributor

schwern commented Oct 16, 2011

I can cut this down to pretty much any subtest with threads on. The issue is the way subtest copies the Builder object, it loses the shared-ness of it's keys.

schwern added a commit that referenced this issue Oct 18, 2011
child() was improperly creating it's own object and calling reset() manually and then copying
filehandles from the parent directly rather than going through the accessors.  Now it uses
create() and the accessors.

Related... fix Test::Builder::NoOutput->create so it doesn't use global filehandles and blow
over them every time create() is called (as by a child).

The reset_outputs() in the basic subtest test were a symptom of this confusion and are
no longer needed.

For #145
schwern added a commit that referenced this issue Oct 18, 2011
The shared nature of a scalar value, in this case $self->{Curr_Test} is lost across
a simple hash copy as subtest() was doing.  So we reshare them on copy.

For #145

Signed-off-by: Michael G. Schwern <schwern@pobox.com>
@schwern
Copy link
Contributor

schwern commented Oct 18, 2011

That should do it. It'll be in the next release coming soon.

@schwern schwern closed this as completed Oct 18, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants