9 Test::More::plan(skip_all => $msg);
12 use Config qw/%Config/;
15 my $force = $ENV{PERL_VARIABLE_MAGIC_TEST_THREADS} ? 1 : !1;
16 my $t_v = $force ? '0' : '1.67';
17 my $ts_v = $force ? '0' : '1.14';
18 skipall 'This perl wasn\'t built to support threads'
19 unless $Config{useithreads};
20 skipall 'perl 5.13.4 required to test thread safety'
21 unless $force or $] >= 5.013004;
22 skipall "threads $t_v required to test thread safety"
23 unless eval "use threads $t_v; 1";
24 skipall "threads::shared $ts_v required to test thread safety"
25 unless eval "use threads::shared $ts_v; 1";
28 use Test::More; # after threads
30 use Variable::Magic qw/wizard cast dispell getdata VMG_THREADSAFE VMG_OP_INFO_NAME VMG_OP_INFO_OBJECT/;
33 skipall 'This Variable::Magic isn\'t thread safe' unless VMG_THREADSAFE;
34 plan tests => (4 * 18 + 1) + (4 * 13 + 1);
35 defined and diag "Using threads $_" for $threads::VERSION;
36 defined and diag "Using threads::shared $_" for $threads::shared::VERSION;
39 my $destroyed : shared = 0;
42 my ($dispell, $op_info) = @_;
43 my $tid = threads->tid();
46 wizard data => sub { $_[1] + $tid },
47 get => sub { ++$c; 0 },
50 if ($op_info == VMG_OP_INFO_OBJECT) {
51 is_deeply { class => ref($op), name => $op->name },
52 { class => 'B::BINOP', name => 'sassign' },
53 "op object in thread $tid is correct";
55 is $op, 'sassign', "op name in thread $tid is correct";
59 free => sub { lock $destroyed; ++$destroyed; 0 },
62 is($@, '', "wizard in thread $tid doesn't croak");
63 isnt($wiz, undef, "wizard in thread $tid is defined");
64 is($c, 0, "wizard in thread $tid doesn't trigger magic");
66 my $res = eval { cast $a, $wiz, sub { 5 }->() };
67 is($@, '', "cast in thread $tid doesn't croak");
68 is($c, 0, "cast in thread $tid doesn't trigger magic");
71 is($@, '', "get in thread $tid doesn't croak");
72 is($b, 3, "get in thread $tid returns the right thing");
73 is($c, 1, "get in thread $tid triggers magic");
74 my $d = eval { getdata $a, $wiz };
75 is($@, '', "getdata in thread $tid doesn't croak");
76 is($d, 5 + $tid, "getdata in thread $tid returns the right thing");
77 is($c, 1, "getdata in thread $tid doesn't trigger magic");
79 is($@, '', "set in thread $tid (check opname) doesn't croak");
81 $res = eval { dispell $a, $wiz };
82 is($@, '', "dispell in thread $tid doesn't croak");
83 is($c, 1, "dispell in thread $tid doesn't trigger magic");
86 is($@, '', "get in thread $tid after dispell doesn't croak");
87 is($b, 9, "get in thread $tid after dispell returns the right thing");
88 is($c, 1, "get in thread $tid after dispell doesn't trigger magic");
90 return; # Ugly if not here
93 for my $dispell (1, 0) {
99 my @t = map { threads->create(\&try, $dispell, $_) }
100 (VMG_OP_INFO_NAME) x 2, (VMG_OP_INFO_OBJECT) x 2;
105 is $destroyed, (1 - $dispell) * 4, 'destructors';