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 my $assign_op = ("$]" < 5.037_003) ? 'sassign': 'padsv_store';
37 my $assign_op_cl = ("$]" < 5.037_003) ? 'B::BINOP': 'B::UNOP';
39 if ($op_info eq 'object') {
40 is_deeply { class => ref($op), name => $op->name },
41 { class => $assign_op_cl, name => $assign_op },
42 "op object in thread $tid is correct";
44 is $op, $assign_op, "op name in thread $tid is correct";
49 free => sub { lock $destroyed; ++$destroyed; 0 },
50 op_info => $op_info eq 'object' ? Variable::Magic::VMG_OP_INFO_OBJECT()
51 : Variable::Magic::VMG_OP_INFO_NAME()
54 is $@, '', "wizard in thread $tid doesn't croak";
55 isnt $wiz, undef, "wizard in thread $tid is defined";
56 is $c, 0, "wizard in thread $tid doesn't trigger magic";
63 my $res = eval { &Variable::Magic::cast(\$a, $wiz, sub { 5 }->()) };
64 is $@, '', "cast in thread $tid doesn't croak";
65 is $c, 0, "cast in thread $tid doesn't trigger magic";
72 is $@, '', "get in thread $tid doesn't croak";
73 is $b, 3, "get in thread $tid returns the right thing";
74 is $c, 1, "get in thread $tid triggers magic";
79 my $d = eval { &Variable::Magic::getdata(\$a, $wiz) };
80 is $@, '', "getdata in thread $tid doesn't croak";
81 is $d, 5 + $tid, "getdata in thread $tid returns the right thing";
82 is $c, 1, "getdata in thread $tid doesn't trigger magic";
88 is $@, '', "set in thread $tid (check opname) doesn't croak";
94 my $res = eval { &Variable::Magic::dispell(\$a, $wiz) };
95 is $@, '', "dispell in thread $tid doesn't croak";
96 is $c, 1, "dispell in thread $tid doesn't trigger magic";
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";
105 is $c, 1, "get in thread $tid after dispell doesn't trigger magic";
112 for my $dispell (1, 0) {
120 my @threads = map spawn(\&try, $dispell, $_), ('name') x 2, ('object') x 2;
121 for my $thr (@threads) {
122 my $res = $thr->join;
123 $completed += $res if defined $res;
128 is $destroyed, (1 - $dispell) * $completed, 'destructors';