]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/40-threads.t
Require threads 1.67 and threads::shared 1.14 in t/40-threads.t
[perl/modules/Variable-Magic.git] / t / 40-threads.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 sub skipall {
7  my ($msg) = @_;
8  require Test::More;
9  Test::More::plan(skip_all => $msg);
10 }
11
12 use Config qw/%Config/;
13
14 BEGIN {
15  my $t_v  = '1.67';
16  my $ts_v = '1.14';
17  skipall 'This perl wasn\'t built to support threads'
18                                                     unless $Config{useithreads};
19  skipall "threads $t_v required to test thread safety"
20                                               unless eval "use threads $t_v; 1";
21  skipall "threads::shared $ts_v required to test thread safety"
22                                      unless eval "use threads::shared $ts_v; 1";
23 }
24
25 use Test::More; # after threads
26
27 use Variable::Magic qw/wizard cast dispell getdata VMG_THREADSAFE VMG_OP_INFO_NAME VMG_OP_INFO_OBJECT/;
28
29 BEGIN {
30  skipall 'This Variable::Magic isn\'t thread safe' unless VMG_THREADSAFE;
31  plan tests => 2 * (4 * 18 + 1) + 2 * (4 * 13 + 1);
32  my $v = $threads::VERSION;
33  diag "Using threads $v" if defined $v;
34  $v = $threads::shared::VERSION;
35  diag "Using threads::shared $v" if defined $v;
36 }
37
38 my $destroyed : shared = 0;
39
40 sub try {
41  my ($dispell, $sig, $op_info) = @_;
42  my $tid = threads->tid();
43  my $c   = 0;
44  my $wiz = eval {
45   wizard data    => sub { $_[1] + $tid },
46          sig     => $sig,
47          get     => sub { ++$c; 0 },
48          set     => sub {
49                      my $op = $_[-1];
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";
54                      } else {
55                       is $op, 'sassign', "op name in thread $tid is correct";
56                      }
57                      0
58                     },
59          free    => sub { lock $destroyed; ++$destroyed; 0 },
60          op_info => $op_info
61  };
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");
65  my $a = 3;
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");
69  my $b;
70  eval { $b = $a };
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");
78  eval { $a = 9 };
79  is($@, '', "set in thread $tid (check opname) doesn't croak");
80  if ($dispell) {
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");
84   undef $b;
85   eval { $b = $a };
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");
89  }
90  return; # Ugly if not here
91 }
92
93 for my $dispell (1, 0) {
94  for my $sig (undef, Variable::Magic::gensig()) {
95   {
96    lock $destroyed;
97    $destroyed = 0;
98   }
99
100   my @t = map { threads->create(\&try, $dispell, $sig, $_) }
101                                (VMG_OP_INFO_NAME) x 2, (VMG_OP_INFO_OBJECT) x 2;
102   $_->join for @t;
103
104   {
105    lock $destroyed;
106    is $destroyed, (1 - $dispell) * 4, 'destructors';
107   }
108  }
109 }