]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/41-clone.t
Skip threads tests unless perl version is 5.13.4 or greater
[perl/modules/Variable-Magic.git] / t / 41-clone.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 'perl 5.13.4 required to test thread safety' unless $] >= 5.013004;
20  skipall "threads $t_v required to test thread safety"
21                                               unless eval "use threads $t_v; 1";
22  skipall "threads::shared $ts_v required to test thread safety"
23                                      unless eval "use threads::shared $ts_v; 1";
24 }
25
26 use Test::More; # after threads
27
28 use Variable::Magic qw/wizard cast dispell getdata VMG_THREADSAFE VMG_OP_INFO_NAME VMG_OP_INFO_OBJECT/;
29
30 BEGIN {
31  skipall 'This Variable::Magic isn\'t thread safe' unless VMG_THREADSAFE;
32  plan tests => 2 * 3 + 2 * (2 * 10 + 2) + 2 * (2 * 7 + 2);
33  my $v = $threads::VERSION;
34  diag "Using threads $v" if defined $v;
35  $v = $threads::shared::VERSION;
36  diag "Using threads::shared $v" if defined $v;
37 }
38
39 my $destroyed : shared = 0;
40 my $c         : shared = 0;
41
42 sub spawn_wiz {
43  my ($op_info) = @_;
44
45  my $wiz = eval {
46   wizard data    => sub { $_[1] + threads->tid() },
47          get     => sub { lock $c; ++$c; 0 },
48          set     => sub {
49                      my $op = $_[-1];
50                      my $tid = threads->tid();
51                      if ($op_info == VMG_OP_INFO_OBJECT) {
52                       is_deeply { class => ref($op),   name => $op->name },
53                                 { class => 'B::BINOP', name => 'sassign' },
54                                 "op object in thread $tid is correct";
55                      } else {
56                       is $op, 'sassign', "op name in thread $tid is correct";
57                      }
58                      0
59                     },
60          free    => sub { lock $destroyed; ++$destroyed; 0 },
61          op_info => $op_info
62  };
63  is($@,     '',    "wizard with op_info $op_info in main thread doesn't croak");
64  isnt($wiz, undef, "wizard with op_info $op_info in main thread is defined");
65  is($c,     0,     "wizard with op_info $op_info in main thread doesn't trigger magic");
66
67  return $wiz;
68 }
69
70 sub try {
71  my ($dispell, $wiz) = @_;
72  my $tid = threads->tid();
73  my $a   = 3;
74  my $res = eval { cast $a, $wiz, sub { 5 }->() };
75  is($@, '', "cast in thread $tid doesn't croak");
76  my $b;
77  eval { $b = $a };
78  is($@, '', "get in thread $tid doesn't croak");
79  is($b, 3,  "get in thread $tid returns the right thing");
80  my $d = eval { getdata $a, $wiz };
81  is($@, '',       "getdata in thread $tid doesn't croak");
82  is($d, 5 + $tid, "getdata in thread $tid returns the right thing");
83  eval { $a = 9 };
84  is($@, '', "set in thread $tid (check opname) doesn't croak");
85  if ($dispell) {
86   $res = eval { dispell $a, $wiz };
87   is($@, '', "dispell in thread $tid doesn't croak");
88   undef $b;
89   eval { $b = $a };
90   is($@, '', "get in thread $tid after dispell doesn't croak");
91   is($b, 9,  "get in thread $tid after dispell returns the right thing");
92  }
93  return; # Ugly if not here
94 }
95
96 my $wiz_name = spawn_wiz VMG_OP_INFO_NAME;
97 my $wiz_obj  = spawn_wiz VMG_OP_INFO_OBJECT;
98
99 for my $dispell (1, 0) {
100  for my $wiz ($wiz_name, $wiz_obj) {
101   {
102    lock $c;
103    $c = 0;
104   }
105   {
106    lock $destroyed;
107    $destroyed = 0;
108   }
109
110   my @t = map { threads->create(\&try, $dispell, $wiz) } 1 .. 2;
111   $_->join for @t;
112
113   {
114    lock $c;
115    is $c, 2, "get triggered twice";
116   }
117   {
118    lock $destroyed;
119    is $destroyed, (1 - $dispell) * 2, 'destructors';
120   }
121  }
122 }