]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blobdiff - t/41-clone.t
Lock shared variables
[perl/modules/Variable-Magic.git] / t / 41-clone.t
index 90f15e0fc08fceb517af25711869b441f1862524..a10a35611b590fd6d8ef1c1d774086448b695994 100644 (file)
@@ -38,15 +38,20 @@ sub spawn_wiz {
 
  my $wiz = eval {
   wizard data    => sub { $_[1] + threads->tid() },
-         get     => sub { ++$c; 0 },
+         get     => sub { lock $c; ++$c; 0 },
          set     => sub {
-                     my $name = $_[-1];
-                     $name = $name->name if $op_info == VMG_OP_INFO_OBJECT;
+                     my $op = $_[-1];
                      my $tid = threads->tid();
-                     is $name, 'sassign', "opname for op_info $op_info in thread $tid is correct";
+                     if ($op_info == VMG_OP_INFO_OBJECT) {
+                      is_deeply { class => ref($op),   name => $op->name },
+                                { class => 'B::BINOP', name => 'sassign' },
+                                "op object in thread $tid is correct";
+                     } else {
+                      is $op, 'sassign', "op name in thread $tid is correct";
+                     }
                      0
                     },
-         free    => sub { ++$destroyed; 0 },
+         free    => sub { lock $destroyed; ++$destroyed; 0 },
          op_info => $op_info
  };
  is($@,     '',    "wizard with op_info $op_info in main thread doesn't croak");
@@ -87,13 +92,25 @@ my $wiz_obj  = spawn_wiz VMG_OP_INFO_OBJECT;
 
 for my $dispell (1, 0) {
  for my $sig ($wiz_name, getsig($wiz_name), $wiz_obj, getsig($wiz_obj)) {
-  $c = 0;
-  $destroyed = 0;
+  {
+   lock $c;
+   $c = 0;
+  }
+  {
+   lock $destroyed;
+   $destroyed = 0;
+  }
 
   my @t = map { threads->create(\&try, $dispell, $sig) } 1 .. 2;
   $_->join for @t;
 
-  is($c, 2, "get triggered twice");
-  is($destroyed, (1 - $dispell) * 2, 'destructors');
+  {
+   lock $c;
+   is $c, 2, "get triggered twice";
+  }
+  {
+   lock $destroyed;
+   is $destroyed, (1 - $dispell) * 2, 'destructors';
+  }
  }
 }