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;
21 my ($dispell, $op_info) = @_;
22 my $tid = threads->tid;
31 data => sub { $_[1] + $tid },
32 get => sub { ++$c; 0 },
36 if ($op_info == VMG_OP_INFO_OBJECT) {
37 is_deeply { class => ref($op), name => $op->name },
38 { class => 'B::BINOP', name => 'sassign' },
39 "op object in thread $tid is correct";
41 is $op, 'sassign', "op name in thread $tid is correct";
46 free => sub { lock $destroyed; ++$destroyed; 0 },
50 is $@, '', "wizard in thread $tid doesn't croak";
51 isnt $wiz, undef, "wizard in thread $tid is defined";
52 is $c, 0, "wizard in thread $tid doesn't trigger magic";
59 my $res = eval { cast $a, $wiz, sub { 5 }->() };
60 is $@, '', "cast in thread $tid doesn't croak";
61 is $c, 0, "cast in thread $tid doesn't trigger magic";
68 is $@, '', "get in thread $tid doesn't croak";
69 is $b, 3, "get in thread $tid returns the right thing";
70 is $c, 1, "get in thread $tid triggers magic";
75 my $d = eval { getdata $a, $wiz };
76 is $@, '', "getdata in thread $tid doesn't croak";
77 is $d, 5 + $tid, "getdata in thread $tid returns the right thing";
78 is $c, 1, "getdata in thread $tid doesn't trigger magic";
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";
92 is $c, 1, "dispell in thread $tid doesn't trigger magic";
99 is $@, '', "get in thread $tid after dispell doesn't croak";
100 is $b, 9, "get in thread $tid after dispell returns the right thing";
101 is $c, 1, "get in thread $tid after dispell doesn't trigger magic";
104 return; # Ugly if not here
107 for my $dispell (1, 0) {
113 my @threads = map spawn(\&try, $dispell, $_),
114 (VMG_OP_INFO_NAME) x 2, (VMG_OP_INFO_OBJECT) x 2;
115 $_->join for @threads;
119 is $destroyed, (1 - $dispell) * 4, 'destructors';