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 my $assign_op = ("$]" < 5.037_003) ? 'sassign': 'padsv_store';
36 my $assign_op_cl = ("$]" < 5.037_003) ? 'B::BINOP': 'B::UNOP';
38 if ($op_info == VMG_OP_INFO_OBJECT) {
39 is_deeply { class => ref($op), name => $op->name },
40 { class => $assign_op_cl, name => $assign_op },
41 "op object in thread $tid is correct";
43 is $op, $assign_op, "op name in thread $tid is correct";
48 free => sub { lock $destroyed; ++$destroyed; 0 },
52 is $@, '', "$desc doesn't croak";
53 isnt $wiz, undef, "$desc is defined";
54 is $c, 0, "$desc doesn't trigger magic";
60 my ($dispell, $wiz) = @_;
61 my $tid = threads->tid;
67 my $res = eval { cast $a, $wiz, sub { 5 }->() };
68 is $@, '', "cast in thread $tid doesn't croak";
75 is $@, '', "get in thread $tid doesn't croak";
76 is $b, 3, "get in thread $tid returns the right thing";
81 my $d = eval { getdata $a, $wiz };
82 is $@, '', "getdata in thread $tid doesn't croak";
83 is $d, 5 + $tid, "getdata in thread $tid returns the right thing";
89 is $@, '', "set in thread $tid (check opname) doesn't croak";
95 my $res = eval { dispell $a, $wiz };
96 is $@, '', "dispell in thread $tid doesn't croak";
103 is $@, '', "get in thread $tid after dispell doesn't croak";
104 is $b, 9, "get in thread $tid after dispell returns the right thing";
111 my $wiz_name = spawn_wiz VMG_OP_INFO_NAME;
112 my $wiz_obj = spawn_wiz VMG_OP_INFO_OBJECT;
114 for my $dispell (1, 0) {
115 for my $wiz ($wiz_name, $wiz_obj) {
127 my @threads = map spawn(\&try, $dispell, $wiz), 1 .. 2;
128 for my $thr (@threads) {
129 my $res = $thr->join;
130 $completed += $res if defined $res;
135 is $c, $completed, "get triggered twice";
139 is $destroyed, (1 - $dispell) * $completed, 'destructors';
146 my $flag : shared = 0;
152 my $tid = threads->tid;
153 pass "set callback called in thread $tid"
155 free => sub { ++$destroyed },
161 @threads = map spawn(
163 my $tid = threads->tid;
164 my $exp = 456 + $tid;
167 threads::shared::cond_wait($flag) until $flag;
170 is $var, $exp, "\$var could be assigned to in thread $tid";
175 is $destroyed, 1, 'wizard is destroyed';
180 threads::shared::cond_broadcast($flag);
183 $_->join for @threads;