]> git.vpit.fr Git - perl/modules/Scope-Upper.git/blob - t/79-uid-threads.t
Port t/79-uid-threads.t to the new threads interface
[perl/modules/Scope-Upper.git] / t / 79-uid-threads.t
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use lib 't/lib';
7 use VPIT::TestHelpers (
8  threads => [ 'Scope::Upper' => 'Scope::Upper::SU_THREADSAFE()' ],
9  'usleep',
10 );
11
12 use Test::Leaner;
13
14 use Scope::Upper qw<uid validate_uid UP HERE>;
15
16 my $top = uid;
17
18 sub cb {
19  my $tid  = threads->tid();
20
21  my $here = uid;
22  my $up;
23  {
24   $up = uid HERE;
25   is uid(UP), $here, "uid(UP) == \$here in block (in thread $tid)";
26  }
27
28  is uid(UP), $top, "uid(UP) == \$top (in thread $tid)";
29
30  usleep rand(2.5e5);
31
32  ok validate_uid($here), "\$here is valid (in thread $tid)";
33  ok !validate_uid($up),  "\$up is no longer valid (in thread $tid)";
34
35  return $here;
36 }
37
38 my %uids;
39 my $threads = 0;
40 for my $thread (map spawn(\&cb), 1 .. 30) {
41  my $tid = $thread->tid;
42  my $uid = $thread->join;
43  if (defined $uid) {
44   ++$threads;
45   ++$uids{$uid};
46   ok !validate_uid($uid), "\$here is no longer valid (out of thread $tid)";
47  }
48 }
49
50 is scalar(keys %uids), $threads, 'all the UIDs were different';
51
52 done_testing;