Elasticsearch/TestServer.pm

filter: Statementstmt 29.4% 35 / 119 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 36.3% 8 / 22 Podpod 0.0% 0 / 5 totaltotal 23.2% 43 / 185 CC 23 SCAR 32.4
CC
23
Cov
22%
_start_node
90.0
start
20.0
_check_nodes
20.0
CRAP 25.5
« Elasticsearch/Cxn/Hijk.pm ↑ Summary Elasticsearch/Role/API.pm »
1 package Search::Elasticsearch::TestServer;
2 2 $Search::Elasticsearch::TestServer::VERSION = '6.80';
3 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Search::Elasticsearch();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use POSIX 'setsid';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use File::Temp();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use IO::Socket();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Search::Elasticsearch::Util qw(parse_params throw);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 has 'es_home' => ( is => 'ro', default => $ENV{ES_HOME} );
14 2 has 'es_version' => ( is => 'ro', default => $ENV{ES_VERSION} );
15 2 has 'instances' => ( is => 'ro', default => 1 );
16 2 has 'http_port' => ( is => 'ro', default => 9600 );
17 2 has 'es_port' => ( is => 'ro', default => 9700 );
18 has 'pids' => (
19 is => 'ro',
20 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 2 clearer => 1,
22 predicate => 1
23 );
24  
25 0 ▶has 'dirs' => ( is => 'ro', default => sub { [] } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
26 0 ▶has 'conf' => ( is => 'ro', default => sub { [] } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
27 2 has '_starter_pid' => ( is => 'rw', required => 0, predicate => 1 );
28  
29 #===================================
30 sub start {
31 #===================================
32 0 ▶ my $self = shift;
Subroutine start: not called CC=4 SCAR=30.0
Pod undocumented
33  
34 0 ▶ my $home = $self->es_home
Branch: 1 branch
BranchTrueFalseTotal
my $home = $self->es_home or throw("Param", "Missing required param <es_home>") 0 0 0
35 or throw( 'Param', "Missing required param <es_home>" );
36 0 ▶ $self->es_version
Branch: 1 branch
BranchTrueFalseTotal
$self->es_version or throw("Param", "Missing required param <es_version>") 0 0 0
37 or throw( 'Param', "Missing required param <es_version>" );
38  
39 0 my $instances = $self->instances;
40 0 my $port = $self->http_port;
41 0 my $es_port = $self->es_port;
42 0 my @http = map { $port++ } ( 1 .. $instances );
43 0 my @transport = map { $es_port++ } ( 1 .. $instances );
44  
45 0 $self->_check_ports( @http, @transport );
46  
47 0 my $old_SIGINT = $SIG{INT};
48 $SIG{INT} = sub {
49 0 ▶ $self->shutdown;
Subroutine __ANON__: not called CC=2 SCAR=17.9
50 0 ▶ if ( ref $old_SIGINT eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $old_SIGINT eq "CODE") 0 0 0
51 0 return $old_SIGINT->();
52 }
53 0 exit(1);
54 0 };
55  
56 0 for ( 0 .. $instances - 1 ) {
57 0 my $dir = File::Temp->newdir();
58 0 push @{ $self->dirs }, $dir;
59 0 print "Starting node: http://127.0.0.1:$http[$_]\n";
60 0 $self->_start_node( $dir, $transport[$_], $http[$_] );
61 }
62  
63 0 $self->_check_nodes(@http);
64 0 return [ map {"http://127.0.0.1:$_"} @http ];
65 }
66  
67 #===================================
68 sub _check_ports {
69 #===================================
70 0 ▶ my $self = shift;
Subroutine _check_ports: not called CC=3 SCAR=24.8
71 0 for my $port (@_) {
72 0 ▶ next unless IO::Socket::INET->new("127.0.0.1:$port");
Branch: 1 branch
BranchTrueFalseTotal
unless "IO::Socket::INET"->new("127.0.0.1:$port") 0 0 0
73 0 throw( 'Param',
74 "There is already a service running on 127.0.0.1:$port. "
75 . "Please shut it down before starting the test server" );
76 }
77 }
78  
79 #===================================
80 sub _check_nodes {
81 #===================================
82 0 ▶ my $self = shift;
Subroutine _check_nodes: not called CC=4 SCAR=30.0
83 0 my $http = HTTP::Tiny->new;
84 0 for my $node (@_) {
85 0 print "Checking node: http://127.0.0.1:$node\n";
86 0 my $i = 20;
87 0 while (1) {
88 last
89 0 ▶ if $http->head("http://127.0.0.1:$node/")->{status} == 200;
Branch: 1 branch
BranchTrueFalseTotal
if $http->head("http://127.0.0.1:$node/")->{"status"} == 200 0 0 0
90 0 ▶ throw( 'Cxn', "Couldn't connect to http://127.0.0.1:$node" )
Branch: 1 branch
BranchTrueFalseTotal
unless $i-- 0 0 0
91 unless $i--;
92 0 sleep 1;
93 }
94  
95 }
96 }
97  
98 #===================================
99 sub _start_node {
100 #===================================
101 0 ▶ my ( $self, $dir, $transport, $http ) = @_;
Subroutine _start_node: not called CC=9 SCAR=45.0
102  
103 0 my $pid_file = File::Temp->new;
104 0 my @config = $self->_command_line( $pid_file, $dir, $transport, $http );
105  
106 0 my $int_caught = 0;
107 {
108 0 ▶ local $SIG{INT} = sub { $int_caught++; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
109 0 ▶ defined( my $pid = fork )
Branch: 1 branch
BranchTrueFalseTotal
defined(my $pid = fork) or throw("Internal", "Couldn't fork a new process: $!") 0 0 0
110 or throw( 'Internal', "Couldn't fork a new process: $!" );
111 0 ▶ if ( $pid == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) { } 0 0 0
112 0 ▶ throw( 'Internal', "Can't start a new session: $!" )
Branch: 1 branch
BranchTrueFalseTotal
if setsid() == -1 0 0 0
113 if setsid == -1;
114 0 ▶ exec(@config) or die "Couldn't execute @config: $!";
Branch: 1 branch
BranchTrueFalseTotal
exec @config or die "Couldn't execute @{config}: $!" 0 0 0
115 }
116 else {
117 0 for ( 1 .. 5 ) {
118 0 ▶ last if -s $pid_file->filename();
Branch: 1 branch
BranchTrueFalseTotal
if -s $pid_file->filename 0 0 0
119 0 sleep 1;
120 }
121 0 open my $pid_fh, '<', $pid_file->filename;
122 0 my $pid = <$pid_fh>;
123 0 ▶ throw( 'Internal', "No PID file found for Elasticsearch" )
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 0 0
124 unless $pid;
125 0 chomp $pid;
126 0 push @{ $self->{pids} }, $pid;
127 0 $self->_starter_pid($$);
128 }
129 }
130 0 ▶ $SIG{INT}->('INT') if $int_caught;
Branch: 1 branch
BranchTrueFalseTotal
if $int_caught 0 0 0
131 }
132  
133 #===================================
134 sub guarded_shutdown {
135 #===================================
136 0 ▶ my $self = shift;
Subroutine guarded_shutdown: not called CC=3 SCAR=24.8
Pod undocumented
137 0 ▶ if ( $self->_has_starter_pid && $$ == $self->_starter_pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_starter_pid and $$ == $self->_starter_pid) 0 0 0
Condition0%0/3
$self->_has_starter_pid and $$ == $self->_starter_pid
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_has_starter_pid and $$ == $self->_starter_pid
$self->_has_starter_pid $$ == $self->_starter_pid
Truth table0%0/3
A and B
A= $self->_has_starter_pidB= $$ == $self->_starter_pid
ABresult
0X0
100
111
138 0 $self->shutdown();
139 }
140 }
141  
142 #===================================
143 sub shutdown {
144 #===================================
145 0 ▶ my $self = shift;
Subroutine shutdown: not called CC=3 SCAR=24.8
Pod undocumented
146 0 local $?;
147  
148 0 ▶ return unless $self->has_pids;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_pids 0 0 0
149  
150 0 my $pids = $self->pids;
151 0 $self->clear_pids;
152 0 ▶ return unless @$pids;
Branch: 1 branch
BranchTrueFalseTotal
unless @$pids 0 0 0
153  
154 0 kill 9, @$pids;
155 0 $self->clear_dirs;
156 }
157  
158 #===================================
159 sub _command_line {
160 #===================================
161 0 ▶ my ( $self, $pid_file, $dir, $transport, $http ) = @_;
Subroutine _command_line: not called CC=2 SCAR=17.9
162  
163 0 my $version = $self->es_version;
164 0 my $class = "Search::Elasticsearch::Client::${version}::TestServer";
165 0 ▶ eval "require $class" || die $@;
Branch: 1 branch
BranchTrueFalseTotal
eval "require $class" or die $@ 0 0 0
166  
167 0 return $class->command_line(@_);
168 }
169  
170 #===================================
171 sub clear_dirs {
172 #===================================
173 0 ▶ my $self = shift;
Subroutine clear_dirs: not called CC=1 SCAR=6.9
Pod undocumented
174 0 @{ $self->dirs() } = ();
175 }
176  
177 #===================================
178 0 ▶sub DEMOLISH { shift->guarded_shutdown }
Subroutine DEMOLISH: not called CC=1 SCAR=6.9
Pod undocumented
179 #===================================
180  
181 2 1;
182  
183 # ABSTRACT: A helper class to launch Elasticsearch nodes
184  
185 __END__
« Elasticsearch/Cxn/Hijk.pm ↑ Summary Elasticsearch/Role/API.pm »