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