]> git.vpit.fr Git - perl/modules/autovivification.git/blob - t/51-threads-teardown.t
When a thread exits, teardown what we set up
[perl/modules/autovivification.git] / t / 51-threads-teardown.t
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use Config qw/%Config/;
7
8 BEGIN {
9  if (!$Config{useithreads}) {
10   require Test::More;
11   Test::More->import;
12   plan(skip_all => 'This perl wasn\'t built to support threads');
13  }
14 }
15
16 use threads;
17
18 use Test::More;
19
20 BEGIN {
21  require autovivification;
22  if (autovivification::A_THREADSAFE()) {
23   plan tests => 1;
24   defined and diag "Using threads $_" for $threads::VERSION;
25  } else {
26   plan skip_all => 'This autovivification isn\'t thread safe';
27  }
28 }
29
30 sub run_perl {
31  my $code = shift;
32
33  my $SystemRoot   = $ENV{SystemRoot};
34  local %ENV;
35  $ENV{SystemRoot} = $SystemRoot if $^O eq 'MSWin32' and defined $SystemRoot;
36
37  system { $^X } $^X, '-T', map("-I$_", @INC), '-e', $code;
38 }
39
40 SKIP:
41 {
42  skip 'Fails on 5.8.2 and lower' => 1 if $] <= 5.008002;
43
44  my $status = run_perl <<' RUN';
45   my $code = 1 + 2 + 4;
46   use threads;
47   $code -= threads->create(sub {
48    eval q{no autovivification; my $x; my $y = $x->{foo}; $x};
49    return defined($x) ? 0 : 1;
50   })->join;
51   $code -= defined(eval q{my $x; my $y = $x->{foo}; $x}) ? 2 : 0;
52   $code -= defined(eval q{no autovivification; my $x; my $y = $x->{foo}; $x})
53            ? 0 : 4;
54   exit $code;
55  RUN
56  is $status, 0, 'loading the pragma in a thread and using it outside doesn\'t segfault';
57 }