]> git.vpit.fr Git - perl/modules/Thread-Cleanup.git/blob - t/10-join.t
Test helper modules version output overhaul
[perl/modules/Thread-Cleanup.git] / t / 10-join.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;
17 use threads::shared;
18
19 use Test::More tests => 5 * (2 + 2) + 1;
20
21 BEGIN {
22  defined and diag "Using threads $_"         for $threads::VERSION;
23  defined and diag "Using threads::shared $_" for $threads::shared::VERSION;
24 }
25
26 use Thread::Cleanup;
27
28 my %called : shared;
29 my %nums   : shared;
30
31 our $x = -1;
32
33 Thread::Cleanup::register {
34  my $tid = threads->tid;
35
36  {
37   lock %called;
38   $called{$tid}++;
39  }
40
41  my $num = do {
42   lock %nums;
43   $nums{$tid};
44  };
45
46  is $x, $num, "\$x in destructor of thread $tid";
47  local $x = $tid;
48 };
49
50 my %ran : shared;
51
52 sub cb {
53  my ($y) = @_;
54
55  my $tid = threads->tid;
56  {
57   lock %ran;
58   $ran{$tid}++;
59  }
60
61  {
62   lock %nums;
63   $nums{$tid} = $y;
64  }
65  is $x, $y, "\$x in thread $tid";
66  local $x = -$tid;
67 }
68
69 my @tids;
70
71 my @t = map {
72  local $x = $_;
73  my $thr = threads->create(\&cb, $_);
74  push @tids, $thr->tid;
75  $thr;
76 } 0 .. 4;
77
78 $_->join for @t;
79
80 is $x, -1, '$x in the main thread';
81
82 for (@tids) {
83  is $ran{$_},    1, "thread $_ was run once";
84  is $called{$_}, 1, "thread $_ destructor was called once";
85 }