]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/41-clone.t
Remove all signature-related features
[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 "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 * 3 + 2 * (2 * 10 + 2) + 2 * (2 * 7 + 2);
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 my $c         : shared = 0;
40
41 sub spawn_wiz {
42  my ($op_info) = @_;
43
44  my $wiz = eval {
45   wizard data    => sub { $_[1] + threads->tid() },
46          get     => sub { lock $c; ++$c; 0 },
47          set     => sub {
48                      my $op = $_[-1];
49                      my $tid = threads->tid();
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 with op_info $op_info in main thread doesn't croak");
63  isnt($wiz, undef, "wizard with op_info $op_info in main thread is defined");
64  is($c,     0,     "wizard with op_info $op_info in main thread doesn't trigger magic");
65
66  return $wiz;
67 }
68
69 sub try {
70  my ($dispell, $wiz) = @_;
71  my $tid = threads->tid();
72  my $a   = 3;
73  my $res = eval { cast $a, $wiz, sub { 5 }->() };
74  is($@, '', "cast in thread $tid doesn't croak");
75  my $b;
76  eval { $b = $a };
77  is($@, '', "get in thread $tid doesn't croak");
78  is($b, 3,  "get in thread $tid returns the right thing");
79  my $d = eval { getdata $a, $wiz };
80  is($@, '',       "getdata in thread $tid doesn't croak");
81  is($d, 5 + $tid, "getdata in thread $tid returns the right thing");
82  eval { $a = 9 };
83  is($@, '', "set in thread $tid (check opname) doesn't croak");
84  if ($dispell) {
85   $res = eval { dispell $a, $wiz };
86   is($@, '', "dispell in thread $tid doesn't croak");
87   undef $b;
88   eval { $b = $a };
89   is($@, '', "get in thread $tid after dispell doesn't croak");
90   is($b, 9,  "get in thread $tid after dispell returns the right thing");
91  }
92  return; # Ugly if not here
93 }
94
95 my $wiz_name = spawn_wiz VMG_OP_INFO_NAME;
96 my $wiz_obj  = spawn_wiz VMG_OP_INFO_OBJECT;
97
98 for my $dispell (1, 0) {
99  for my $wiz ($wiz_name, $wiz_obj) {
100   {
101    lock $c;
102    $c = 0;
103   }
104   {
105    lock $destroyed;
106    $destroyed = 0;
107   }
108
109   my @t = map { threads->create(\&try, $dispell, $wiz) } 1 .. 2;
110   $_->join for @t;
111
112   {
113    lock $c;
114    is $c, 2, "get triggered twice";
115   }
116   {
117    lock $destroyed;
118    is $destroyed, (1 - $dispell) * 2, 'destructors';
119   }
120  }
121 }