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