]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/41-clone.t
Output threads and threads::shared versions in threads tests
[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/;
22
23 if (VMG_THREADSAFE) {
24  plan tests => 3 + 2 * (2 * 8 + 2) + 2 * (2 * 5 + 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  diag 'This will leak a few scalars';
30 } else {
31  plan skip_all => 'This Variable::Magic isn\'t thread safe';
32 }
33
34 my $destroyed : shared = 0;
35 my $c         : shared = 0;
36 my $wiz = eval {
37  wizard get  => sub { ++$c },
38         data => sub { $_[1] + threads->tid() },
39         free => sub { ++$destroyed }
40 };
41 is($@,     '',    "wizard in main thread doesn't croak");
42 isnt($wiz, undef, "wizard in main thread is defined");
43 is($c,     0,     "wizard in main thread doesn't trigger magic");
44
45 my $sig;
46
47 sub try {
48  my ($dispell) = @_;
49  my $tid = threads->tid();
50  my $a   = 3;
51  my $res = eval { cast $a, $sig, sub { 5 }->() };
52  is($@, '', "cast in thread $tid doesn't croak");
53  my $b;
54  eval { $b = $a };
55  is($@, '', "get in thread $tid doesn't croak");
56  is($b, 3,  "get in thread $tid returns the right thing");
57  my $d = eval { getdata $a, $sig };
58  is($@, '',       "getdata in thread $tid doesn't croak");
59  is($d, 5 + $tid, "getdata in thread $tid returns the right thing");
60  if ($dispell) {
61   $res = eval { dispell $a, $sig };
62   is($@, '', "dispell in thread $tid doesn't croak");
63   undef $b;
64   eval { $b = $a };
65   is($@, '', "get in thread $tid after dispell doesn't croak");
66   is($b, 3,  "get in thread $tid after dispell returns the right thing");
67  }
68  return; # Ugly if not here
69 }
70
71 for my $dispell (1, 0) {
72  $c = 0;
73  $destroyed = 0;
74  $sig = $wiz;
75
76  my @t = map { threads->create(\&try, $dispell) } 1 .. 2;
77  $t[0]->join;
78  $t[1]->join;
79
80  is($c, 2, "get triggered twice");
81  is($destroyed, (1 - $dispell) * 2, 'destructors');
82
83  $c = 0;
84  $destroyed = 0;
85  $sig = getsig $wiz;
86
87  @t = map { threads->create(\&try, $dispell) } 1 .. 2;
88  $t[0]->join;
89  $t[1]->join;
90
91  is($c, 2, "get triggered twice");
92  is($destroyed, (1 - $dispell) * 2, 'destructors');
93 }