File: 38loop-thread.t

package info (click to toggle)
libio-async-perl 0.64-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,068 kB
  • ctags: 491
  • sloc: perl: 12,530; makefile: 8
file content (59 lines) | stat: -rw-r--r-- 1,229 bytes parent folder | download | duplicates (5)
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
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/perl

use strict;
use warnings;

use IO::Async::Test;

use Test::More;

use IO::Async::Loop;
use IO::Async::OS;

plan skip_all => "Threads are not available" unless IO::Async::OS->HAVE_THREADS;

my $loop = IO::Async::Loop->new_builtin;

testing_loop( $loop );

# thread in scalar context
{
   my @result;
   $loop->create_thread(
      code      => sub { return "A result" },
      on_joined => sub { @result = @_ },
   );

   wait_for { @result };

   is_deeply( \@result, [ return => "A result" ], 'result to on_joined for returning thread' );
}

# thread in list context
{
   my @result;
   $loop->create_thread(
      code      => sub { return "A result", "of many", "values" },
      context   => "list",
      on_joined => sub { @result = @_ },
   );

   wait_for { @result };

   is_deeply( \@result, [ return => "A result", "of many", "values" ], 'result to on_joined for returning thread in list context' );
}

# thread that dies
{
   my @result;
   $loop->create_thread(
      code      => sub { die "Ooops I fail\n" },
      on_joined => sub { @result = @_ },
   );

   wait_for { @result };

   is_deeply( \@result, [ died => "Ooops I fail\n" ], 'result to on_joined for a died thread' );
}

done_testing;