| 1 | package App::MultiModule::Test; | ||
| 2 | $App::MultiModule::Test::VERSION = '1.171870'; | ||
| 3 | use strict;use warnings; | ||
| 4 | use POSIX ":sys_wait_h"; | ||
| 5 | use IPC::Transit; | ||
| 6 | use Test::More; | ||
| 7 | use Message::Match qw(mmatch); | ||
| 8 | |||
| 9 | my @test_queues = qw/test_queue_full tqueue tqueue_out OtherModule Router OtherExternalModule tqueue_out_alt OtherModule_out YetAnotherExternalModule_out OtherExternalModule OtherModule YetAnotherExternalModule tqueue_out_secondary tqueue_out_tertiary OutOfBand MultiModule OtherExternalModule OtherModule Router test_alert_queue OtherExternalModule_out OtherModule_out Incrementer StatelessProducer Incrementer_out TaskDoesNotCompile/; | ||
| 10 | |||
| 11 | =head2 begin | ||
| 12 | =cut | ||
| 13 | sub begin { | ||
| 14 | if($^O !~ /linux/i) { | ||
| 15 | ok 1, 'this only works on Linux, so we are just going to pass'; | ||
| 16 | done_testing(); | ||
| 17 | exit 0; | ||
| 18 | } | ||
| 19 | # $IPC::Transit::config_dir = "/tmp/app_multimodule_transit_$$"; | ||
| 20 | unlink 'test.conf' if -e 'test.conf'; | ||
| 21 | system 'rm -rf state'; | ||
| 22 | clear_queue($_) for @test_queues; | ||
| 23 | unlink 'debug.out'; | ||
| 24 | } | ||
| 25 | |||
| 26 | my $program_pid; | ||
| 27 | |||
| 28 | =head2 finish | ||
| 29 | =cut | ||
| 30 | sub finish { | ||
| 31 | unlink 'test.conf' if -e 'test.conf'; | ||
| 32 | system 'rm -rf state'; | ||
| 33 | clear_queue($_) for @test_queues; | ||
| 34 | unlink 'debug.out'; | ||
| 35 | system "rm -rf /tmp/app_multimodule_transit_$$"; | ||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | =head2 run_program | ||
| 40 | =cut | ||
| 41 | sub run_program { | ||
| 42 | my $args = shift or die "App::MultiModule::Test::run_program: args required"; | ||
| 43 | my @args = split /\s+/, $args; | ||
| 44 | |||
| 45 | # unshift @args, '-Ilib', 'bin/MultiModule'; #, '-T'; | ||
| 46 | # push @args, "/tmp/app_multimodule_transit_$$"; | ||
| 47 | my $new_pid = fork; | ||
| 48 | die "App::MultiModule::Test::run_program: fork failed: $!" | ||
| 49 | if not defined $new_pid; | ||
| 50 | if(not $new_pid) { | ||
| 51 | $ENV{PATH}="bin/:$ENV{PATH}"; | ||
| 52 | exec 'bin/MultiModule', @args; | ||
| 53 | exit; | ||
| 54 | } | ||
| 55 | sleep 1; | ||
| 56 | eval { | ||
| 57 | open my $fh, '<', "/proc/$new_pid/cmdline" | ||
| 58 | or die "failed to open /proc/$new_pid/cmdline for reading: $!\n"; | ||
| 59 | read $fh, my $cmdline, 1024 | ||
| 60 | or die "failed to read from /proc/$new_pid/cmdline: $!\n";; | ||
| 61 | close $fh | ||
| 62 | or die "failed to close /proc/$new_pid/cmdline: $!\n"; | ||
| 63 | }; | ||
| 64 | if($@) { | ||
| 65 | die "App::MultiModule::Test::run_program: failed: $@\n"; | ||
| 66 | } | ||
| 67 | $program_pid = $new_pid; | ||
| 68 | return $program_pid; | ||
| 69 | } | ||
| 70 | |||
| 71 | =head2 cleanly_exit | ||
| 72 | =cut | ||
| 73 | sub cleanly_exit { | ||
| 74 | my $qname = shift; | ||
| 75 | ok IPC::Transit::send(qname => 'tqueue', message => { | ||
| 76 | '.multimodule' => { | ||
| 77 | control => [ | ||
| 78 | { type => 'cleanly_exit', | ||
| 79 | exit_externals => 1, | ||
| 80 | } | ||
| 81 | ], | ||
| 82 | } | ||
| 83 | }); | ||
| 84 | sleep 6; | ||
| 85 | } | ||
| 86 | |||
| 87 | =head2 term_program | ||
| 88 | =cut | ||
| 89 | sub term_program { | ||
| 90 | kill 15, $program_pid; | ||
| 91 | sleep 4; | ||
| 92 | kill 9, $program_pid; | ||
| 93 | sleep 2; | ||
| 94 | waitpid($program_pid, WNOHANG); | ||
| 95 | } | ||
| 96 | |||
| 97 | =head2 fetch_alerts | ||
| 98 | =cut | ||
| 99 | sub fetch_alerts { | ||
| 100 | my $qname = shift; | ||
| 101 | my $match = shift; | ||
| 102 | my $got_levels = shift; | ||
| 103 | my $how_long = shift; | ||
| 104 | my %args = @_; | ||
| 105 | my $founds = []; | ||
| 106 | $got_levels = {} unless $got_levels; | ||
| 107 | local $SIG{ALRM} = sub { die "timed out\n"; }; | ||
| 108 | alarm $how_long; | ||
| 109 | eval { | ||
| 110 | while(1) { | ||
| 111 | while(my $message = IPC::Transit::receive(qname => $qname)) { | ||
| 112 | if( $message->{messages} and | ||
| 113 | ref $message->{messages} eq 'ARRAY' and | ||
| 114 | $message->{messages}->[0] and | ||
| 115 | $message->{messages}->[0]->{args} and | ||
| 116 | $message->{messages}->[0]->{args}->{message}) { | ||
| 117 | my $m = $message->{messages}->[0]->{args}->{message}; | ||
| 118 | # print STDERR '$m=' . Data::Dumper::Dumper $m; | ||
| 119 | if(mmatch($m, $match)) { | ||
| 120 | $got_levels->{$m->{level}} = 0 | ||
| 121 | unless $got_levels->{$m->{level}}; | ||
| 122 | $got_levels->{$m->{level}}++; | ||
| 123 | push @{$founds}, $m; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | } | ||
| 127 | } | ||
| 128 | }; | ||
| 129 | return ($got_levels, $founds); | ||
| 130 | } | ||
| 131 | |||
| 132 | =head2 clear_queue | ||
| 133 | =cut | ||
| 134 | sub clear_queue { | ||
| 135 | my $qname = shift; | ||
| 136 | IPC::Transit::receive(qname => $qname, nonblock => 1) for (1..100); | ||
| 137 | } | ||
| 138 | |||
| 139 | 1; | ||
| 140 |