Ensure threads terminate properly
[p5sagit/Class-Accessor-Grouped.git] / t / accessors_xs.t
index eecf45c..63bce81 100644 (file)
@@ -1,5 +1,14 @@
+my $has_threads;
+BEGIN { eval '
+  use 5.008005; # older perls segfault on threading under CXSA
+  use threads;
+  use threads::shared;
+  $has_threads = 1;
+' }
+
 use strict;
 use warnings;
+no warnings 'once';
 use FindBin qw($Bin);
 use File::Spec::Functions;
 use File::Spec::Unix (); # need this for %INC munging
@@ -27,10 +36,13 @@ $Class::Accessor::Grouped::USE_XS = 1;
 
 for my $tname (qw/accessors.t accessors_ro.t accessors_wo.t/) {
 
-  for (1,2) {
-    note "\nTesting $tname with USE_XS (pass $_)\n\n";
+  my $pass = 1;
+  share($pass) if $has_threads;
+
+  my $todo = sub {
+    note "\nTesting $tname with USE_XS (pass @{[ $pass++ ]})\n\n";
 
-    my $tfn = catfile($Bin, $tname);
+    my ($tfn) = catfile($Bin, $tname) =~ /(.+)/;
 
     for (
       qw|AccessorGroups.pm AccessorGroups/BeenThereDoneThat.pm AccessorGroupsRO.pm AccessorGroupsSubclass.pm AccessorGroupsParent.pm AccessorGroupsWO.pm|,
@@ -46,6 +58,32 @@ for my $tname (qw/accessors.t accessors_ro.t accessors_wo.t/) {
     local $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ /subroutine .+ redefined/i };
 
     do($tfn);
+
+    666;
+  };
+
+  if ($has_threads) {
+    for (1,2) {
+      is (
+        threads->create(sub {
+          is (
+            threads->create(sub {
+              $todo->();
+            })->join,
+            666,
+            'Innner thread joined ok',
+          );
+          777;
+        })->join,
+        777,
+        'Outer thread joined ok',
+      );
+
+      is ($todo->(), 666, "Unthreaded run ok") for (1,2);
+    }
+  }
+  else {
+    is ($todo->(), 666, "Unthreaded run ok") for (1,2);
   }
 }