]> git.vpit.fr Git - perl/modules/Thread-Cleanup.git/blobdiff - t/10-join.t
Update VPIT::TestHelpers to 15e8aee3
[perl/modules/Thread-Cleanup.git] / t / 10-join.t
index 0a4ea4de61b8c4bda0267b87382825f38e75727c..a3be76159b774bd392d499623d3c3b9b58e47fee 100644 (file)
@@ -3,29 +3,16 @@
 use strict;
 use warnings;
 
-use Config qw/%Config/;
+use lib 't/lib';
+use VPIT::TestHelpers;
+use Thread::Cleanup::TestThreads;
 
-BEGIN {
- if (!$Config{useithreads}) {
-  require Test::More;
-  Test::More->import;
-  plan(skip_all => 'This perl wasn\'t built to support threads');
- }
-}
-
-use threads;
-use threads::shared;
-
-use Test::More tests => 5 * (2 + 2) + 1;
-
-BEGIN {
- defined and diag "Using threads $_"         for $threads::VERSION;
- defined and diag "Using threads::shared $_" for $threads::shared::VERSION;
-}
+use Test::More 'no_plan';
 
 use Thread::Cleanup;
 
 my %called : shared;
+my $destr  : shared;
 my %nums   : shared;
 
 our $x = -1;
@@ -42,8 +29,14 @@ Thread::Cleanup::register {
   lock %nums;
   $nums{$tid};
  };
-
  is $x, $num, "\$x in destructor of thread $tid";
+
+ my $gd = do {
+  lock $destr;
+  (defined $destr && $destr =~ /\[$tid\]/) ? 1 : undef;
+ };
+ is $gd, undef, "thread $tid destructor fires before global destruction";
+
  local $x = $tid;
 };
 
@@ -58,6 +51,14 @@ sub cb {
   $ran{$tid}++;
  }
 
+ my $immortal = VPIT::TestHelpers::Guard->new(sub {
+  # It seems we can't lock aggregates during global destruction, so we
+  # resort to using a string instead.
+  lock $destr;
+  $destr .= "[$tid]";
+ });
+ $immortal->{self} = $immortal;
+
  {
   lock %nums;
   $nums{$tid} = $y;
@@ -66,16 +67,15 @@ sub cb {
  local $x = -$tid;
 }
 
-my @tids;
 
-my @t = map {
+my @threads = map {
  local $x = $_;
- my $thr = threads->create(\&cb, $_);
- push @tids, $thr->tid;
- $thr;
+ spawn(\&cb, $_);
 } 0 .. 4;
 
-$_->join for @t;
+my @tids = map $_->tid, @threads;
+
+$_->join for @threads;
 
 is $x, -1, '$x in the main thread';