<div dir="ltr"><div dir="ltr"><div dir="ltr">Good idea, to check for expected warnings only.<div><br></div><div>This is reminding me of modules to help test warnings, looks like <a href="https://metacpan.org/pod/Test::Warnings">https://metacpan.org/pod/Test::Warnings</a> is the one to use these days.</div><div><br></div><div>Wonder what is out there to ease the eval/die testing, maybe <a href="https://metacpan.org/pod/Test::Exception">https://metacpan.org/pod/Test::Exception</a> ?</div><div><br clear="all"><div><div dir="ltr" class="gmail_signature">-y<br></div></div><br></div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Aug 1, 2020 at 10:12 PM David Christensen <<a href="mailto:dpchrist@holgerdanske.com">dpchrist@holgerdanske.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">On 2020-08-01 00:10, David Christensen wrote:<br>
> sanfrancisco-pm:<br>
<br>
> sub foo<br>
> {<br>
> if ($test_mode) {<br>
> no warnings 'threads';<br>
> <br>
> $thr = threads->create(<br>
> sub { die "hello, world!" }<br>
> );<br>
> } else {<br>
> $thr = threads->create(<br>
> sub { die "hello, world!" }<br>
> );<br>
> }<br>
> }<br>
<br>
"no warnings 'threads'" seemed overly broad, and was bugging me.<br>
<br>
<br>
So, I came up with another approach -- use an eval-apply design pattern <br>
inside the child thread. The inner code becomes an expression and fits <br>
into a ternary conditional. Child errors are saved into a shared lookup <br>
table. All exception messages should be caught (except those that crash <br>
Perl?). Expected exception messages will pass test cases and unexpected <br>
messages will fail test cases:<br>
<br>
<br>
2020-08-01 18:59:02 dpchrist@tinkywinky ~/sandbox/perl<br>
$ cat <a href="http://thread-exception-eval.pl" rel="noreferrer" target="_blank">thread-exception-eval.pl</a><br>
#!perl<br>
<br>
use strict;<br>
use warnings;<br>
use threads;<br>
<br>
use Data::Dumper;<br>
use Test::More;<br>
<br>
our $test_mode;<br>
our %tid_ee :shared;<br>
<br>
sub bar { die @_ };<br>
<br>
sub foo<br>
{<br>
threads->create(<br>
sub {<br>
my $f = shift;<br>
my $r = $test_mode<br>
? eval { $f->(@_) }<br>
: $f->(@_);<br>
$tid_ee{threads->tid} = $@;<br>
return $r;<br>
},<br>
@_<br>
);<br>
}<br>
<br>
my $thr;<br>
<br>
ok $thr = foo(\&bar, "hello, world!"), __FILE__ . __LINE__; # 1<br>
is $thr->join, undef, __FILE__ . __LINE__; # 2<br>
like $thr->error, qr/hello/, __FILE__ . __LINE__; # 3<br>
is $tid_ee{$thr}, undef, __FILE__ . __LINE__; # 4<br>
<br>
$test_mode = 1;<br>
<br>
ok $thr = foo(\&bar, "hello, world!"), __FILE__ . __LINE__; # 5<br>
is $thr->join, undef, __FILE__ . __LINE__; # 6<br>
is $thr->error, undef, __FILE__ . __LINE__; # 7<br>
like $tid_ee{$thr->tid}, qr/hello/, __FILE__ . __LINE__; # 8<br>
<br>
ok $thr = foo(\&bar,"goodbye, cruel world!"),__FILE__.__LINE__; # 9<br>
is $thr->join, undef, __FILE__ . __LINE__; # 10<br>
is $thr->error, undef, __FILE__ . __LINE__; # 11<br>
like $tid_ee{$thr->tid}, qr/hello/, __FILE__ . __LINE__; # 12<br>
<br>
done_testing;<br>
<br>
2020-08-01 18:59:50 dpchrist@tinkywinky ~/sandbox/perl<br>
$ perl <a href="http://thread-exception-eval.pl" rel="noreferrer" target="_blank">thread-exception-eval.pl</a><br>
Thread 1 terminated abnormally: hello, world! at <br>
<a href="http://thread-exception-eval.pl" rel="noreferrer" target="_blank">thread-exception-eval.pl</a> line 13.<br>
ok 1 - thread-exception-eval.pl32<br>
ok 2 - thread-exception-eval.pl33<br>
ok 3 - thread-exception-eval.pl34<br>
ok 4 - thread-exception-eval.pl35<br>
ok 5 - thread-exception-eval.pl39<br>
ok 6 - thread-exception-eval.pl40<br>
ok 7 - thread-exception-eval.pl41<br>
ok 8 - thread-exception-eval.pl42<br>
ok 9 - thread-exception-eval.pl44<br>
ok 10 - thread-exception-eval.pl45<br>
ok 11 - thread-exception-eval.pl46<br>
not ok 12 - thread-exception-eval.pl47<br>
# Failed test 'thread-exception-eval.pl47'<br>
# at <a href="http://thread-exception-eval.pl" rel="noreferrer" target="_blank">thread-exception-eval.pl</a> line 47.<br>
# 'goodbye, cruel world! at <a href="http://thread-exception-eval.pl" rel="noreferrer" target="_blank">thread-exception-eval.pl</a> <br>
line 13.<br>
# '<br>
# doesn't match '(?^:hello)'<br>
1..12<br>
# Looks like you failed 1 test of 12.<br>
<br>
<br>
David<br>
_______________________________________________<br>
SanFrancisco-pm mailing list<br>
<a href="mailto:SanFrancisco-pm@pm.org" target="_blank">SanFrancisco-pm@pm.org</a><br>
<a href="https://mail.pm.org/mailman/listinfo/sanfrancisco-pm" rel="noreferrer" target="_blank">https://mail.pm.org/mailman/listinfo/sanfrancisco-pm</a><br>
</blockquote></div>