summaryrefslogtreecommitdiff
path: root/ext/threads/t/err.t
blob: 9911187e2a54556daf9d05e68698aa7708684192 (plain)
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 strict;
use warnings;

BEGIN {
    if ($ENV{'PERL_CORE'}){
        chdir 't';
        unshift @INC, '../lib';
    }

    require($ENV{PERL_CORE} ? "./test.pl" : "./t/test.pl");

    use Config;
    if (! $Config{'useithreads'}) {
        skip_all(q/Perl not compiled with 'useithreads'/);
    }

    plan(10);
}

use ExtUtils::testlib;

use_ok('threads');

### Start of Testing ###

no warnings 'threads';

# Create a thread that generates an error
my $thr = threads->create(sub { my $x = Foo->new(); });

# Check that thread returns 'undef'
my $result = $thr->join();
ok(! defined($result), 'thread died');

# Check error
like($thr->error(), q/Can't locate object method/, 'thread error');


# Create a thread that 'die's with an object
$thr = threads->create(sub {
                    threads->yield();
                    sleep(1);
                    die(bless({ error => 'bogus' }, 'Err::Class'));
                });

my $err = $thr->error();
ok(! defined($err), 'no error yet');

# Check that thread returns 'undef'
$result = $thr->join();
ok(! defined($result), 'thread died');

# Check that error object is retrieved
$err = $thr->error();
isa_ok($err, 'Err::Class', 'error object');
is($err->{error}, 'bogus', 'error field');

# Check that another thread can reference the error object
my $thrx = threads->create(sub { die(bless($thr->error(), 'Foo')); });

# Check that thread returns 'undef'
$result = $thrx->join();
ok(! defined($result), 'thread died');

# Check that the rethrown error object is retrieved
$err = $thrx->error();
isa_ok($err, 'Foo', 'error object');
is($err->{error}, 'bogus', 'error field');

# EOF