6 use Config qw/%Config/;
9 if (!$Config{useithreads}) {
12 plan(skip_all => 'This perl wasn\'t built to support threads');
16 use threads; # Before Test::More
21 use Variable::Magic qw/wizard cast dispell getdata VMG_THREADSAFE VMG_OP_INFO_NAME VMG_OP_INFO_OBJECT/;
24 plan tests => 2 * (4 * 18 + 1) + 2 * (4 * 13 + 1);
25 my $v = $threads::VERSION;
26 diag "Using threads $v" if defined $v;
27 $v = $threads::shared::VERSION;
28 diag "Using threads::shared $v" if defined $v;
30 plan skip_all => 'This Variable::Magic isn\'t thread safe';
33 my $destroyed : shared = 0;
36 my ($sig, $dispell, $op_info) = @_;
37 my $tid = threads->tid();
40 wizard data => sub { $_[1] + $tid },
42 get => sub { ++$c; 0 },
45 $name = $name->name if $op_info == VMG_OP_INFO_OBJECT;
46 is $name, 'sassign', "opname for op_info $op_info in thread $tid is correct";
49 free => sub { ++$destroyed; 0 },
52 is($@, '', "wizard in thread $tid doesn't croak");
53 isnt($wiz, undef, "wizard in thread $tid is defined");
54 is($c, 0, "wizard in thread $tid doesn't trigger magic");
56 my $res = eval { cast $a, $wiz, sub { 5 }->() };
57 is($@, '', "cast in thread $tid doesn't croak");
58 is($c, 0, "cast in thread $tid doesn't trigger magic");
61 is($@, '', "get in thread $tid doesn't croak");
62 is($b, 3, "get in thread $tid returns the right thing");
63 is($c, 1, "get in thread $tid triggers magic");
64 my $d = eval { getdata $a, $wiz };
65 is($@, '', "getdata in thread $tid doesn't croak");
66 is($d, 5 + $tid, "getdata in thread $tid returns the right thing");
67 is($c, 1, "getdata in thread $tid doesn't trigger magic");
69 is($@, '', "set in thread $tid (check opname) doesn't croak");
71 $res = eval { dispell $a, $wiz };
72 is($@, '', "dispell in thread $tid doesn't croak");
73 is($c, 1, "dispell in thread $tid doesn't trigger magic");
76 is($@, '', "get in thread $tid after dispell doesn't croak");
77 is($b, 9, "get in thread $tid after dispell returns the right thing");
78 is($c, 1, "get in thread $tid after dispell doesn't trigger magic");
80 return; # Ugly if not here
83 for my $dispell (1, 0) {
84 for my $sig (undef, Variable::Magic::gensig()) {
86 my @t = map { threads->create(\&try, $sig, $dispell, $_) }
87 (VMG_OP_INFO_NAME) x 2, (VMG_OP_INFO_OBJECT) x 2;
89 is($destroyed, (1 - $dispell) * 4, 'destructors');