Skip to content

Commit 7eaca34

Browse files
author
Jan Henning Thorsen
committed
Released version 0.04
* Add support for callbacks * Fix failing tests
1 parent 91a8617 commit 7eaca34

File tree

5 files changed

+36
-21
lines changed

5 files changed

+36
-21
lines changed

Changes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
Revision history for Mojo-IOLoop-ReadWriteFork
22

3+
0.04 Fri Nov 22 19:52:52 2013
4+
* Add support for callbacks
5+
* Fix failing tests
6+
37
0.03 Fri Nov 22 08:35:43 2013
48
* A bit more relaxed test for cat.t: Running it on BSD results in
59
"^D" at the end.

README

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ NAME
22
Mojo::IOLoop::ReadWriteFork - Fork a process and read/write from it
33

44
VERSION
5-
0.03
5+
0.04
66

77
DESCRIPTION
88
This class enable you to fork children which you can write data to and
@@ -31,8 +31,8 @@ SYNOPSIS
3131
});
3232

3333
$fork->start(
34-
program => 'cat',
35-
program_args => [ '-' ],
34+
program => 'bash',
35+
program_args => [ -c => 'echo $YIKES foo bar baz' ],
3636
conduit => 'pty',
3737
);
3838

@@ -63,6 +63,11 @@ ATTRIBUTES
6363

6464
METHODS
6565
start
66+
$self->start(
67+
program => sub { my @program_args = @_; ... },
68+
program_args => [ @data ],
69+
);
70+
6671
$self->start(
6772
program => $str,
6873
program_args => [@str],

lib/Mojo/IOLoop/ReadWriteFork.pm

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Mojo::IOLoop::ReadWriteFork - Fork a process and read/write from it
66
77
=head1 VERSION
88
9-
0.03
9+
0.04
1010
1111
=head1 DESCRIPTION
1212
@@ -38,8 +38,8 @@ and STDOUT are more than welcome.
3838
});
3939
4040
$fork->start(
41-
program => 'cat',
42-
program_args => [ '-' ],
41+
program => 'bash',
42+
program_args => [ -c => 'echo $YIKES foo bar baz' ],
4343
conduit => 'pty',
4444
);
4545
@@ -58,7 +58,7 @@ use constant CHUNK_SIZE => $ENV{MOJO_CHUNK_SIZE} || 131072;
5858
use constant DEBUG => $ENV{MOJO_READWRITE_FORK_DEBUG} || 0;
5959
use constant WAIT_PID_INTERVAL => $ENV{WAIT_PID_INTERVAL} || 0.01;
6060

61-
our $VERSION = '0.03';
61+
our $VERSION = '0.04';
6262

6363
=head1 EVENTS
6464
@@ -102,6 +102,11 @@ has reactor => sub {
102102
103103
=head2 start
104104
105+
$self->start(
106+
program => sub { my @program_args = @_; ... },
107+
program_args => [ @data ],
108+
);
109+
105110
$self->start(
106111
program => $str,
107112
program_args => [@str],
@@ -198,9 +203,6 @@ sub _start {
198203
warn "[$$] Starting $args->{program} @{ $args->{program_args} }\n" if DEBUG;
199204
close $stdin_write;
200205
close $stdout_read;
201-
close STDIN;
202-
close STDOUT;
203-
close STDERR;
204206
open STDIN, '<&' . fileno $stdin_read or die $!;
205207
open STDOUT, '>&' . fileno $stdout_write or die $!;
206208
open STDERR, '>&' . fileno $stdout_write or die $!;
@@ -211,6 +213,7 @@ sub _start {
211213

212214
if(ref $args->{program} eq 'CODE') {
213215
$args->{program}->(@{ $args->{program_args} });
216+
exit 0;
214217
}
215218
else {
216219
exec $args->{program}, @{ $args->{program_args} };
@@ -260,8 +263,9 @@ sub write {
260263
my($self, $buffer) = @_;
261264

262265
$self->{stdin_write} or return;
263-
warn "[${ \$self->pid }] Write buffer (" .url_escape($buffer) .")\n" if DEBUG;
264266
print { $self->{stdin_write} } $buffer;
267+
$self->{stdin_write}->flush or die "Write buffer (" .url_escape($buffer) .") failed: $!";
268+
warn "[${ \$self->pid }] Wrote buffer (" .url_escape($buffer) .")\n" if DEBUG;
265269
}
266270

267271
=head2 kill

t/bash.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ plan tests => 4;
3333
$run->start(
3434
program => 'bash',
3535
program_args => [ -c => 'echo $YIKES foo bar baz' ],
36-
conduit => 'pipe',
36+
conduit => 'pty',
3737
);
3838
}
3939

t/cat.t renamed to t/callback.t

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ use Mojo::IOLoop::ReadWriteFork;
33
use Test::More;
44
use Test::Memory::Cycle;
55

6-
$ENV{PATH} ||= '';
7-
plan skip_all => 'cat is missing' unless grep { -x "$_/cat" } split /:/, $ENV{PATH};
86
plan tests => 10;
97

108
{
119
my $run = Mojo::IOLoop::ReadWriteFork->new;
1210
my $output = '';
1311
my $closed = 0;
14-
my $cycled;
12+
my $read;
1513

1614
memory_cycle_ok $run, 'no cycle after new()';
1715
$run->on(error => sub {
@@ -25,26 +23,30 @@ plan tests => 10;
2523
$run->on(read => sub {
2624
my($run, $buffer, $writer) = @_;
2725
$output .= $buffer;
28-
memory_cycle_ok $run, 'no cycle inside read' unless $cycled++;
26+
$run->write("line one\n") unless $read;
27+
memory_cycle_ok $run, 'no cycle inside read' unless $read++;
2928
});
3029
memory_cycle_ok $run, 'no cycle after on()';
3130

3231
eval { $run->start({ program_args => [] }) };
3332
like $@, qr{program is required input}, 'program is required';
3433
$run->start(
35-
program => 'cat',
36-
program_args => [ '-' ],
37-
conduit => 'pty',
34+
program => sub {
35+
print join(" ", @_), "\n";
36+
my $input = <STDIN>;
37+
print $input;
38+
print "line two\n";
39+
},
40+
program_args => [qw( some args )],
3841
);
3942

4043
memory_cycle_ok $run, 'no cycle after start()';
4144
is $run->pid, 0, 'no pid' or diag $run->pid;
42-
Mojo::IOLoop->timer(0.1, sub { $run->write("hello world\n\x04") });
4345
Mojo::IOLoop->timer(3 => sub { Mojo::IOLoop->stop }); # guard
4446
Mojo::IOLoop->start;
4547
memory_cycle_ok $run, 'no cycle after Mojo::IOLoop->start';
4648

4749
like $run->pid, qr{^[1-9]\d+$}, 'got pid' or diag $run->pid;
48-
like $output, qr/^hello world\W{1,2}hello world\W{1,2}/, 'got stdout from "cat -"' or diag $output;
50+
like $output, qr/^some args\nline one\nline two\n/, 'got stdout from callback' or diag $output;
4951
is $closed, 1, "got close event";
5052
}

0 commit comments

Comments
 (0)