File: 79-uid-threads.t

package info (click to toggle)
libscope-upper-perl 0.25-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 536 kB
  • ctags: 78
  • sloc: perl: 6,745; makefile: 7
file content (47 lines) | stat: -rw-r--r-- 889 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!perl -T

use strict;
use warnings;

use lib 't/lib';
use Scope::Upper::TestThreads;

use Test::Leaner;

use Scope::Upper qw<uid validate_uid UP HERE>;

my $top = uid;

sub cb {
 my $tid  = threads->tid();

 my $here = uid;
 my $up;
 {
  $up = uid HERE;
  is uid(UP), $here, "uid(UP) == \$here in block (in thread $tid)";
 }

 is uid(UP), $top, "uid(UP) == \$top (in thread $tid)";

 usleep rand(1e6);

 ok validate_uid($here), "\$here is valid (in thread $tid)";
 ok !validate_uid($up),  "\$up is no longer valid (in thread $tid)";

 return $here;
}

my %uids;
my $threads = 0;
for my $thread (map threads->create(\&cb), 1 .. 30) {
 ++$threads;
 my $tid = $thread->tid;
 my $uid = $thread->join;
 ++$uids{$uid};
 ok !validate_uid($uid), "\$here is no longer valid (out of thread $tid)";
}

is scalar(keys %uids), $threads, 'all the UIDs were different';

done_testing($threads * 5 + 1);