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;
37 my ($dispell, $op_info) = @_;
38 my $tid = threads->tid();
41 wizard data => sub { $_[1] + $tid },
43 get => sub { ++$c; 0 },
46 $name = $name->name if $op_info == VMG_OP_INFO_OBJECT;
47 is $name, 'sassign', "opname for op_info $op_info in thread $tid is correct";
50 free => sub { ++$destroyed; 0 },
53 is($@, '', "wizard in thread $tid doesn't croak");
54 isnt($wiz, undef, "wizard in thread $tid is defined");
55 is($c, 0, "wizard in thread $tid doesn't trigger magic");
57 my $res = eval { cast $a, $wiz, sub { 5 }->() };
58 is($@, '', "cast in thread $tid doesn't croak");
59 is($c, 0, "cast in thread $tid doesn't trigger magic");
62 is($@, '', "get in thread $tid doesn't croak");
63 is($b, 3, "get in thread $tid returns the right thing");
64 is($c, 1, "get in thread $tid triggers magic");
65 my $d = eval { getdata $a, $wiz };
66 is($@, '', "getdata in thread $tid doesn't croak");
67 is($d, 5 + $tid, "getdata in thread $tid returns the right thing");
68 is($c, 1, "getdata in thread $tid doesn't trigger magic");
70 is($@, '', "set in thread $tid (check opname) doesn't croak");
72 $res = eval { dispell $a, $wiz };
73 is($@, '', "dispell in thread $tid doesn't croak");
74 is($c, 1, "dispell in thread $tid doesn't trigger magic");
77 is($@, '', "get in thread $tid after dispell doesn't croak");
78 is($b, 9, "get in thread $tid after dispell returns the right thing");
79 is($c, 1, "get in thread $tid after dispell doesn't trigger magic");
81 return; # Ugly if not here
84 for my $dispell (1, 0) {
85 for my $sig (undef, Variable::Magic::gensig()) {
87 my @t = map { threads->create(\&try, $dispell, $_) }
88 (VMG_OP_INFO_NAME) x 2, (VMG_OP_INFO_OBJECT) x 2;
90 is($destroyed, (1 - $dispell) * 4, 'destructors');