Make cref registry thread-safe and a ton of tests
[p5sagit/Class-Accessor-Grouped.git] / t / accessors_xs_cachedwarn.t
index f7c2c6b..eecc8df 100644 (file)
@@ -1,3 +1,11 @@
+my $has_threads;
+BEGIN { eval '
+  use 5.008004; # older perls get confused by $SIG fiddling
+  use threads;
+  use threads::shared;
+  $has_threads = 1;
+' }
+
 use strict;
 use warnings;
 use Test::More;
@@ -20,20 +28,26 @@ BEGIN {
 
 use AccessorGroupsSubclass;
 
-my $obj = AccessorGroupsSubclass->new;
-my $deferred_stub = AccessorGroupsSubclass->can('singlefield');
-my $obj2 = AccessorGroups->new;
-
 my @w;
+share(@w) if $has_threads;
+
 {
-  local $SIG{__WARN__} = sub { push @w, @_ };
-  is ($obj->$deferred_stub(1), 1, 'Set');
-  is ($obj->$deferred_stub, 1, 'Get');
-  is ($obj->$deferred_stub(2), 2, 'ReSet');
-  is ($obj->$deferred_stub, 2, 'ReGet');
-
-  is ($obj->singlefield, 2, 'Normal get');
-  is ($obj2->singlefield, undef, 'Normal get on unrelated object');
+  my $obj = AccessorGroupsSubclass->new;
+  my $deferred_stub = AccessorGroupsSubclass->can('singlefield');
+  my $obj2 = AccessorGroups->new;
+
+  my $todo = sub {
+    local $SIG{__WARN__} = sub { push @w, @_ };
+    is ($obj->$deferred_stub(1), 1, 'Set');
+    is ($obj->$deferred_stub, 1, 'Get');
+    is ($obj->$deferred_stub(2), 2, 'ReSet');
+    is ($obj->$deferred_stub, 2, 'ReGet');
+
+    is ($obj->singlefield, 2, 'Normal get');
+    is ($obj2->singlefield, undef, 'Normal get on unrelated object');
+  };
+
+  $has_threads ? threads->create( $todo )->join : $todo->();
 }
 
 is (@w, 3, '3 warnings total');