File: threads.t

package info (click to toggle)
libdevel-confess-perl 0.009003-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 212 kB
  • ctags: 57
  • sloc: perl: 985; makefile: 2
file content (70 lines) | stat: -rw-r--r-- 1,095 bytes parent folder | download | duplicates (4)
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
69
70
use lib 't/lib';
use ThreadsCheck;
use threads;
use strict;
use warnings;
BEGIN {
  $ENV{DEVEL_CONFESS_OPTIONS} = '';
}

use MiniTest;

use Devel::Confess qw(nowarnings);

my $gone = 0;
{
  package MyException;
  use overload
    fallback => 1,
    '""' => sub {
      $_[0]->{message};
    },
  ;
  sub new {
    my ($class, $message) = @_;
    my $self = bless { message => $message }, $class;
    return $self;
  }
  sub DESTROY {
    $gone++;
  }
}

sub foo {
  eval { die MyException->new("yarp") };
  $@;
}

sub bar {
  foo();
}

my $ex = bar();

my $stringy_ex = "$ex";

my $stringy_from_thread = threads->create(sub {
  "$ex";
})->join;

is $stringy_from_thread, $stringy_ex,
  'stack trace maintained across threads';

my $thread_gone = threads->create(sub {
  undef $ex;
  $gone;
})->join;

is $thread_gone, $gone + 1,
  'DESTROY called in threads for cloned exception';

my $cleared = threads->create(sub {
  my $class = ref $ex;
  undef $ex;
  UNIVERSAL::can($class, 'DESTROY') ? 0 : 1;
})->join;

ok $cleared,
  'cloned exception cleans up namespace when destroyed';

done_testing;