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);
26 plan skip_all => 'This Variable::Magic isn\'t thread safe';
29 my $destroyed : shared = 0;
34 my $tid = threads->tid();
37 wizard get => sub { ++$c },
38 data => sub { $_[1] + $tid },
39 free => sub { ++$destroyed },
42 is($@, '', "wizard in thread $tid doesn't croak");
43 isnt($wiz, undef, "wizard in thread $tid is defined");
44 is($c, 0, "wizard in thread $tid doesn't trigger magic");
46 my $res = eval { cast $a, $wiz, sub { 5 }->() };
47 is($@, '', "cast in thread $tid doesn't croak");
48 is($c, 0, "cast in thread $tid doesn't trigger magic");
51 is($@, '', "get in thread $tid doesn't croak");
52 is($b, 3, "get in thread $tid returns the right thing");
53 is($c, 1, "get in thread $tid triggers magic");
54 my $d = eval { getdata $a, $wiz };
55 is($@, '', "getdata in thread $tid doesn't croak");
56 is($d, 5 + $tid, "getdata in thread $tid returns the right thing");
57 is($c, 1, "getdata in thread $tid doesn't trigger magic");
58 $res = eval { dispell $a, $wiz };
59 is($@, '', "dispell in thread $tid doesn't croak");
60 is($c, 1, "dispell in thread $tid doesn't trigger magic");
63 is($@, '', "get in thread $tid after dispell doesn't croak");
64 is($b, 3, "get in thread $tid after dispell returns the right thing");
65 is($c, 1, "get in thread $tid after dispell doesn't trigger magic");
69 my @t = map { threads->create(\&try) } 1 .. 2;
73 is($destroyed, 0, 'destructors didn\'t fired');
76 $sig = Variable::Magic::gensig();
78 @t = map { threads->create(\&try) } 1 .. 2;
82 is($destroyed, 0, 'destructors didn\'t fired');