6 use Variable::Magic qw<
7 wizard cast dispell getdata
8 VMG_OP_INFO_NAME VMG_OP_INFO_OBJECT
12 use VPIT::TestHelpers (
13 threads => [ 'Variable::Magic' => 'Variable::Magic::VMG_THREADSAFE()' ],
16 use Test::Leaner 'no_plan';
18 my $destroyed : shared = 0;
24 my $desc = "wizard with op_info $op_info in main thread";
29 data => sub { $_[1] + threads->tid() },
30 get => sub { lock $c; ++$c; 0 },
33 my $tid = threads->tid();
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";
40 is $op, 'sassign', "op name in thread $tid is correct";
45 free => sub { lock $destroyed; ++$destroyed; 0 },
49 is $@, '', "$desc doesn't croak";
50 isnt $wiz, undef, "$desc is defined";
51 is $c, 0, "$desc doesn't trigger magic";
57 my ($dispell, $wiz) = @_;
58 my $tid = threads->tid;
64 my $res = eval { cast $a, $wiz, sub { 5 }->() };
65 is $@, '', "cast in thread $tid doesn't croak";
72 is $@, '', "get in thread $tid doesn't croak";
73 is $b, 3, "get in thread $tid returns the right thing";
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";
86 is $@, '', "set in thread $tid (check opname) doesn't croak";
92 my $res = eval { dispell $a, $wiz };
93 is $@, '', "dispell in thread $tid doesn't croak";
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";
108 my $wiz_name = spawn_wiz VMG_OP_INFO_NAME;
109 my $wiz_obj = spawn_wiz VMG_OP_INFO_OBJECT;
111 for my $dispell (1, 0) {
112 for my $wiz ($wiz_name, $wiz_obj) {
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;
132 is $c, $completed, "get triggered twice";
136 is $destroyed, (1 - $dispell) * $completed, 'destructors';
143 my $flag : shared = 0;
149 my $tid = threads->tid;
150 pass "set callback called in thread $tid"
152 free => sub { ++$destroyed },
158 @threads = map spawn(
160 my $tid = threads->tid;
161 my $exp = 456 + $tid;
164 threads::shared::cond_wait($flag) until $flag;
167 is $var, $exp, "\$var could be assigned to in thread $tid";
172 is $destroyed, 1, 'wizard is destroyed';
177 threads::shared::cond_broadcast($flag);
180 $_->join for @threads;