1 package IPC::MorseSignals::TestSuite;
7 use POSIX qw/pause SIGINT SIGTERM SIGKILL SIGHUP EXIT_FAILURE/;
9 use IPC::MorseSignals::Emitter;
10 use IPC::MorseSignals::Receiver;
12 use base qw/Exporter/;
14 our @EXPORT_OK = qw/try bench init cleanup/;
16 $Data::Dumper::Indent = 0;
18 my ($lives, $pid, $rdr);
22 die 'forked too many times' if $lives < 0;
23 pipe $rdr, my $wtr or die "pipe() failed: $!";
26 die "fork() failed: $!";
29 close $rdr or die "close() failed: $!";
32 $SIG{__WARN__} = sub { print $wtr "!warn\n"; };
33 my $rcv = new IPC::MorseSignals::Receiver \%SIG, done => sub {
34 print $wtr Dumper($_[1]), "\n";
37 $SIG{ALRM} = sub { alarm 1; kill SIGHUP => $ppid };
39 $SIG{HUP} = sub { alarm 0; $rcv->reset }; # We can reset the alarm here.
44 local $SIG{HUP} = sub { $ready = 1 };
46 close $wtr or die "close() failed: $!";
47 my $oldfh = select $rdr;
54 close $rdr or die "close() falied: $!";
74 sub cleanup { slaughter }
76 my $snd = new IPC::MorseSignals::Emitter;
83 while (!$ok && (($speed /= 2) >= 1)) {
85 my $dump = Dumper($msg);
88 local $SIG{ALRM} = sub { die 'timeout' };
89 local $SIG{__WARN__} = sub { alarm 0; die 'do not want warnings' };
90 my $a = (int(100 * (3 * length $msg) / $speed) || 1);
100 if (!defined $r) { # Something bad happened, respawn
112 return ($ok) ? $speed : 0;
116 my ($l, $n, $diag, $res) = @_;
119 my @alpha = ('a' .. 'z');
120 my $msg = join '', map { $alpha[rand @alpha] } 1 .. $l;
121 my $dump = Dumper($msg);
122 my $desc_base = "$l bytes sent $n time" . ('s' x ($n != 1));
123 while (($ok < $n) && (($speed /= 2) >= 1)) {
125 my $desc = "$desc_base at $speed bits/s";
126 $diag->("try $desc...");
131 local $SIG{ALRM} = sub { die 'timeout' };
132 local $SIG{__WARN__} = sub { alarm 0; die 'do not want warnings' };
133 my $a = (int(100 * (3 * $l) / $speed) || 1);
143 if (!defined $r) { # Something bad happened, respawn
158 push @$res, $desc_base . (($speed) ? ' at ' . $speed . ' bits/s' : ' failed');