Commit | Line | Data |
a43cb6b7 |
1 | #!./perl |
2 | |
3 | BEGIN { |
4 | chdir 't' if -d 't'; |
5 | @INC = '../lib'; |
220939e6 |
6 | require './test.pl'; |
a43cb6b7 |
7 | } |
8 | |
9 | # Script to test auto flush on fork/exec/system/qx. The idea is to |
10 | # print "Pe" to a file from a parent process and "rl" to the same file |
11 | # from a child process. If buffers are flushed appropriately, the |
12 | # file should contain "Perl". We'll see... |
13 | use Config; |
14 | use warnings; |
15 | use strict; |
16 | |
17 | # This attempts to mirror the #ifdef forest found in perl.h so that we |
18 | # know when to run these tests. If that forest ever changes, change |
19 | # it here too or expect test gratuitous test failures. |
375927eb |
20 | my $useperlio = defined $Config{useperlio} ? $Config{useperlio} eq 'define' ? 1 : 0 : 0; |
21 | my $fflushNULL = defined $Config{fflushNULL} ? $Config{fflushNULL} eq 'define' ? 1 : 0 : 0; |
22 | my $d_sfio = defined $Config{d_sfio} ? $Config{d_sfio} eq 'define' ? 1 : 0 : 0; |
23 | my $fflushall = defined $Config{fflushall} ? $Config{fflushall} eq 'define' ? 1 : 0 : 0; |
24 | my $d_fork = defined $Config{d_fork} ? $Config{d_fork} eq 'define' ? 1 : 0 : 0; |
25 | |
26 | if ($useperlio || $fflushNULL || $d_sfio) { |
a6dd0448 |
27 | print "1..7\n"; |
a43cb6b7 |
28 | } else { |
375927eb |
29 | if ($fflushall) { |
a6dd0448 |
30 | print "1..7\n"; |
a43cb6b7 |
31 | } else { |
32 | print "1..0 # Skip: fflush(NULL) or equivalent not available\n"; |
33 | exit; |
34 | } |
35 | } |
36 | |
a6dd0448 |
37 | my $runperl = $^X =~ m/\s/ ? qq{"$^X"} : $^X; |
38 | $runperl .= qq{ "-I../lib"}; |
39 | |
a43cb6b7 |
40 | sub file_eq { |
41 | my $f = shift; |
42 | my $val = shift; |
43 | |
44 | open IN, $f or die "open $f: $!"; |
45 | chomp(my $line = <IN>); |
46 | close IN; |
47 | |
48 | print "# got $line\n"; |
49 | print "# expected $val\n"; |
50 | return $line eq $val; |
51 | } |
52 | |
53 | # This script will be used as the command to execute from |
54 | # child processes |
62a28c97 |
55 | my $ffprog = tempfile(); |
56 | open PROG, "> $ffprog" or die "open $ffprog: $!"; |
a43cb6b7 |
57 | print PROG <<'EOF'; |
58 | my $f = shift; |
59 | my $str = shift; |
60 | open OUT, ">> $f" or die "open $f: $!"; |
61 | print OUT $str; |
62 | close OUT; |
63 | EOF |
64 | ; |
62a28c97 |
65 | close PROG or die "close $ffprog: $!";; |
a43cb6b7 |
66 | |
67 | $| = 0; # we want buffered output |
68 | |
69 | # Test flush on fork/exec |
375927eb |
70 | if (!$d_fork) { |
a43cb6b7 |
71 | print "ok 1 # skipped: no fork\n"; |
72 | } else { |
62a28c97 |
73 | my $f = tempfile(); |
a43cb6b7 |
74 | open OUT, "> $f" or die "open $f: $!"; |
75 | print OUT "Pe"; |
76 | my $pid = fork; |
77 | if ($pid) { |
78 | # Parent |
79 | wait; |
80 | close OUT or die "close $f: $!"; |
81 | } elsif (defined $pid) { |
82 | # Kid |
83 | print OUT "r"; |
62a28c97 |
84 | my $command = qq{$runperl "$ffprog" "$f" "l"}; |
a43cb6b7 |
85 | print "# $command\n"; |
86 | exec $command or die $!; |
87 | exit; |
88 | } else { |
89 | # Bang |
90 | die "fork: $!"; |
91 | } |
92 | |
93 | print file_eq($f, "Perl") ? "ok 1\n" : "not ok 1\n"; |
a43cb6b7 |
94 | } |
95 | |
96 | # Test flush on system/qx/pipe open |
97 | my %subs = ( |
98 | "system" => sub { |
99 | my $c = shift; |
100 | system $c; |
101 | }, |
102 | "qx" => sub { |
103 | my $c = shift; |
104 | qx{$c}; |
105 | }, |
106 | "popen" => sub { |
107 | my $c = shift; |
108 | open PIPE, "$c|" or die "$c: $!"; |
109 | close PIPE; |
110 | }, |
111 | ); |
112 | my $t = 2; |
113 | for (qw(system qx popen)) { |
114 | my $code = $subs{$_}; |
62a28c97 |
115 | my $f = tempfile(); |
116 | my $command = qq{$runperl $ffprog "$f" "rl"}; |
a43cb6b7 |
117 | open OUT, "> $f" or die "open $f: $!"; |
118 | print OUT "Pe"; |
f126f811 |
119 | close OUT or die "close $f: $!";; |
a43cb6b7 |
120 | print "# $command\n"; |
121 | $code->($command); |
a43cb6b7 |
122 | print file_eq($f, "Perl") ? "ok $t\n" : "not ok $t\n"; |
a43cb6b7 |
123 | ++$t; |
124 | } |
a6dd0448 |
125 | |
220939e6 |
126 | my $cmd = _create_runperl( |
127 | switches => ['-l'], |
128 | prog => |
129 | sprintf('print qq[ok $_] for (%d..%d)', $t, $t+2)); |
130 | print "# cmd = '$cmd'\n"; |
a6dd0448 |
131 | open my $CMD, "$cmd |" or die "Can't open pipe to '$cmd': $!"; |
132 | while (<$CMD>) { |
133 | system("$runperl -e 0"); |
134 | print; |
135 | } |
136 | close $CMD; |
137 | $t += 3; |