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) {
122 my @threads = map spawn(\&try, $dispell, $wiz), 1 .. 2;
123 $_->join for @threads;
127 is $c, 2, "get triggered twice";
131 is $destroyed, (1 - $dispell) * 2, 'destructors';
138 my $flag : shared = 0;
144 my $tid = threads->tid;
145 pass "set callback called in thread $tid"
147 free => sub { ++$destroyed },
153 @threads = map spawn(
155 my $tid = threads->tid;
156 my $exp = 456 + $tid;
159 threads::shared::cond_wait($flag) until $flag;
162 is $var, $exp, "\$var could be assigned to in thread $tid";
167 is $destroyed, 1, 'wizard is destroyed';
172 threads::shared::cond_broadcast($flag);
175 $_->join for @threads;