| 1 | package MultiModuleTest::OtherExternalModule; | ||
| 2 | $MultiModuleTest::OtherExternalModule::VERSION = '1.171870'; | ||
| 3 | use strict;use warnings; | ||
| 4 | use Data::Dumper; | ||
| 5 | use Message::Transform qw(mtransform); | ||
| 6 | use IPC::Transit; | ||
| 7 | |||
| 8 | use parent 'App::MultiModule::Task'; | ||
| 9 | |||
| 10 | my @leaked_memory = (); | ||
| 11 | |||
| 12 | =head2 is_stateful | ||
| 13 | |||
| 14 | =cut | ||
| 15 | sub is_stateful { | ||
| 16 | return 'yes!'; | ||
| 17 | } | ||
| 18 | |||
| 19 | =head2 message | ||
| 20 | |||
| 21 | =cut | ||
| 22 | sub message { | ||
| 23 | my $self = shift; | ||
| 24 | my $message = shift; | ||
| 25 | if($message->{crash_me}) { | ||
| 26 | print STDERR "OtherExternalModule: I have been commanded to crash\n"; | ||
| 27 | kill 9, $$; #!!! | ||
| 28 | } | ||
| 29 | if($message->{uncaught_exception}) { | ||
| 30 | my $a = 0; | ||
| 31 | my $b = 1; | ||
| 32 | my $c = $b / $a; | ||
| 33 | #Oddly, I felt some kind of crazy thrill writing the above three lines | ||
| 34 | } | ||
| 35 | if($message->{leak_memory}) { | ||
| 36 | #print STDERR "OtherExternalModule leaking $message->{leak_memory} bytes of memory\n"; | ||
| 37 | push @leaked_memory, 'h' x $message->{leak_memory}; | ||
| 38 | } | ||
| 39 | if($message->{spin_cpu}) { | ||
| 40 | #print STDERR "OtherExternalModule spinning CPU for $message->{spin_cpu} seconds\n"; | ||
| 41 | local $SIG{ALRM} = sub { die "timed out\n"; }; | ||
| 42 | alarm $message->{spin_cpu}; | ||
| 43 | eval { | ||
| 44 | while(1) { | ||
| 45 | my $a = 'wtf'; | ||
| 46 | } | ||
| 47 | }; | ||
| 48 | } | ||
| 49 | if($message->{fill_queue}) { | ||
| 50 | my $bytes = $message->{fill_queue_bytes} || 1024; | ||
| 51 | IPC::Transit::send( | ||
| 52 | qname => $message->{fill_queue}, | ||
| 53 | message => { junk => 'h' x $bytes }, | ||
| 54 | nonblock => 1, | ||
| 55 | ); | ||
| 56 | } | ||
| 57 | # my $ct = 0; | ||
| 58 | # while(1) { | ||
| 59 | # $ct++; | ||
| 60 | # } | ||
| 61 | # unlink 'thing.out'; | ||
| 62 | # while(1) { | ||
| 63 | # { open my $fh, '>>', 'thing.out'; | ||
| 64 | # print $fh 'h' x 1023 for (1..2); | ||
| 65 | # close $fh; | ||
| 66 | # } | ||
| 67 | # my $sys = "cat /proc/$$/io"; | ||
| 68 | # system $sys; | ||
| 69 | # sleep 1; | ||
| 70 | # } | ||
| 71 | =head1 out | ||
| 72 | my @fh; | ||
| 73 | my @mem; | ||
| 74 | while(1) { | ||
| 75 | for(1..20) { | ||
| 76 | open my $fh, '<', 'thing.out'; | ||
| 77 | push @fh, $fh; | ||
| 78 | IPC::Transit::send(qname => 'test', message => { junk => 'hsddddddddddddddddddddddddddddddddddd' }); | ||
| 79 | } | ||
| 80 | push @mem, 'h' x (1024 * 1024) for (1..300); | ||
| 81 | print 'scalar @fh=' . scalar @fh . "\n"; | ||
| 82 | local $SIG{ALRM} = sub { die "timed out\n"; }; | ||
| 83 | alarm 1; | ||
| 84 | my $ct = 0; | ||
| 85 | eval { | ||
| 86 | while(1) { | ||
| 87 | $ct++; | ||
| 88 | } | ||
| 89 | }; | ||
| 90 | sleep 1; | ||
| 91 | } | ||
| 92 | =cut | ||
| 93 | if($message->{oob_testing}) { | ||
| 94 | $self->oob_testing($message); | ||
| 95 | return; | ||
| 96 | } | ||
| 97 | if($self->{config}->{transform}) { | ||
| 98 | mtransform $message, | ||
| 99 | $self->{config}->{transform}; | ||
| 100 | } | ||
| 101 | my $incr = $self->{config}->{increment_by}; | ||
| 102 | my $ct = $message->{ct}; | ||
| 103 | $self->debug('OtherExternalModule.pm: (message)', message => $message) | ||
| 104 | if $self->{debug} > 5; | ||
| 105 | $self->debug('OtherExternalModule.pm: (config)', config => $self->{config}) | ||
| 106 | if $self->{debug} > 5; | ||
| 107 | $message->{my_ct} = $ct + $incr; | ||
| 108 | $self->{state}->{sum_increment_by} = 0 unless $self->{state}->{sum_increment_by}; | ||
| 109 | $self->{state}->{sum_increment_by} += $incr; | ||
| 110 | $message->{sum_increment_by} = $self->{state}->{sum_increment_by}; | ||
| 111 | $message->{module_pid} = $$; | ||
| 112 | # $self->debug("$$: OtherExternalModule message: " . Data::Dumper::Dumper $message) if $self->{debug}; | ||
| 113 | $self->{state}->{most_recent} = $message->{my_ct}; | ||
| 114 | $self->emit($message); | ||
| 115 | } | ||
| 116 | |||
| 117 | =head2 oob_testing | ||
| 118 | |||
| 119 | =cut | ||
| 120 | sub oob_testing { | ||
| 121 | my $self = shift; | ||
| 122 | my $message = shift; | ||
| 123 | |||
| 124 | $self->send_oob($message->{oob_testing}->{type}, $message); | ||
| 125 | } | ||
| 126 | |||
| 127 | 1; |