]> git.vpit.fr Git - perl/modules/IPC-MorseSignals.git/blob - t/10-base.t
Importing IPC-MorseSignals-0.02.tar.gz
[perl/modules/IPC-MorseSignals.git] / t / 10-base.t
1 #!perl -T
2
3 use Test::More tests => 7 * 5;
4
5 use POSIX qw/SIGINT SIGTERM SIGKILL EXIT_SUCCESS/;
6
7 use IPC::MorseSignals qw/msend mrecv/;
8
9 my $speed = 128;
10
11 sub trysend {
12  my ($msg, $desc) = @_;
13  pipe my $rdr, my $wtr or die "$desc: pipe() failed : $!";
14  my $pid = fork;
15  if (!defined $pid) {
16   die "$desc: fork() failed : $!";
17  } elsif ($pid == 0) {
18   close $rdr;
19   local @SIG{qw/USR1 USR2/} = mrecv sub {
20    print $wtr $_[0], "\n";
21    close $wtr;
22    exit EXIT_SUCCESS;
23   };
24   1 while 1;
25  }
26  close $wtr or die "$desc: close() failed : $!";
27  eval {
28   local $SIG{ALRM} = sub { die 'timeout' };
29   my $a = (int(100 * (3 * length $msg) / $speed) || 1);
30   $a = 10 if $a > 10;
31   alarm $a;
32   msend $msg => $pid, $speed;
33   waitpid $pid, 0;
34  };
35  alarm 0;
36  if ($@) {
37   kill SIGINT,  $pid;
38   kill SIGTERM, $pid;
39   kill SIGKILL, $pid;
40   die "$desc: died ($@)";
41  }
42  my $recv = do { local $/; <$rdr> };
43  close $rdr;
44  chomp $recv;
45  ok($msg eq $recv, $desc);
46 }
47
48 for (1 .. 5) {
49  trysend 'hello', 'ascii';
50  trysend 'éàùçà', 'extended';
51  trysend '€€€', 'unicode';
52  trysend 'a€bécàd€e', 'mixed';
53  trysend "\0" x 10, 'few bits';
54  trysend "\x{FF}" x 10, 'lots of bits';
55  trysend "a\0b", 'null character';
56 }