7 use VPIT::TestHelpers (
8 threads => [ 'Variable::Magic' => 'Variable::Magic::VMG_THREADSAFE()' ],
11 use Test::Leaner 'no_plan';
13 my $destroyed : shared = 0;
16 my ($dispell, $op_info) = @_;
17 my $tid = threads->tid;
24 eval { require Variable::Magic; 1 } or return;
30 Variable::Magic::wizard(
31 data => sub { $_[1] + $tid },
32 get => sub { ++$c; 0 },
36 if ($op_info eq '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 },
47 op_info => $op_info eq 'object' ? Variable::Magic::VMG_OP_INFO_OBJECT()
48 : Variable::Magic::VMG_OP_INFO_NAME()
51 is $@, '', "wizard in thread $tid doesn't croak";
52 isnt $wiz, undef, "wizard in thread $tid is defined";
53 is $c, 0, "wizard in thread $tid doesn't trigger magic";
60 my $res = eval { &Variable::Magic::cast(\$a, $wiz, sub { 5 }->()) };
61 is $@, '', "cast in thread $tid doesn't croak";
62 is $c, 0, "cast in thread $tid doesn't trigger magic";
69 is $@, '', "get in thread $tid doesn't croak";
70 is $b, 3, "get in thread $tid returns the right thing";
71 is $c, 1, "get in thread $tid triggers magic";
76 my $d = eval { &Variable::Magic::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";
79 is $c, 1, "getdata in thread $tid doesn't trigger magic";
85 is $@, '', "set in thread $tid (check opname) doesn't croak";
91 my $res = eval { &Variable::Magic::dispell(\$a, $wiz) };
92 is $@, '', "dispell in thread $tid doesn't croak";
93 is $c, 1, "dispell in thread $tid doesn't trigger magic";
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";
102 is $c, 1, "get in thread $tid after dispell doesn't trigger magic";
105 return; # Ugly if not here
108 for my $dispell (1, 0) {
114 my @threads = map spawn(\&try, $dispell, $_), ('name') x 2, ('object') x 2;
115 $_->join for @threads;
119 is $destroyed, (1 - $dispell) * 4, 'destructors';