]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/40-threads.t
Fix the B stashes cache cloning, and really use it for blessing op objects
[perl/modules/Variable-Magic.git] / t / 40-threads.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Config qw/%Config/;
7
8 BEGIN {
9  if (!$Config{useithreads}) {
10   require Test::More;
11   Test::More->import;
12   plan(skip_all => 'This perl wasn\'t built to support threads');
13  }
14 }
15
16 use threads; # Before Test::More
17 use threads::shared;
18
19 use Test::More;
20
21 use Variable::Magic qw/wizard cast dispell getdata VMG_THREADSAFE VMG_OP_INFO_NAME VMG_OP_INFO_OBJECT/;
22
23 if (VMG_THREADSAFE) {
24  plan tests => 2 * (4 * 18 + 1) + 2 * (4 * 13 + 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;
29 } else {
30  plan skip_all => 'This Variable::Magic isn\'t thread safe';
31 }
32
33 my $destroyed : shared = 0;
34
35 sub try {
36  my ($dispell, $sig, $op_info) = @_;
37  my $tid = threads->tid();
38  my $c   = 0;
39  my $wiz = eval {
40   wizard data    => sub { $_[1] + $tid },
41          sig     => $sig,
42          get     => sub { ++$c; 0 },
43          set     => sub {
44                      my $op = $_[-1];
45                      if ($op_info == VMG_OP_INFO_OBJECT) {
46                       is_deeply { class => ref($op),   name => $op->name },
47                                 { class => 'B::BINOP', name => 'sassign' },
48                                 "op object in thread $tid is correct";
49                      } else {
50                       is $op, 'sassign', "op name in thread $tid is correct";
51                      }
52                      0
53                     },
54          free    => sub { ++$destroyed; 0 },
55          op_info => $op_info
56  };
57  is($@,     '',    "wizard in thread $tid doesn't croak");
58  isnt($wiz, undef, "wizard in thread $tid is defined");
59  is($c,     0,     "wizard in thread $tid doesn't trigger magic");
60  my $a = 3;
61  my $res = eval { cast $a, $wiz, sub { 5 }->() };
62  is($@, '', "cast in thread $tid doesn't croak");
63  is($c, 0,  "cast in thread $tid doesn't trigger magic");
64  my $b;
65  eval { $b = $a };
66  is($@, '', "get in thread $tid doesn't croak");
67  is($b, 3,  "get in thread $tid returns the right thing");
68  is($c, 1,  "get in thread $tid triggers magic");
69  my $d = eval { getdata $a, $wiz };
70  is($@, '',       "getdata in thread $tid doesn't croak");
71  is($d, 5 + $tid, "getdata in thread $tid returns the right thing");
72  is($c, 1,        "getdata in thread $tid doesn't trigger magic");
73  eval { $a = 9 };
74  is($@, '', "set in thread $tid (check opname) doesn't croak");
75  if ($dispell) {
76   $res = eval { dispell $a, $wiz };
77   is($@, '', "dispell in thread $tid doesn't croak");
78   is($c, 1,  "dispell in thread $tid doesn't trigger magic");
79   undef $b;
80   eval { $b = $a };
81   is($@, '', "get in thread $tid after dispell doesn't croak");
82   is($b, 9,  "get in thread $tid after dispell returns the right thing");
83   is($c, 1,  "get in thread $tid after dispell doesn't trigger magic");
84  }
85  return; # Ugly if not here
86 }
87
88 for my $dispell (1, 0) {
89  for my $sig (undef, Variable::Magic::gensig()) {
90   $destroyed = 0;
91   my @t = map { threads->create(\&try, $dispell, $sig, $_) }
92                                (VMG_OP_INFO_NAME) x 2, (VMG_OP_INFO_OBJECT) x 2;
93   $_->join for @t;
94   is($destroyed, (1 - $dispell) * 4, 'destructors');
95  }
96 }