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