File: 08_ioloop.t

package info (click to toggle)
libmojo-ioloop-readwriteprocess-perl 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 540 kB
  • sloc: perl: 4,655; sh: 101; makefile: 2
file content (41 lines) | stat: -rw-r--r-- 1,140 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/perl

use warnings;
use strict;
use Test::More;
use POSIX;
use FindBin;
use Mojo::File qw(tempfile path);
use lib ("$FindBin::Bin/lib", "../lib", "lib");

use Mojo::IOLoop::ReadWriteProcess              qw(process);
use Mojo::IOLoop::ReadWriteProcess::Test::Utils qw(attempt);
use Mojo::IOLoop;

subtest to_ioloop => sub {

  my $p = process(sub { print "Hello from first process\n"; sleep 1; exit 70 });

  $p->start();                   # Start and sets the handlers
  my $stream = $p->to_ioloop;    # Get the stream
  my $output;

  $stream->on(
    read => sub { $output .= pop; is $p->is_running, 1, 'Process is running!' }
  );    # Hook on Mojo::IOLoop::Stream events

  Mojo::IOLoop->singleton->start() unless Mojo::IOLoop->singleton->is_running;

  attempt {
    attempts  => 10,
    condition => sub { $p->is_running == 0 },
    cb        => sub { sleep 1 }
  };

  is $p->is_running,  0,  'Process is not running anymore';
  is $p->exit_status, 70, 'We got exit status';
  ok !$p->errored, 'No error from the process';
  is $output, "Hello from first process\n", 'Got correct output from process';
};

done_testing();