'~~' is not a feature [repost]
[p5sagit/p5-mst-13.2.git] / lib / File / Temp / t / fork.t
CommitLineData
b0ad0448 1#!/usr/bin/perl
2$| = 1;
3
4# Note that because fork loses test count we do not use Test::More
5
6use strict;
7
8BEGIN { print "1..8\n"; }
9
10use File::Temp;
11
12# OO interface
13
14my $file = File::Temp->new(CLEANUP=>1);
15
16myok( 1, -f $file->filename, "OO File exists" );
17
18my $children = 2;
19for my $i (1 .. $children) {
20 my $pid = fork;
21 die "Can't fork: $!" unless defined $pid;
22 if ($pid) {
23 # parent process
24 next;
25 } else {
26 # in a child we can't keep the count properly so we do it manually
27 # make sure that child 1 dies first
28 srand();
29 my $time = (($i-1) * 5) +int(rand(5));
30 print "# child $i sleeping for $time seconds\n";
31 sleep($time);
32 my $count = $i + 1;
33 myok( $count, -f $file->filename(), "OO file present in child $i" );
34 print "# child $i exiting\n";
35 exit;
36 }
37}
38
39while ($children) {
40 wait;
41 $children--;
42}
43
44
45
46myok( 4, -f $file->filename(), "OO File exists in parent" );
47
48# non-OO interface
49
50my ($fh, $filename) = File::Temp::tempfile( CLEANUP => 1 );
51
52myok( 5, -f $filename, "non-OO File exists" );
53
54$children = 2;
55for my $i (1 .. $children) {
56 my $pid = fork;
57 die "Can't fork: $!" unless defined $pid;
58 if ($pid) {
59 # parent process
60 next;
61 } else {
62 srand();
63 my $time = (($i-1) * 5) +int(rand(5));
64 print "# child $i sleeping for $time seconds\n";
65 sleep($time);
66 my $count = 5 + $i;
67 myok( $count, -f $filename, "non-OO File present in child $i" );
68 print "# child $i exiting\n";
69 exit;
70 }
71}
72
73while ($children) {
74 wait;
75 $children--;
76}
77myok(8, -f $filename, "non-OO File exists in parent" );
78
79
80# Local ok sub handles explicit number
81sub myok {
82 my ($count, $test, $msg) = @_;
83
84 if ($test) {
85 print "ok $count - $msg\n";
86 } else {
87 print "not ok $count - $msg\n";
88 }
89 return $test;
90}