]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/41-clone.t
Make op_info thread safe
[perl/modules/Variable-Magic.git] / t / 41-clone.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 getsig VMG_THREADSAFE VMG_OP_INFO_NAME VMG_OP_INFO_OBJECT/;
22
23 if (VMG_THREADSAFE) {
24  plan tests => 2 * 3 + 4 * (2 * 10 + 2) + 4 * (2 * 7 + 2);
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 my $c         : shared = 0;
35
36 sub spawn_wiz {
37  my ($op_info) = @_;
38
39  my $wiz = eval {
40   wizard data    => sub { $_[1] + threads->tid() },
41          get     => sub { ++$c; 0 },
42          set     => sub {
43                      my $name = $_[-1];
44                      $name = $name->name if $op_info == VMG_OP_INFO_OBJECT;
45                      my $tid = threads->tid();
46                      is $name, 'sassign', "opname for op_info $op_info in thread $tid is correct";
47                      0
48                     },
49          free    => sub { ++$destroyed; 0 },
50          op_info => $op_info
51  };
52  is($@,     '',    "wizard with op_info $op_info in main thread doesn't croak");
53  isnt($wiz, undef, "wizard with op_info $op_info in main thread is defined");
54  is($c,     0,     "wizard with op_info $op_info in main thread doesn't trigger magic");
55
56  return $wiz;
57 }
58
59 sub try {
60  my ($dispell, $sig) = @_;
61  my $tid = threads->tid();
62  my $a   = 3;
63  my $res = eval { cast $a, $sig, sub { 5 }->() };
64  is($@, '', "cast in thread $tid doesn't croak");
65  my $b;
66  eval { $b = $a };
67  is($@, '', "get in thread $tid doesn't croak");
68  is($b, 3,  "get in thread $tid returns the right thing");
69  my $d = eval { getdata $a, $sig };
70  is($@, '',       "getdata in thread $tid doesn't croak");
71  is($d, 5 + $tid, "getdata in thread $tid returns the right thing");
72  eval { $a = 9 };
73  is($@, '', "set in thread $tid (check opname) doesn't croak");
74  if ($dispell) {
75   $res = eval { dispell $a, $sig };
76   is($@, '', "dispell in thread $tid doesn't croak");
77   undef $b;
78   eval { $b = $a };
79   is($@, '', "get in thread $tid after dispell doesn't croak");
80   is($b, 9,  "get in thread $tid after dispell returns the right thing");
81  }
82  return; # Ugly if not here
83 }
84
85 my $wiz_name = spawn_wiz VMG_OP_INFO_NAME;
86 my $wiz_obj  = spawn_wiz VMG_OP_INFO_OBJECT;
87
88 for my $dispell (1, 0) {
89  for my $sig ($wiz_name, getsig($wiz_name), $wiz_obj, getsig($wiz_obj)) {
90   $c = 0;
91   $destroyed = 0;
92
93   my @t = map { threads->create(\&try, $dispell, $sig) } 1 .. 2;
94   $_->join for @t;
95
96   is($c, 2, "get triggered twice");
97   is($destroyed, (1 - $dispell) * 2, 'destructors');
98  }
99 }