/
wheel_run.pm
404 lines (325 loc) · 11.6 KB
/
wheel_run.pm
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/perl -w
# $Id$
# Test the portable pipe classes and Wheel::Run, which uses them.
use strict;
use lib qw(./mylib ../mylib ../lib ./lib);
use Socket;
use TestSetup;
# Skip these tests if fork() is unavailable.
# We can't test_setup(0, "reason") because that calls exit(). And Tk
# will croak if you call BEGIN { exit() }. And that croak will cause
# this test to FAIL instead of skip.
BEGIN {
my $error;
if ($^O eq "MacOS") {
$error = "$^O does not support fork";
}
if ($error) {
print "1..0 # Skip $error\n";
CORE::exit();
}
}
test_setup(24);
# Turn on extra debugging output within this test program.
sub DEBUG () { 0 }
sub POE::Kernel::ASSERT_DEFAULT () { 1 }
sub POE::Kernel::TRACE_DEFAULT () { 1 }
sub POE::Kernel::TRACE_FILENAME () { "./test-output.err" }
use POE qw(Wheel::Run Filter::Line Pipe::TwoWay Pipe::OneWay);
### Test one-way pipe() pipe.
{ my ($uni_read, $uni_write) = POE::Pipe::OneWay->new('pipe');
if (defined $uni_read and defined $uni_write) {
&ok(1);
print $uni_write "whee pipe\n";
my $uni_input = <$uni_read>; chomp $uni_input;
&ok_if( 2, $uni_input eq 'whee pipe' );
}
else {
&many_ok(1, 2, "skipped: $^O does not support pipe().");
}
}
### Test one-way socketpair() pipe.
{ my ($uni_read, $uni_write) = POE::Pipe::OneWay->new('socketpair');
if (defined $uni_read and defined $uni_write) {
&ok(3);
print $uni_write "whee socketpair\n";
my $uni_input = <$uni_read>; chomp $uni_input;
&ok_if( 4, $uni_input eq 'whee socketpair' );
}
else {
&many_ok(3, 4, "skipped: $^O does not support socketpair().");
}
}
### Test one-way pair of inet sockets.
{ my ($uni_read, $uni_write) = POE::Pipe::OneWay->new('inet');
if (defined $uni_read and defined $uni_write) {
&ok(5);
print $uni_write "whee inet\n";
my $uni_input = <$uni_read>; chomp $uni_input;
&ok_if( 6, $uni_input eq 'whee inet' );
}
else {
&many_ok(5, 6, "skipped: $^O does not support inet sockets.");
}
}
### Test two-way pipe.
{ my ($a_rd, $a_wr, $b_rd, $b_wr) =
POE::Pipe::TwoWay->new('pipe');
if (defined $a_rd and defined $a_wr and defined $b_rd and defined $b_wr) {
&ok(7);
print $a_wr "a wr inet\n";
my $b_input = <$b_rd>; chomp $b_input;
&ok_if(8, $b_input eq 'a wr inet');
print $b_wr "b wr inet\n";
my $a_input = <$a_rd>; chomp $a_input;
&ok_if(9, $a_input eq 'b wr inet');
}
else {
&many_ok(7, 9, "skipped: $^O does not support pipe().");
}
}
### Test two-way socketpair.
{ my ($a_rd, $a_wr, $b_rd, $b_wr) =
POE::Pipe::TwoWay->new('socketpair');
if (defined $a_rd and defined $a_wr and defined $b_rd and defined $b_wr) {
&ok(10);
print $a_wr "a wr inet\n";
my $b_input = <$b_rd>; chomp $b_input;
&ok_if(11, $b_input eq 'a wr inet');
print $b_wr "b wr inet\n";
my $a_input = <$a_rd>; chomp $a_input;
&ok_if(12, $a_input eq 'b wr inet');
}
else {
&many_ok(10, 12, "skipped: $^O does not support socketpair().");
}
}
### Test two-way inet sockets.
{ my ($a_rd, $a_wr, $b_rd, $b_wr) =
POE::Pipe::TwoWay->new('inet');
if (defined $a_rd and defined $a_wr and defined $b_rd and defined $b_wr) {
&ok(13);
print $a_wr "a wr inet\n";
my $b_input = <$b_rd>; chomp $b_input;
&ok_if(14, $b_input eq 'a wr inet');
print $b_wr "b wr inet\n";
my $a_input = <$a_rd>; chomp $a_input;
&ok_if(15, $a_input eq 'b wr inet');
}
else {
&many_ok(13, 15, "skipped: $^O does not support inet sockets.");
}
}
### Test Wheel::Run with filehandles. Uses "!" as a newline to avoid
### having to deal with whatever the system uses. Use double quotes
### if we're running on Windows. Wraps the input in an outer loop
### because Win32's non-blocking flag bleeds across "forks".
my $tty_flush_count = 0;
my $os_quote = ($^O eq 'MSWin32') ? q(") : q(');
my $program =
( "$^X -we $os_quote" .
'$/ = q(!); select STDERR; $| = 1; select STDOUT; $| = 1; ' .
'my $out = shift; '.
'my $err = shift; '.
'OUTER: while (1) { ' .
' while (<STDIN>) { ' .
' last OUTER if /^bye/; ' .
' print(STDOUT qq($out: $_)) if s/^out //; ' .
' print(STDERR qq($err: $_)) if s/^err //; ' .
' } ' .
'} ' .
"exit 0; $os_quote"
);
{ POE::Session->create
( inline_states =>
{ _start => sub {
my ($kernel, $heap) = @_[KERNEL, HEAP];
# Run a child process.
$heap->{wheel} = POE::Wheel::Run->new
( Program => $program,
ProgramArgs => [ "out", "err" ],
StdioFilter => POE::Filter::Line->new( Literal => "!" ),
StderrFilter => POE::Filter::Line->new( Literal => "!" ),
StdoutEvent => 'stdout_nonexistent',
StderrEvent => 'stderr_nonexistent',
ErrorEvent => 'error_nonexistent',
StdinEvent => 'stdin_nonexistent',
);
# Test event changing.
$heap->{wheel}->event( StdoutEvent => 'stdout',
StderrEvent => 'stderr',
StdinEvent => 'stdin',
);
$heap->{wheel}->event( ErrorEvent => 'error',
CloseEvent => 'close',
);
# Ask the child for something on stdout.
$heap->{wheel}->put( 'out test-out' );
},
# Error! Ow!
error => sub { },
# The child has closed. Delete its wheel.
close => sub {
delete $_[HEAP]->{wheel};
},
# Dummy _stop to prevent runtime errors.
_stop => sub { },
# Count every line that's flushed to the child.
stdin => sub { $tty_flush_count++; },
# Got a stdout response. Ask for something on stderr.
stdout => sub { &ok_if(17, $_[ARG0] eq 'out: test-out');
DEBUG and warn $_[ARG0];
$_[HEAP]->{wheel}->put( 'err test-err' );
},
# Got a sterr response. Tell the child to exit.
stderr => sub { &ok_if(18, $_[ARG0] eq 'err: test-err');
DEBUG and warn $_[ARG0];
$_[HEAP]->{wheel}->put( 'bye' );
},
},
);
}
### Test Wheel::Run with a coderef instead of a subprogram. Uses "!"
### as a newline to avoid having to deal with whatever the system
### uses. Wraps the input in an outer loop because Win32's
### non-blocking flag bleeds across "forks".
my $coderef_flush_count = 0;
if ($^O eq "MSWin32" and exists $INC{"Tk.pm"}) {
ok(23, "SKIP: Wheel::Run + Tk + ActiveState + CODE program = pain");
ok(24, "SKIP: Wheel::Run + Tk + ActiveState + CODE program = pain");
$coderef_flush_count = 3;
}
else {
my $program = sub {
my ($out, $err) = @_;
local $/ = q(!);
OUTER: while (1) {
while (<STDIN>) {
last OUTER if /^bye/;
print(STDOUT qq($out: $_)) if s/^out //;
print(STDERR qq($err: $_)) if s/^err //;
}
}
};
POE::Session->create
( inline_states =>
{ _start => sub {
my ($kernel, $heap) = @_[KERNEL, HEAP];
# Run a child process.
$heap->{wheel} = POE::Wheel::Run->new
( Program => $program,
ProgramArgs => [ "out", "err" ],
StdioFilter => POE::Filter::Line->new( Literal => "!" ),
StderrFilter => POE::Filter::Line->new( Literal => "!" ),
StdoutEvent => 'stdout_nonexistent',
StderrEvent => 'stderr_nonexistent',
ErrorEvent => 'error_nonexistent',
StdinEvent => 'stdin_nonexistent',
);
# Test event changing.
$heap->{wheel}->event( StdoutEvent => 'stdout',
StderrEvent => 'stderr',
StdinEvent => 'stdin',
);
$heap->{wheel}->event( ErrorEvent => 'error',
CloseEvent => 'close',
);
# Ask the child for something on stdout.
$heap->{wheel}->put( 'out test-out' );
},
# Error! Ow!
error => sub { },
# The child has closed. Delete its wheel.
close => sub {
delete $_[HEAP]->{wheel};
},
# Dummy _stop to prevent runtime errors.
_stop => sub { },
# Count every line that's flushed to the child.
stdin => sub { $coderef_flush_count++; },
# Got a stdout response. Ask for something on stderr.
stdout => sub { &ok_if(23, $_[ARG0] eq 'out: test-out');
DEBUG and warn $_[ARG0];
$_[HEAP]->{wheel}->put( 'err test-err' );
},
# Got a sterr response. Tell the child to exit.
stderr => sub { &ok_if(24, $_[ARG0] eq 'err: test-err');
DEBUG and warn $_[ARG0];
$_[HEAP]->{wheel}->put( 'bye' );
},
},
);
}
### Test Wheel::Run with ptys. Uses "!" as a newline to avoid having
### to deal with whatever the system uses.
my $pty_flush_count = 0;
if (POE::Wheel::Run::PTY_AVAILABLE) {
POE::Session->create
( inline_states =>
{ _start => sub {
my ($kernel, $heap) = @_[KERNEL, HEAP];
# Handle SIGCHLD.
$kernel->sig(CHLD => "sigchild");
# Run a child process.
$heap->{wheel} = POE::Wheel::Run->new
( Program => $program,
ProgramArgs => [ "out", "err" ],
StdioFilter => POE::Filter::Line->new( Literal => "!" ),
StdoutEvent => 'stdout_nonexistent',
ErrorEvent => 'error_nonexistent',
StdinEvent => 'stdin_nonexistent',
Conduit => 'pty',
);
# Test event changing.
$heap->{wheel}->event( StdoutEvent => 'stdout',
ErrorEvent => 'error',
StdinEvent => 'stdin',
);
# Ask the child for something on stdout.
$heap->{wheel}->put( 'out test-out' );
},
# Error! Ow!
error => sub { },
# Catch SIGCHLD. Stop the wheel if the exited child is ours.
sigchild => sub {
my $signame = $_[ARG0];
DEBUG and
warn "session ", $_[SESSION]->ID, " caught signal $signame\n";
my ($heap, $child_pid) = @_[HEAP, ARG1];
DEBUG and warn "\tthe child process ID is $child_pid\n";
if ($child_pid == $heap->{wheel}->PID()) {
DEBUG and warn "\tthe child process is ours\n";
delete $heap->{wheel};
}
return 0;
},
# Dummy _stop to prevent runtime errors.
_stop => sub { },
# Count every line that's flushed to the child.
stdin => sub { $pty_flush_count++; },
# Got a stdout response. Do a little expect/send dance.
stdout => sub {
my ($heap, $input) = @_[HEAP, ARG0];
if ($input eq 'out: test-out') {
&ok(20);
$heap->{wheel}->put( 'err test-err' );
}
elsif ($input eq 'err: test-err') {
&ok(21);
$heap->{wheel}->put( 'bye' );
}
},
},
);
}
else {
&many_ok( 19, 21, "skipped: IO::Pty is needed for this test.");
}
### Run the main loop.
$poe_kernel->run();
### Post-run tests.
&ok_if( 16, $tty_flush_count == 3 );
&ok_if( 19, $pty_flush_count == 3 ) if POE::Wheel::Run::PTY_AVAILABLE;
&ok_if( 22, $coderef_flush_count == 3 );
&results();
1;