]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/40-threads.t
Don't test threads if this perl wasn't built with ithreads enabled
[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/;
22
23 if (VMG_THREADSAFE) {
24  plan tests => 2 * 16 + 1;
25 } else {
26  plan skip_all => 'This Variable::Magic isn\'t thread safe';
27 }
28
29 my $destroyed : shared = 0;
30
31 sub try {
32  my $tid = threads->tid();
33  my $c   = 0;
34  my $wiz = eval {
35   wizard get  => sub { ++$c },
36          data => sub { $_[1] + $tid },
37          free => sub { ++$destroyed };
38  };
39  is($@,     '',    "wizard in thread $tid doesn't croak");
40  isnt($wiz, undef, "wizard in thread $tid is defined");
41  is($c,     0,     "wizard in thread $tid doesn't trigger magic");
42  my $a = 3;
43  my $res = eval { cast $a, $wiz, sub { 5 }->() };
44  is($@, '', "cast in thread $tid doesn't croak");
45  is($c, 0,  "cast in thread $tid doesn't trigger magic");
46  my $b;
47  eval { $b = $a };
48  is($@, '', "get in thread $tid doesn't croak");
49  is($b, 3,  "get in thread $tid returns the right thing");
50  is($c, 1,  "get in thread $tid triggers magic");
51  my $d = eval { getdata $a, $wiz };
52  is($@, '',       "getdata in thread $tid doesn't croak");
53  is($d, 5 + $tid, "getdata in thread $tid returns the right thing");
54  is($c, 1,        "getdata in thread $tid doesn't trigger magic");
55  $res = eval { dispell $a, $wiz };
56  is($@, '', "dispell in thread $tid doesn't croak");
57  is($c, 1,  "dispell in thread $tid doesn't trigger magic");
58  undef $b;
59  eval { $b = $a };
60  is($@, '', "get in thread $tid after dispell doesn't croak");
61  is($b, 3,  "get in thread $tid after dispell returns the right thing");
62  is($c, 1,  "get in thread $tid after dispell doesn't trigger magic");
63  return;
64 }
65
66 my @t = map { threads->create(\&try) } 1 .. 2;
67 $t[0]->join;
68 $t[1]->join;
69
70 is($destroyed, 0, 'destructors didn\'t fired');