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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#!perl -T
use strict;
use warnings;
use lib 't/lib';
use Scope::Upper::TestThreads;
use Test::Leaner;
use Scope::Upper qw<uplevel UP>;
sub depth {
my $depth = 0;
while (1) {
my @c = caller($depth);
last unless @c;
++$depth;
}
return $depth - 1;
}
is depth(), 0, 'check top depth';
is sub { depth() }->(), 1, 'check subroutine call depth';
is do { local $@; eval { depth() } }, 1, 'check eval block depth';
our $z;
sub cb {
my $d = splice @_, 1, 1;
my $p = shift;
my $tid = pop;
is depth(), $d - 1, "$p: correct depth inside";
$tid, @_, $tid + 2
}
sub up1 {
my $tid = threads->tid();
local $z = $tid;
my $p = "[$tid] up1";
usleep rand(1e6);
my @res = (
-2,
sub {
my @dummy = (
-1,
sub {
my $d = depth();
my @ret = &uplevel(\&cb => ($p, $d, $tid + 1, $tid) => UP);
is depth(), $d, "$p: correct depth after uplevel";
@ret;
}->(),
1
);
}->(),
2
);
is_deeply \@res, [ -2, -1, $tid .. $tid + 2, 1, 2 ], "$p: returns correctly";
}
my @threads = map spawn(\&up1), 1 .. 30;
$_->join for @threads;
done_testing(3 + scalar(@threads) * 3);
|