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 => 2 * 3 + 2 * (2 * 10 + 2) + 2 * (2 * 7 + 2);
35 my $v = $threads::VERSION;
36 diag "Using threads $v" if defined $v;
37 $v = $threads::shared::VERSION;
38 diag "Using threads::shared $v" if defined $v;
41 my $destroyed : shared = 0;
48 wizard data => sub { $_[1] + threads->tid() },
49 get => sub { lock $c; ++$c; 0 },
52 my $tid = threads->tid();
53 if ($op_info == VMG_OP_INFO_OBJECT) {
54 is_deeply { class => ref($op), name => $op->name },
55 { class => 'B::BINOP', name => 'sassign' },
56 "op object in thread $tid is correct";
58 is $op, 'sassign', "op name in thread $tid is correct";
62 free => sub { lock $destroyed; ++$destroyed; 0 },
65 is($@, '', "wizard with op_info $op_info in main thread doesn't croak");
66 isnt($wiz, undef, "wizard with op_info $op_info in main thread is defined");
67 is($c, 0, "wizard with op_info $op_info in main thread doesn't trigger magic");
73 my ($dispell, $wiz) = @_;
74 my $tid = threads->tid();
76 my $res = eval { cast $a, $wiz, sub { 5 }->() };
77 is($@, '', "cast in thread $tid doesn't croak");
80 is($@, '', "get in thread $tid doesn't croak");
81 is($b, 3, "get in thread $tid returns the right thing");
82 my $d = eval { getdata $a, $wiz };
83 is($@, '', "getdata in thread $tid doesn't croak");
84 is($d, 5 + $tid, "getdata in thread $tid returns the right thing");
86 is($@, '', "set in thread $tid (check opname) doesn't croak");
88 $res = eval { dispell $a, $wiz };
89 is($@, '', "dispell in thread $tid doesn't croak");
92 is($@, '', "get in thread $tid after dispell doesn't croak");
93 is($b, 9, "get in thread $tid after dispell returns the right thing");
95 return; # Ugly if not here
98 my $wiz_name = spawn_wiz VMG_OP_INFO_NAME;
99 my $wiz_obj = spawn_wiz VMG_OP_INFO_OBJECT;
101 for my $dispell (1, 0) {
102 for my $wiz ($wiz_name, $wiz_obj) {
112 my @t = map { threads->create(\&try, $dispell, $wiz) } 1 .. 2;
117 is $c, 2, "get triggered twice";
121 is $destroyed, (1 - $dispell) * 2, 'destructors';