lib/Test/Myriad.pm

filter: Statementstmt 91.6% 22 / 24 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.7% 30 / 32 CC 1 SCAR 0.4
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Myriad/Role/RPC.pm ↑ Summary lib/Myriad/Exception/Base.pm »
1 package Test::Myriad;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶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 3 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Future::Utils qw(fmap0);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Check::UnitCheck;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Object::Pad qw(:experimental);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 0 ▶use Myriad;
Subroutine BEGIN: called CC=1 SCAR=2.6
16 use Myriad::Service::Implementation;
17 use Test::Myriad::Service;
18  
19 use Log::Any::Adapter;
20  
21 our @REGISTERED_SERVICES;
22  
23 my $loop = IO::Async::Loop->new();
24 my $myriad = Myriad->new();
25  
26 =head1 NAME
27  
28 Myriad::Test - a collection of helpers to test microservices.
29  
30 =head1 SYNOPSIS
31  
32 use Test::Myriad;
33  
34 my $mock_service = add_service(name => 'mocked_service');
35  
36 =head1 DESCRIPTION
37  
38 A mini utility to help developers testing myriad services.
39  
40 it can create completely fake services or mock already
41 existing ones.
42  
43 It uses the L<Myriad::Transport::Memory> by default to change that
44 you can set the environment variable MYRIAD_TEST_TRANSPORT
45  
46 =head1 Methods
47  
48 =head2 add_service
49  
50 Adds a service to the test environment the service can be
51 an already existing service or totally a new mocked one.
52  
53 it takes one of the following params:
54  
55 =over 4
56  
57 =item * C<service> - A package name for an existing service.
58  
59 =item * C<name> - A Perl package name that will hold the new mocked service.
60  
61 =back
62  
63 =cut
64  
65 sub add_service {
66 my ($self, %args) = @_;
67 my ($pkg, $meta);
68 if (my $service = delete $args{service}) {
69 $pkg = $service;
70 $meta = Object::Pad::MOP::Class->for_class($service);
71 } elsif ($service = delete $args{name}) {
72 die 'The name should look like a Perl package name' unless $service =~ /::/;
73 $pkg = $service;
74 $meta = Object::Pad::MOP::Class->begin_class($pkg, extends => 'Myriad::Service::Implementation');
75  
76 {
77 no strict 'refs';
78 push @{$pkg . '::ISA' }, 'Myriad::Service';
79 $Myriad::Service::SLOT{$pkg} = {
80 map { $_ => $meta->add_field('$' . $_) } qw(api)
81 };
82 }
83 }
84  
85 push @REGISTERED_SERVICES, $pkg;
86  
87 return Test::Myriad::Service->new(meta => $meta, pkg => $pkg, myriad => $myriad);
88 }
89  
90 =head2 ready
91  
92 Returns a L<Future> indicate that test env
93 is ready to be used.
94  
95 at the moment it is just a shortcut for L<Myriad> run_future.
96  
97 =cut
98  
99 sub ready {
100 return $myriad->run_future;
101 }
102  
103 =head2 instance
104  
105 Returns the L<Myriad> instance we are using
106  
107 =cut
108  
109 sub instance {
110 return $myriad;
111 }
112  
113 sub import {
114 my $self = shift;;
115 Check::UnitCheck::unitcheckify(sub {
116 $myriad->configure_from_argv(('--transport', $ENV{MYRIAD_TEST_TRANSPORT} // 'memory', 'service'))->get();
117  
118 # Override logger
119 Log::Any::Adapter->set('TAP', filter => 'info');
120  
121 $loop->later(sub {
122 (fmap0 {
123 $myriad->add_service($_);
124 } foreach => [@REGISTERED_SERVICES])->then(sub {
125 return $myriad->run;
126 })->on_fail(sub {
127 my $error = shift;
128 die "Failed to start the test environment due: $error";
129 })->retain;
130 });
131 });
132 }
133  
134 1;
135  
136 __END__
« lib/Myriad/Role/RPC.pm ↑ Summary lib/Myriad/Exception/Base.pm »