6 use threads; # Before Test::More
11 use Variable::Magic qw/wizard cast dispell getdata VMG_THREADSAFE/;
14 plan tests => 2 * 16 + 1;
16 plan skip_all => 'This Variable::Magic isn\'t thread safe';
19 my $destroyed : shared = 0;
22 my $tid = threads->tid();
25 wizard get => sub { ++$c },
26 data => sub { $_[1] + $tid },
27 free => sub { ++$destroyed };
29 is($@, '', "wizard in thread $tid doesn't croak");
30 isnt($wiz, undef, "wizard in thread $tid is defined");
31 is($c, 0, "wizard in thread $tid doesn't trigger magic");
33 my $res = eval { cast $a, $wiz, sub { 5 }->() };
34 is($@, '', "cast in thread $tid doesn't croak");
35 is($c, 0, "cast in thread $tid doesn't trigger magic");
38 is($@, '', "get in thread $tid doesn't croak");
39 is($b, 3, "get in thread $tid returns the right thing");
40 is($c, 1, "get in thread $tid triggers magic");
41 my $d = eval { getdata $a, $wiz };
42 is($@, '', "getdata in thread $tid doesn't croak");
43 is($d, 5 + $tid, "getdata in thread $tid returns the right thing");
44 is($c, 1, "getdata in thread $tid doesn't trigger magic");
45 $res = eval { dispell $a, $wiz };
46 is($@, '', "dispell in thread $tid doesn't croak");
47 is($c, 1, "dispell in thread $tid doesn't trigger magic");
50 is($@, '', "get in thread $tid after dispell doesn't croak");
51 is($b, 3, "get in thread $tid after dispell returns the right thing");
52 is($c, 1, "get in thread $tid after dispell doesn't trigger magic");
56 my @t = map { threads->create(\&try) } 1 .. 2;
60 is($destroyed, 0, 'destructors didn\'t fired');