tests for Sub::Quote/Sub::Defer in threads
[gitmo/Moo.git] / t / sub-quote-threads.t
CommitLineData
cc7c20a0 1use strictures 1;
2use Test::More;
3use Config;
4BEGIN {
5 unless ($Config{useithreads} && eval { require threads } ) {
6 plan skip_all => "your perl does not support ithreads";
7 }
8}
9
10use Sub::Quote;
11
12my $one = quote_sub my $one_code = q{
13 BEGIN { $::EVALED{'one'} = 1 }
14 42
15};
16
17my $two = quote_sub q{
18 BEGIN { $::EVALED{'two'} = 1 }
19 3 + $x++
20} => { '$x' => \do { my $x = 0 } };
21
22ok(threads->create(sub {
23 my $quoted = quoted_from_sub($one);
24 $quoted && $quoted->[1] eq $one_code;
25})->join, 'able to retrieve quoted sub in thread');
26
27my $u_one = unquote_sub $one;
28
29is(threads->create(sub { $one->() })->join, 42, 'One (quoted version)');
30
31is(threads->create(sub { $u_one->() })->join, 42, 'One (unquoted version)');
32
33my $r = threads->create(sub {
34 my @r;
35 push @r, $two->();
36 push @r, unquote_sub($two)->();
37 push @r, $two->();
38 \@r;
39})->join;
40
41is($r->[0], 3, 'Two in thread (quoted version)');
42is($r->[1], 4, 'Two in thread (unquoted version)');
43is($r->[2], 5, 'Two in thread (quoted version again)');
44
45done_testing;