]> git.vpit.fr Git - perl/modules/indirect.git/blob - t/40-threads.t
Port threads tests to the new VPIT::TestHelpers interface
[perl/modules/indirect.git] / t / 40-threads.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 BEGIN { require indirect; }
7
8 use lib 't/lib';
9 use VPIT::TestHelpers (
10  threads => [ 'indirect' => indirect::I_THREADSAFE ],
11 );
12
13 use Test::Leaner;
14
15 sub expect {
16  my ($pkg) = @_;
17  qr/^Indirect call of method "new" on object "$pkg" at \(eval \d+\) line \d+/;
18 }
19
20 {
21  no indirect;
22
23  sub try {
24   my $tid = threads->tid();
25
26   for (1 .. 2) {
27    {
28     my $class = "Coconut$tid";
29     my @warns;
30     {
31      local $SIG{__WARN__} = sub { push @warns, @_ };
32      eval 'die "the code compiled but it shouldn\'t have\n";
33            no indirect ":fatal"; my $x = new ' . $class . ' 1, 2;';
34     }
35     like         $@ || '', expect($class),
36                       "\"no indirect\" in eval in thread $tid died as expected";
37     is_deeply \@warns, [ ],
38                       "\"no indirect\" in eval in thread $tid didn't warn";
39    }
40
41 SKIP:
42    {
43     skip 'Hints aren\'t propagated into eval STRING below perl 5.10' => 3
44                                                            unless "$]" >= 5.010;
45     my $class = "Pineapple$tid";
46     my @warns;
47     {
48      local $SIG{__WARN__} = sub { push @warns, @_ };
49      eval 'return; my $y = new ' . $class . ' 1, 2;';
50     }
51     is $@, '',
52              "\"no indirect\" propagated into eval in thread $tid didn't croak";
53     my $first = shift @warns;
54     like $first || '', expect($class),
55               "\"no indirect\" propagated into eval in thread $tid warned once";
56     is_deeply \@warns, [ ],
57          "\"no indirect\" propagated into eval in thread $tid warned just once";
58    }
59   }
60  }
61 }
62
63 my @threads = map spawn(\&try), 1 .. 10;
64
65 $_->join for @threads;
66
67 pass 'done';
68
69 done_testing(scalar(@threads) * 2 * (2 + 3) + 1);