7 use Variable::Magic::TestThreads;
9 use Test::More 'no_plan';
11 use Variable::Magic qw<
12 wizard cast dispell getdata
13 VMG_OP_INFO_NAME VMG_OP_INFO_OBJECT
16 my $destroyed : shared = 0;
22 my $desc = "wizard with op_info $op_info in main thread";
27 data => sub { $_[1] + threads->tid() },
28 get => sub { lock $c; ++$c; 0 },
31 my $tid = threads->tid();
33 if ($op_info == VMG_OP_INFO_OBJECT) {
34 is_deeply { class => ref($op), name => $op->name },
35 { class => 'B::BINOP', name => 'sassign' },
36 "op object in thread $tid is correct";
38 is $op, 'sassign', "op name in thread $tid is correct";
43 free => sub { lock $destroyed; ++$destroyed; 0 },
47 is $@, '', "$desc doesn't croak";
48 isnt $wiz, undef, "$desc is defined";
49 is $c, 0, "$desc doesn't trigger magic";
55 my ($dispell, $wiz) = @_;
56 my $tid = threads->tid;
62 my $res = eval { cast $a, $wiz, sub { 5 }->() };
63 is $@, '', "cast in thread $tid doesn't croak";
70 is $@, '', "get in thread $tid doesn't croak";
71 is $b, 3, "get in thread $tid returns the right thing";
76 my $d = eval { getdata $a, $wiz };
77 is $@, '', "getdata in thread $tid doesn't croak";
78 is $d, 5 + $tid, "getdata in thread $tid returns the right thing";
84 is $@, '', "set in thread $tid (check opname) doesn't croak";
90 my $res = eval { dispell $a, $wiz };
91 is $@, '', "dispell in thread $tid doesn't croak";
98 is $@, '', "get in thread $tid after dispell doesn't croak";
99 is $b, 9, "get in thread $tid after dispell returns the right thing";
106 my $wiz_name = spawn_wiz VMG_OP_INFO_NAME;
107 my $wiz_obj = spawn_wiz VMG_OP_INFO_OBJECT;
109 for my $dispell (1, 0) {
110 for my $wiz ($wiz_name, $wiz_obj) {
120 my @threads = map spawn(\&try, $dispell, $wiz), 1 .. 2;
121 $_->join for @threads;
125 is $c, 2, "get triggered twice";
129 is $destroyed, (1 - $dispell) * 2, 'destructors';
136 my $flag : shared = 0;
142 my $tid = threads->tid;
143 pass "set callback called in thread $tid"
145 free => sub { ++$destroyed },
151 @threads = map spawn(
153 my $tid = threads->tid;
154 my $exp = 456 + $tid;
157 threads::shared::cond_wait($flag) until $flag;
160 is $var, $exp, "\$var could be assigned to in thread $tid";
165 is $destroyed, 1, 'wizard is destroyed';
170 threads::shared::cond_broadcast($flag);
173 $_->join for @threads;