]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/41-clone.t
Work around stray exits in t/40-threads.t and t/41-clone.t
[perl/modules/Variable-Magic.git] / t / 41-clone.t
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use Variable::Magic qw<
7  wizard cast dispell getdata
8  VMG_OP_INFO_NAME VMG_OP_INFO_OBJECT
9 >;
10
11 use lib 't/lib';
12 use VPIT::TestHelpers (
13  threads => [ 'Variable::Magic' => 'Variable::Magic::VMG_THREADSAFE()' ],
14 );
15
16 use Test::Leaner 'no_plan';
17
18 my $destroyed : shared = 0;
19 my $c         : shared = 0;
20
21 sub spawn_wiz {
22  my ($op_info) = @_;
23
24  my $desc = "wizard with op_info $op_info in main thread";
25
26  local $@;
27  my $wiz = eval {
28   wizard(
29    data    => sub { $_[1] + threads->tid() },
30    get     => sub { lock $c; ++$c; 0 },
31    set     => sub {
32     my $op = $_[-1];
33     my $tid = threads->tid();
34
35     if ($op_info == VMG_OP_INFO_OBJECT) {
36      is_deeply { class => ref($op),   name => $op->name },
37                { class => 'B::BINOP', name => 'sassign' },
38                "op object in thread $tid is correct";
39     } else {
40      is $op, 'sassign', "op name in thread $tid is correct";
41     }
42
43     return 0
44    },
45    free    => sub { lock $destroyed; ++$destroyed; 0 },
46    op_info => $op_info,
47   );
48  };
49  is $@,     '',    "$desc doesn't croak";
50  isnt $wiz, undef, "$desc is defined";
51  is $c,     0,     "$desc doesn't trigger magic";
52
53  return $wiz;
54 }
55
56 sub try {
57  my ($dispell, $wiz) = @_;
58  my $tid = threads->tid;
59
60  my $a = 3;
61
62  {
63   local $@;
64   my $res = eval { cast $a, $wiz, sub { 5 }->() };
65   is $@, '', "cast in thread $tid doesn't croak";
66  }
67
68  {
69   local $@;
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  }
75
76  {
77   local $@;
78   my $d = eval { getdata $a, $wiz };
79   is $@, '',       "getdata in thread $tid doesn't croak";
80   is $d, 5 + $tid, "getdata in thread $tid returns the right thing";
81  }
82
83  {
84   local $@;
85   eval { $a = 9 };
86   is $@, '', "set in thread $tid (check opname) doesn't croak";
87  }
88
89  if ($dispell) {
90   {
91    local $@;
92    my $res = eval { dispell $a, $wiz };
93    is $@, '', "dispell in thread $tid doesn't croak";
94   }
95
96   {
97    local $@;
98    my $b;
99    eval { $b = $a };
100    is $@, '', "get in thread $tid after dispell doesn't croak";
101    is $b, 9,  "get in thread $tid after dispell returns the right thing";
102   }
103  }
104
105  return 1;
106 }
107
108 my $wiz_name = spawn_wiz VMG_OP_INFO_NAME;
109 my $wiz_obj  = spawn_wiz VMG_OP_INFO_OBJECT;
110
111 for my $dispell (1, 0) {
112  for my $wiz ($wiz_name, $wiz_obj) {
113   {
114    lock $c;
115    $c = 0;
116   }
117   {
118    lock $destroyed;
119    $destroyed = 0;
120   }
121
122   my $completed = 0;
123
124   my @threads = map spawn(\&try, $dispell, $wiz), 1 .. 2;
125   for my $thr (@threads) {
126    my $res = $thr->join;
127    $completed += $res if defined $res;
128   }
129
130   {
131    lock $c;
132    is $c, $completed, "get triggered twice";
133   }
134   {
135    lock $destroyed;
136    is $destroyed, (1 - $dispell) * $completed, 'destructors';
137   }
138  }
139 }
140
141 {
142  my @threads;
143  my $flag : shared = 0;
144  my $destroyed;
145
146  {
147   my $wiz = wizard(
148    set => sub {
149     my $tid = threads->tid;
150     pass "set callback called in thread $tid"
151    },
152    free => sub { ++$destroyed },
153   );
154
155   my $var = 123;
156   cast $var, $wiz;
157
158   @threads = map spawn(
159    sub {
160     my $tid = threads->tid;
161     my $exp = 456 + $tid;
162     {
163      lock $flag;
164      threads::shared::cond_wait($flag) until $flag;
165     }
166     $var = $exp;
167     is $var, $exp, "\$var could be assigned to in thread $tid";
168    }
169   ), 1 .. 5;
170  }
171
172  is $destroyed, 1, 'wizard is destroyed';
173
174  {
175   lock $flag;
176   $flag = 1;
177   threads::shared::cond_broadcast($flag);
178  }
179
180  $_->join for @threads;
181 }