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/;
24 plan tests => 2 * (2 * 16 + 1) + 2 * (2 * 11 + 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;
38 my $tid = threads->tid();
41 wizard get => sub { ++$c },
42 data => sub { $_[1] + $tid },
43 free => sub { ++$destroyed },
46 is($@, '', "wizard in thread $tid doesn't croak");
47 isnt($wiz, undef, "wizard in thread $tid is defined");
48 is($c, 0, "wizard in thread $tid doesn't trigger magic");
50 my $res = eval { cast $a, $wiz, sub { 5 }->() };
51 is($@, '', "cast in thread $tid doesn't croak");
52 is($c, 0, "cast in thread $tid doesn't trigger magic");
55 is($@, '', "get in thread $tid doesn't croak");
56 is($b, 3, "get in thread $tid returns the right thing");
57 is($c, 1, "get in thread $tid triggers magic");
58 my $d = eval { getdata $a, $wiz };
59 is($@, '', "getdata in thread $tid doesn't croak");
60 is($d, 5 + $tid, "getdata in thread $tid returns the right thing");
61 is($c, 1, "getdata in thread $tid doesn't trigger magic");
63 $res = eval { dispell $a, $wiz };
64 is($@, '', "dispell in thread $tid doesn't croak");
65 is($c, 1, "dispell in thread $tid doesn't trigger magic");
68 is($@, '', "get in thread $tid after dispell doesn't croak");
69 is($b, 3, "get in thread $tid after dispell returns the right thing");
70 is($c, 1, "get in thread $tid after dispell doesn't trigger magic");
72 return; # Ugly if not here
75 for my $dispell (1, 0) {
79 my @t = map { threads->create(\&try, $dispell) } 1 .. 2;
83 is($destroyed, (1 - $dispell) * 2, 'destructors');
86 $sig = Variable::Magic::gensig();
88 @t = map { threads->create(\&try, $dispell) } 1 .. 2;
92 is($destroyed, (1 - $dispell) * 2, 'destructors');