File: 50thread.t

package info (click to toggle)
libfuture-xs-perl 0.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 212 kB
  • sloc: ansic: 1,471; perl: 181; makefile: 3
file content (49 lines) | stat: -rw-r--r-- 1,051 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/perl

use v5.14;
use warnings;

use Test2::V0;
use Test2::IPC; # to quiet the warning

use Future::XS;

eval { require Config && $Config::Config{useithreads} } or
   plan skip_all => "This perl does not support threads";

require threads;

# Just to keep the in-dist unit tests happy; when loaded via Future.pm the
# one provided there takes precedence
sub Future::XS::wrap_cb
{
   my $self = shift;
   my ( $name, $cb ) = @_;
   return $cb;
}

# future outside of thread
{
    my $f1 = Future::XS->new;
    my $f2 = $f1->then( sub { Future::XS->done( "result" ) } );

    threads->create(sub {
        return "dummy";
    })->join;

    $f1->done;
    is( $f2->get, "result", 'Result of Future::XS entirely ouside of sidecar thread' );
}

# future inside thread
{
   my $ret = threads->create(sub {
      my $f1 = Future::XS->new;
      my $f2 = $f1->then( sub { Future::XS->done( "result" ) } );
      $f1->done;
      return $f2->get;
   })->join;
   is( $ret, "result", 'Result of Future::XS entirely within thread' );
}

done_testing;