| 1 | package App::MultiModule::Test::StatefulStream; | ||
| 2 | $App::MultiModule::Test::StatefulStream::VERSION = '1.171870'; | ||
| 3 | use strict;use warnings; | ||
| 4 | use Test::More; | ||
| 5 | use IPC::Transit; | ||
| 6 | use Storable; | ||
| 7 | use Message::Match qw(mmatch); | ||
| 8 | |||
| 9 | use App::MultiModule::API; | ||
| 10 | |||
| 11 | my $inc; | ||
| 12 | { | ||
| 13 | my $i; | ||
| 14 | $inc = sub { | ||
| 15 | $i = 1 unless $i; | ||
| 16 | $i++; | ||
| 17 | return $i; | ||
| 18 | }; | ||
| 19 | } | ||
| 20 | |||
| 21 | |||
| 22 | =head2 send | ||
| 23 | =cut | ||
| 24 | sub new { | ||
| 25 | my $class = shift; | ||
| 26 | my %args = @_; | ||
| 27 | die "App::MultiModule::Test::StatefulStream::new: required argument 'task_name' not passed" | ||
| 28 | unless $args{task_name}; | ||
| 29 | # die "App::MultiModule::Test::StatefulStream::new: required argument 'program_pid' not passed" | ||
| 30 | # unless $args{program_pid}; | ||
| 31 | $args{timeout} = 40 unless $args{timeout}; | ||
| 32 | $args{match} = { | ||
| 33 | this => int rand 10000, | ||
| 34 | that => $inc->(), | ||
| 35 | } unless $args{match}; | ||
| 36 | my $self = { | ||
| 37 | increment => int rand 10000, | ||
| 38 | task_name => $args{task_name}, | ||
| 39 | program_pid => $args{program_pid}, | ||
| 40 | current_id => 1, | ||
| 41 | sent_messages => [], | ||
| 42 | timeout => $args{timeout}, | ||
| 43 | out_qname => $args{task_name} . '_out', | ||
| 44 | match => $args{match}, | ||
| 45 | }; | ||
| 46 | |||
| 47 | bless ($self, $class); | ||
| 48 | |||
| 49 | return $self; | ||
| 50 | } | ||
| 51 | |||
| 52 | =head2 send | ||
| 53 | =cut | ||
| 54 | sub send { | ||
| 55 | my $self = shift; | ||
| 56 | my %args = @_; | ||
| 57 | my $ct = int rand 10000; | ||
| 58 | my $id = $self->{current_id}++; | ||
| 59 | push @{$self->{sent_messages}}, { id => $id, ct => $ct }; | ||
| 60 | my $message = Storable::dclone $self->{match}; | ||
| 61 | if($args{extras}) { | ||
| 62 | while(my($key,$value) = each %{$args{extras}}) { | ||
| 63 | $message->{$key} = $value; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | $message->{ct} = $ct; | ||
| 67 | $message->{id} = $id; | ||
| 68 | ok IPC::Transit::send(qname => $self->{task_name}, message => $message); | ||
| 69 | } | ||
| 70 | |||
| 71 | my $incrementer_emit_ct; | ||
| 72 | |||
| 73 | =head2 receive | ||
| 74 | =cut | ||
| 75 | sub receive { | ||
| 76 | my $self = shift; | ||
| 77 | my %args = @_; | ||
| 78 | $incrementer_emit_ct = 1 unless defined $incrementer_emit_ct; | ||
| 79 | my $ret; | ||
| 80 | while(my $message = IPC::Transit::receive(qname => 'Incrementer_out', nonblock => 1)) { | ||
| 81 | # print STDERR '$message->{emit_ct} == $incrementer_emit_ct' . "$message->{emit_ct} == $incrementer_emit_ct\n"; | ||
| 82 | ok $message->{emit_ct} == $incrementer_emit_ct; | ||
| 83 | $incrementer_emit_ct++; | ||
| 84 | } | ||
| 85 | return undef unless scalar @{$self->{sent_messages}}; | ||
| 86 | my $task_name = $self->{task_name}; | ||
| 87 | my $out_qname = $self->{out_qname}; | ||
| 88 | my $module_pid; | ||
| 89 | eval { | ||
| 90 | local $SIG{ALRM} = sub { die "timed out\n"; }; | ||
| 91 | alarm $self->{timeout}; | ||
| 92 | ok my $msg = IPC::Transit::receive(qname => $out_qname); | ||
| 93 | $ret = $msg; | ||
| 94 | $module_pid = $msg->{module_pid}; | ||
| 95 | if($args{match}) { | ||
| 96 | # print STDERR 'msg: ' . Data::Dumper::Dumper $msg; | ||
| 97 | # print STDERR 'match: ' . Data::Dumper::Dumper $args{match}; | ||
| 98 | ok mmatch $msg, $args{match}; | ||
| 99 | } | ||
| 100 | my $info = shift @{$self->{sent_messages}}; | ||
| 101 | |||
| 102 | print STDERR '$msg->{ct} == $info->{ct}: ' . "$msg->{ct} : $info->{ct}\n" unless $msg->{ct} == $info->{ct}; | ||
| 103 | ok $msg->{ct} == $info->{ct}; | ||
| 104 | |||
| 105 | print STDERR '$msg->{id} == $info->{id}: ' . "$msg->{id} : $info->{id}\n" unless $msg->{id} == $info->{id}; | ||
| 106 | ok $msg->{id} == $info->{id}; | ||
| 107 | |||
| 108 | print '$msg->{my_ct} == $info->{ct} + $self->{increment}: ' . "$msg->{my_ct} : " . ($info->{ct} + $self->{increment}) . "\n" unless $msg->{my_ct} == $info->{ct} + $self->{increment}; | ||
| 109 | ok $msg->{my_ct} == $info->{ct} + $self->{increment}; | ||
| 110 | |||
| 111 | if($self->{program_pid}) { | ||
| 112 | if($args{type} and $args{type} eq 'external') { | ||
| 113 | ok $msg->{module_pid} != $self->{program_pid}; | ||
| 114 | } | ||
| 115 | if($args{type} and $args{type} eq 'internal') { | ||
| 116 | ok $msg->{module_pid} == $self->{program_pid}; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | }; | ||
| 120 | alarm 0; | ||
| 121 | ok not $@ unless $args{no_fail_exceptions}; | ||
| 122 | if($@) { | ||
| 123 | print STDERR "exception: $@\n"; | ||
| 124 | print STDERR Data::Dumper::Dumper \%args; | ||
| 125 | print STDERR "\$task_name=$task_name \$out_qname=$out_qname \$module_pid=$module_pid\n"; | ||
| 126 | } else { | ||
| 127 | print STDERR "OK: \$task_name=$task_name \$out_qname=$out_qname \$module_pid=$module_pid\n"; | ||
| 128 | } | ||
| 129 | return undef if $@; | ||
| 130 | return $ret; | ||
| 131 | } | ||
| 132 | 1; |