xref: /openbsd-src/gnu/usr.bin/perl/dist/threads/t/err.t (revision b8851fcc53cbe24fd20b090f26dd149e353f6174)
1b39c5158Smillertuse strict;
2b39c5158Smillertuse warnings;
3b39c5158Smillert
4b39c5158SmillertBEGIN {
5b39c5158Smillert    require($ENV{PERL_CORE} ? '../../t/test.pl' : './t/test.pl');
6b39c5158Smillert
7b39c5158Smillert    use Config;
8b39c5158Smillert    if (! $Config{'useithreads'}) {
9b39c5158Smillert        skip_all(q/Perl not compiled with 'useithreads'/);
10b39c5158Smillert    }
11b39c5158Smillert
12b39c5158Smillert    plan(10);
13b39c5158Smillert}
14b39c5158Smillert
15b39c5158Smillertuse ExtUtils::testlib;
16b39c5158Smillert
17b39c5158Smillertuse_ok('threads');
18b39c5158Smillert
19b39c5158Smillert### Start of Testing ###
20b39c5158Smillert
21b39c5158Smillertno warnings 'threads';
22b39c5158Smillert
23b39c5158Smillert# Create a thread that generates an error
24b39c5158Smillertmy $thr = threads->create(sub { my $x = Foo->new(); });
25b39c5158Smillert
26b39c5158Smillert# Check that thread returns 'undef'
27b39c5158Smillertmy $result = $thr->join();
28b39c5158Smillertok(! defined($result), 'thread died');
29b39c5158Smillert
30b39c5158Smillert# Check error
31*b8851fccSafresh1like($thr->error(), qr/^Can't locate object method/s, 'thread error');
32b39c5158Smillert
33b39c5158Smillert
34b39c5158Smillert# Create a thread that 'die's with an object
35b39c5158Smillert$thr = threads->create(sub {
36b39c5158Smillert                    threads->yield();
37b39c5158Smillert                    sleep(1);
38b39c5158Smillert                    die(bless({ error => 'bogus' }, 'Err::Class'));
39b39c5158Smillert                });
40b39c5158Smillert
41b39c5158Smillertmy $err = $thr->error();
42b39c5158Smillertok(! defined($err), 'no error yet');
43b39c5158Smillert
44b39c5158Smillert# Check that thread returns 'undef'
45b39c5158Smillert$result = $thr->join();
46b39c5158Smillertok(! defined($result), 'thread died');
47b39c5158Smillert
48b39c5158Smillert# Check that error object is retrieved
49b39c5158Smillert$err = $thr->error();
50b39c5158Smillertisa_ok($err, 'Err::Class', 'error object');
51b39c5158Smillertis($err->{error}, 'bogus', 'error field');
52b39c5158Smillert
53b39c5158Smillert# Check that another thread can reference the error object
54b39c5158Smillertmy $thrx = threads->create(sub { die(bless($thr->error(), 'Foo')); });
55b39c5158Smillert
56b39c5158Smillert# Check that thread returns 'undef'
57b39c5158Smillert$result = $thrx->join();
58b39c5158Smillertok(! defined($result), 'thread died');
59b39c5158Smillert
60b39c5158Smillert# Check that the rethrown error object is retrieved
61b39c5158Smillert$err = $thrx->error();
62b39c5158Smillertisa_ok($err, 'Foo', 'error object');
63b39c5158Smillertis($err->{error}, 'bogus', 'error field');
64b39c5158Smillert
65b39c5158Smillertexit(0);
66b39c5158Smillert
67b39c5158Smillert# EOF
68