| 1 | package Test::Myriad::Service; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | our $VERSION = '1.001'; # VERSION | ||
| 7 | our $AUTHORITY = 'cpan:DERIV'; # AUTHORITY | ||
| 8 | |||
| 9 | 1 | ▶ | use Scalar::Util qw(weaken); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use Sub::Util; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | 0 | ▶ | use Myriad::Service::Implementation; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | use Myriad::Class; | ||
| 14 | use Myriad::Service::Attributes; | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | Test::Myriad::Service - an abstraction to mock microservices. | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | my $service = Myriad::Test::Service->new(..); | ||
| 23 | $service->add_rpc('rpc_name', %default_response); | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | =head1 Methods | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | has $name; | ||
| 32 | has $pkg; | ||
| 33 | has $meta_service; | ||
| 34 | has $myriad; | ||
| 35 | |||
| 36 | has $default_rpc; | ||
| 37 | has $mocked_rpc; | ||
| 38 | |||
| 39 | BUILD (%args) { | ||
| 40 | $meta_service = delete $args{meta}; | ||
| 41 | $pkg = delete $args{pkg}; | ||
| 42 | weaken($myriad = delete $args{myriad}); | ||
| 43 | |||
| 44 | $default_rpc = {}; | ||
| 45 | $mocked_rpc = {}; | ||
| 46 | |||
| 47 | # Replace the RPC subs with a mockable | ||
| 48 | # version if the class already exists | ||
| 49 | try { | ||
| 50 | if (my $methods = $myriad->registry->rpc_for($pkg)) { | ||
| 51 | for my $method (keys $methods->%*) { | ||
| 52 | $default_rpc->{$method} = $methods->{$method}->{code}; | ||
| 53 | $methods->{$method}->{code} = async sub { | ||
| 54 | if ($mocked_rpc->{$method}) { | ||
| 55 | return delete $mocked_rpc->{$method}; | ||
| 56 | } | ||
| 57 | try { | ||
| 58 | my $self = shift; | ||
| 59 | await $self->$method(@_); | ||
| 60 | } catch ($e) { | ||
| 61 | $log->tracef("An exception has been thrown while calling the original sub - %s", $e); | ||
| 62 | die $e; | ||
| 63 | } | ||
| 64 | }; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | } catch ($e) { | ||
| 68 | $log->tracef('Myriad::Registry error while checking %s, %s', $pkg, $e); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | =head2 add_rpc | ||
| 73 | |||
| 74 | Attaches a new RPC to the service with a defaultt response. | ||
| 75 | |||
| 76 | =over 4 | ||
| 77 | |||
| 78 | =item * C<name> - The name of the RPC. | ||
| 79 | |||
| 80 | =item * C<response> - A hash that will be sent as the response. | ||
| 81 | |||
| 82 | =back | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | method add_rpc ($name, %response) { | ||
| 87 | my $faker = async sub { | ||
| 88 | if ($mocked_rpc->{$name}) { | ||
| 89 | return delete $mocked_rpc->{$name}; | ||
| 90 | } elsif (my $default_response = $default_rpc->{$name}) { | ||
| 91 | return $default_response; | ||
| 92 | } | ||
| 93 | }; | ||
| 94 | |||
| 95 | # Don't prefix the RPC name it's used in messages delivery. | ||
| 96 | |||
| 97 | Myriad::Service::Attributes->apply_attributes( | ||
| 98 | class => $meta_service->name, | ||
| 99 | code => Sub::Util::set_subname($name, $faker), | ||
| 100 | attributes => ['RPC'], | ||
| 101 | ); | ||
| 102 | |||
| 103 | $default_rpc->{$name} = \%response; | ||
| 104 | $meta_service->add_method($name, $faker); | ||
| 105 | |||
| 106 | $self; | ||
| 107 | } | ||
| 108 | |||
| 109 | =head2 mock_rpc | ||
| 110 | |||
| 111 | Override the original RPC response for a single call. | ||
| 112 | |||
| 113 | =over 4 | ||
| 114 | |||
| 115 | =item * C<name> - The name of the RPC to be mocked. | ||
| 116 | |||
| 117 | =item * C<response> - A hash that will be sent as the response. | ||
| 118 | |||
| 119 | =back | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | method mock_rpc ($name, %response) { | ||
| 124 | die 'You should define rpc methdos using "add_rpc" first' unless $default_rpc->{$name}; | ||
| 125 | die 'You cannot mock RPC call twice' if $mocked_rpc->{$name}; | ||
| 126 | $mocked_rpc->{$name} = \%response; | ||
| 127 | |||
| 128 | $self; | ||
| 129 | } | ||
| 130 | |||
| 131 | =head2 call_rpc | ||
| 132 | |||
| 133 | A shortcut to call an RPC in the current service. | ||
| 134 | |||
| 135 | The call will be conducted over Myriad Transport and not | ||
| 136 | as a method invocation. | ||
| 137 | |||
| 138 | =over 4 | ||
| 139 | |||
| 140 | =item * C<method> - The RPC method name. | ||
| 141 | |||
| 142 | =item * C<args> - A hash of the method arguments. | ||
| 143 | |||
| 144 | =back | ||
| 145 | |||
| 146 | =cut | ||
| 147 | |||
| 148 | async method call_rpc ($method, %args) { | ||
| 149 | my $service_name = $myriad->registry->make_service_name($pkg); | ||
| 150 | await $myriad->rpc_client->call_rpc($service_name, $method, %args); | ||
| 151 | } | ||
| 152 | |||
| 153 | =head2 add_subscription | ||
| 154 | |||
| 155 | Creats a new subscription in the service. | ||
| 156 | |||
| 157 | This sub takes the source of the data in multiple ways | ||
| 158 | described in the parameters section, only one of them required. | ||
| 159 | |||
| 160 | =over 4 | ||
| 161 | |||
| 162 | =item * C<channel> - The channel name that the events will be emitted to. | ||
| 163 | |||
| 164 | =item * C<array> - A perl arrayref that its content is going to be emitted as events. | ||
| 165 | |||
| 166 | =back | ||
| 167 | |||
| 168 | =cut | ||
| 169 | |||
| 170 | method add_subscription ($channel, %args) { | ||
| 171 | if (my $data = $args{array}) { | ||
| 172 | my $batch = async sub { | ||
| 173 | while (my @next = splice($data->@*, 0, 5)) { | ||
| 174 | return \@next; | ||
| 175 | } | ||
| 176 | }; | ||
| 177 | |||
| 178 | Myriad::Service::Attributes->apply_attributes( | ||
| 179 | class => $meta_service->name, | ||
| 180 | code => Sub::Util::set_subname($channel, $batch), | ||
| 181 | attributes => ['Batch'], | ||
| 182 | ); | ||
| 183 | |||
| 184 | $meta_service->add_method("batch_$channel", $batch); | ||
| 185 | |||
| 186 | $self | ||
| 187 | } else { | ||
| 188 | die 'only simple arrays are supported at the moment'; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | =head2 add_receiver | ||
| 193 | |||
| 194 | Adds a new receiver in the given service. | ||
| 195 | |||
| 196 | =over 4 | ||
| 197 | |||
| 198 | =item * C<from> - The source service name. | ||
| 199 | |||
| 200 | =item * C<channel> - The source of the events channel name. | ||
| 201 | |||
| 202 | =item * C<handler> - A coderef that will handle the events. | ||
| 203 | |||
| 204 | =back | ||
| 205 | |||
| 206 | =cut | ||
| 207 | |||
| 208 | method add_receiver ($from, $channel, $handler) { | ||
| 209 | my $receiver = async sub { | ||
| 210 | my ($self, $src) = @_; | ||
| 211 | await $src->each($handler)->completed; | ||
| 212 | }; | ||
| 213 | |||
| 214 | Myriad::Service::Attributes->apply_attributes( | ||
| 215 | class => $meta_service->name, | ||
| 216 | code => Sub::Util::set_subname("receiver_$channel", $receiver), | ||
| 217 | attributes => ["Receiver(from => '$from', channel => '$channel')"] | ||
| 218 | ); | ||
| 219 | |||
| 220 | $meta_service->add_method("receiver_$channel", $receiver); | ||
| 221 | |||
| 222 | $self; | ||
| 223 | } | ||
| 224 | |||
| 225 | 1; | ||
| 226 | |||
| 227 | __END__ | ||