1#!/usr/bin/env perl 2# -*- cperl -*- 3 4# Copyright (c) 2007, 2008 MySQL AB 5# Use is subject to license terms. 6# 7# This program is free software; you can redistribute it and/or modify 8# it under the terms of the GNU General Public License as published by 9# the Free Software Foundation; version 2 of the License. 10# 11# This program is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14# GNU General Public License for more details. 15# 16# You should have received a copy of the GNU General Public License 17# along with this program; if not, write to the Free Software 18# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA 19 20use strict; 21use FindBin; 22use My::SafeProcess; 23 24# 25# Test longterm running of SafeProcess 26# 27 28my $perl_path= $^X; 29my $verbose= 0; 30my $loops= 100; 31 32print "kill one and wait for one\n"; 33for (1...$loops){ 34 use File::Temp qw / tempdir /; 35 my $dir = tempdir( CLEANUP => 1 ); 36 37 my @procs; 38 for (1..10){ 39 40 my $args= [ "$FindBin::Bin/dummyd.pl", "--vardir=$dir" ]; 41 my $proc= My::SafeProcess->new 42 ( 43 path => $perl_path, 44 args => \$args, 45 verbose => $verbose, 46 ); 47 push(@procs, $proc); 48 } 49 50 foreach my $proc (@procs) { 51 $proc->kill(); 52 # dummyd will always be killed and thus 53 # exit_status should have been set to 1 54 die "oops, exit_status: ", $proc->exit_status() 55 unless $proc->exit_status() == 1; 56 } 57 58 print "=" x 60, "\n"; 59} 60 61 62print "With 1 second sleep in dummyd\n"; 63for (1...$loops){ 64 use File::Temp qw / tempdir /; 65 my $dir = tempdir( CLEANUP => 1 ); 66 67 my @procs; 68 for (1..10){ 69 70 my $args= [ "$FindBin::Bin/dummyd.pl", 71 "--vardir=$dir", 72 "--sleep=1" ]; 73 my $proc= My::SafeProcess->new 74 ( 75 path => $perl_path, 76 args => \$args, 77 verbose => $verbose, 78 ); 79 push(@procs, $proc); 80 } 81 82 foreach my $proc (@procs) { 83 $proc->kill(); 84 } 85 86 print "=" x 60, "\n"; 87} 88 89print "kill all and wait for one\n"; 90for (1...$loops){ 91 use File::Temp qw / tempdir /; 92 my $dir = tempdir( CLEANUP => 1 ); 93 94 my @procs; 95 for (1..10){ 96 97 my $args= [ "$FindBin::Bin/dummyd.pl", "--vardir=$dir" ]; 98 my $proc= My::SafeProcess->new 99 ( 100 path => $perl_path, 101 args => \$args, 102 verbose => $verbose, 103 ); 104 push(@procs, $proc); 105 } 106 107 foreach my $proc (@procs) { 108 $proc->start_kill(); 109 } 110 111 foreach my $proc (@procs) { 112 $proc->wait_one(); 113 } 114 115 print "=" x 60, "\n"; 116} 117 118print "kill all using shutdown without callback\n"; 119for (1...$loops){ 120 use File::Temp qw / tempdir /; 121 my $dir = tempdir( CLEANUP => 1 ); 122 123 my @procs; 124 for (1..10){ 125 126 my $args= [ "$FindBin::Bin/dummyd.pl", "--vardir=$dir" ]; 127 my $proc= My::SafeProcess->new 128 ( 129 path => $perl_path, 130 args => \$args, 131 verbose => $verbose, 132 ); 133 push(@procs, $proc); 134 } 135 136 My::SafeProcess::shutdown(2, @procs); 137 138 print "=" x 60, "\n"; 139} 140 141print "kill all using shutdown\n"; 142for (1...$loops){ 143 use File::Temp qw / tempdir /; 144 my $dir = tempdir( CLEANUP => 1 ); 145 146 my @procs; 147 for (1..10){ 148 149 my $args= [ "$FindBin::Bin/dummyd.pl", "--vardir=$dir" ]; 150 my $proc= My::SafeProcess->new 151 ( 152 path => $perl_path, 153 args => \$args, 154 verbose => $verbose, 155 shutdown => sub { }, # Does nothing 156 ); 157 push(@procs, $proc); 158 } 159 160 My::SafeProcess::shutdown(2, @procs); 161 162 print "=" x 60, "\n"; 163} 164 165exit(0); 166