]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/41-clone.t
Lock shared variables
[perl/modules/Variable-Magic.git] / t / 41-clone.t
1 #!perl -T
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; # Before Test::More
17 use threads::shared;
18
19 use Test::More;
20
21 use Variable::Magic qw/wizard cast dispell getdata getsig VMG_THREADSAFE VMG_OP_INFO_NAME VMG_OP_INFO_OBJECT/;
22
23 if (VMG_THREADSAFE) {
24  plan tests => 2 * 3 + 4 * (2 * 10 + 2) + 4 * (2 * 7 + 2);
25  my $v = $threads::VERSION;
26  diag "Using threads $v" if defined $v;
27  $v = $threads::shared::VERSION;
28  diag "Using threads::shared $v" if defined $v;
29 } else {
30  plan skip_all => 'This Variable::Magic isn\'t thread safe';
31 }
32
33 my $destroyed : shared = 0;
34 my $c         : shared = 0;
35
36 sub spawn_wiz {
37  my ($op_info) = @_;
38
39  my $wiz = eval {
40   wizard data    => sub { $_[1] + threads->tid() },
41          get     => sub { lock $c; ++$c; 0 },
42          set     => sub {
43                      my $op = $_[-1];
44                      my $tid = threads->tid();
45                      if ($op_info == VMG_OP_INFO_OBJECT) {
46                       is_deeply { class => ref($op),   name => $op->name },
47                                 { class => 'B::BINOP', name => 'sassign' },
48                                 "op object in thread $tid is correct";
49                      } else {
50                       is $op, 'sassign', "op name in thread $tid is correct";
51                      }
52                      0
53                     },
54          free    => sub { lock $destroyed; ++$destroyed; 0 },
55          op_info => $op_info
56  };
57  is($@,     '',    "wizard with op_info $op_info in main thread doesn't croak");
58  isnt($wiz, undef, "wizard with op_info $op_info in main thread is defined");
59  is($c,     0,     "wizard with op_info $op_info in main thread doesn't trigger magic");
60
61  return $wiz;
62 }
63
64 sub try {
65  my ($dispell, $sig) = @_;
66  my $tid = threads->tid();
67  my $a   = 3;
68  my $res = eval { cast $a, $sig, sub { 5 }->() };
69  is($@, '', "cast in thread $tid doesn't croak");
70  my $b;
71  eval { $b = $a };
72  is($@, '', "get in thread $tid doesn't croak");
73  is($b, 3,  "get in thread $tid returns the right thing");
74  my $d = eval { getdata $a, $sig };
75  is($@, '',       "getdata in thread $tid doesn't croak");
76  is($d, 5 + $tid, "getdata in thread $tid returns the right thing");
77  eval { $a = 9 };
78  is($@, '', "set in thread $tid (check opname) doesn't croak");
79  if ($dispell) {
80   $res = eval { dispell $a, $sig };
81   is($@, '', "dispell in thread $tid doesn't croak");
82   undef $b;
83   eval { $b = $a };
84   is($@, '', "get in thread $tid after dispell doesn't croak");
85   is($b, 9,  "get in thread $tid after dispell returns the right thing");
86  }
87  return; # Ugly if not here
88 }
89
90 my $wiz_name = spawn_wiz VMG_OP_INFO_NAME;
91 my $wiz_obj  = spawn_wiz VMG_OP_INFO_OBJECT;
92
93 for my $dispell (1, 0) {
94  for my $sig ($wiz_name, getsig($wiz_name), $wiz_obj, getsig($wiz_obj)) {
95   {
96    lock $c;
97    $c = 0;
98   }
99   {
100    lock $destroyed;
101    $destroyed = 0;
102   }
103
104   my @t = map { threads->create(\&try, $dispell, $sig) } 1 .. 2;
105   $_->join for @t;
106
107   {
108    lock $c;
109    is $c, 2, "get triggered twice";
110   }
111   {
112    lock $destroyed;
113    is $destroyed, (1 - $dispell) * 2, 'destructors';
114   }
115  }
116 }