]> git.vpit.fr Git - perl/modules/Lexical-Types.git/blob - t/30-threads.t
Get rid of ENTERn/LEAVEn
[perl/modules/Lexical-Types.git] / t / 30-threads.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Config qw/%Config/;
7
8 BEGIN {
9  if (!$Config{useithreads}) {
10   require Test::More;
11   Test::More->import;
12   plan(skip_all => 'This perl wasn\'t built to support threads');
13  }
14 }
15
16 use threads;
17
18 use Test::More;
19
20 BEGIN {
21  require Lexical::Types;
22  if (Lexical::Types::LT_THREADSAFE()) {
23   plan tests => 10 * 2 * 3 * (1 + 2);
24   defined and diag "Using threads $_" for $threads::VERSION;
25  } else {
26   plan skip_all => 'This Lexical::Types isn\'t thread safe';
27  }
28 }
29
30 {
31  package Lexical::Types::Test::Tag;
32
33  sub TYPEDSCALAR {
34   my $tid = threads->tid();
35   my ($file, $line) = (caller(0))[1, 2];
36   my $where = "at $file line $line in thread $tid";
37   local $Test::Builder::Level = $Test::Builder::Level + 1;
38   Test::More::is($_[0], __PACKAGE__, "base type is correct $where");
39   Test::More::is($_[2], 'Tag', "original type is correct $where");
40   $_[1] = $tid;
41   ();
42  }
43 }
44
45 { package Tag; }
46
47 use Lexical::Types as => 'Lexical::Types::Test::';
48
49 sub try {
50  my $tid = threads->tid();
51
52  for (1 .. 2) {
53   my Tag $t;
54   is $t, $tid, "typed lexical correctly initialized at run $_ in thread $tid";
55
56   eval <<'EVALD';
57    use Lexical::Types as => "Lexical::Types::Test::";
58    my Tag $t2;
59    is $t2, $tid, "typed lexical correctly initialized in eval at run $_ in thread $tid";
60 EVALD
61   diag $@ if $@;
62
63 SKIP:
64   {
65    skip 'Hints aren\'t propagated into eval STRING below perl 5.10' => 3
66                                                              unless $] >= 5.010;
67    eval <<'EVALD';
68     my Tag $t3;
69     is $t3, $tid, "typed lexical correctly initialized in eval (propagated) at run $_ in thread $tid"
70 EVALD
71   }
72  }
73 }
74
75 my @t = map threads->create(\&try), 1 .. 10;
76 $_->join for @t;