blib/lib/IPC/Manager/Client/LocalMemory.pm

filter: Statementstmt 93.6% 117 / 125 Branchbran 78.5% 22 / 28 Conditioncond 42.8% 6 / 14 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 24 / 24 Podpod 73.3% 11 / 15 totaltotal 84.5% 181 / 214 CC 22 SCAR 10.5
CC
22
Cov
87%
init
7.6
ready_messages
3.1
peer_exists
3.0
CRAP 2.9
« blib/lib/IPC/Manager/Role/Service/Requests.pm ↑ Summary blib/lib/IPC/Manager/Role/Client/Connection.pm »
1 package IPC::Manager::Client::LocalMemory;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 our $VERSION = '0.000036';
6  
7 5 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 ▶use parent 'IPC::Manager::Client';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Object::HashBase;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use IPC::Manager::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # Global in-memory store keyed by route.
15 # Each route contains:
16 # clients => { id => { pid => $$, messages => [] } } -- active clients
17 # stats => { id => { read => {}, sent => {} } } -- persisted after disconnect
18 5 my %STORES;
19  
20 1 ▶sub _viable { 1 }
Subroutine _viable: called CC=1 SCAR=0.0
21  
22 sub spawn {
23 48 ▶ my $class = shift;
Subroutine spawn: called CC=1 SCAR=0.0
Pod documented
24 48 my %params = @_;
25  
26 48 my $route = "localmemory-" . ++$IPC::Manager::Client::LocalMemory::_COUNTER;
27  
28 48 $STORES{$route} = { clients => {}, stats => {} };
29  
30 48 return $route;
31 }
32  
33 sub unspawn {
34 48 ▶ my $class = shift;
Subroutine unspawn: called CC=1 SCAR=0.0
Pod documented
35 48 my ($route) = @_;
36 48 delete $STORES{$route};
37 }
38  
39 sub peer_exists_in_store {
40 6 ▶ my $class = shift;
Subroutine peer_exists_in_store: called CC=2 SCAR=6.9
Pod undocumented
41 6 my ($route) = @_;
42 6 ▶ return exists $STORES{$route} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $STORES{$route} ? : 3 3 6
43 }
44  
45 sub _store {
46 348 ▶ my $self = shift;
Subroutine _store: called CC=2 SCAR=8.1
47 348 ▶ return $STORES{$self->{route}} // croak "Route '$self->{route}' does not exist";
Condition33%1/3
$STORES{$self->{"route"}} // croak(qq[Route '$self->{"route"}' does not exist])
l!l&&r!l&&!r
34800
MC/DC0%0/2
$STORES{$self->{"route"}} // croak(qq[Route '$self->{"route"}' does not exist])
$STORES{$self->{"route"}} croak(qq[Route '$self->{"route"}' does not exist])
Truth table33%1/3
A // B
A= $STORES{$self->{"route"}}B= croak(qq[Route '$self->{"route"}' does not exist])
ABresult
1X1
011
000
48 }
49  
50 sub _client_data {
51 88 ▶ my $self = shift;
Subroutine _client_data: called CC=2 SCAR=7.6
52 88 my $store = $self->_store;
53 88 ▶ return $store->{clients}{$self->{id}} // croak "Client '$self->{id}' not registered";
Condition33%1/3
$store->{"clients"}{$self->{"id"}} // croak(qq[Client '$self->{"id"}' not registered])
l!l&&r!l&&!r
8800
MC/DC0%0/2
$store->{"clients"}{$self->{"id"}} // croak(qq[Client '$self->{"id"}' not registered])
$store->{"clients"}{$self->{"id"}} croak(qq[Client '$self->{"id"}' not registered])
Truth table33%1/3
A // B
A= $store->{"clients"}{$self->{"id"}}B= croak(qq[Client '$self->{"id"}' not registered])
ABresult
1X1
011
000
54 }
55  
56 sub init {
57 74 ▶ my $self = shift;
Subroutine init: called CC=7 SCAR=20.2
Pod undocumented
58  
59 74 $self->SUPER::init();
60  
61 70 my $store = $self->_store;
62 70 my $id = $self->{id};
63  
64 70 ▶ if ($self->{reconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'reconnect'}) { } 2 68 70
65 2 ▶ unless ($store->{clients}{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($store->{"clients"}{$id}) 1 1 2
66 1 $self->{disconnected} = 1;
67 1 croak "Client '$id' does not exist";
68 }
69 1 my $data = $store->{clients}{$id};
70 1 ▶ if ($data->{pid} && $data->{pid} != $$ && $self->pid_is_running($data->{pid})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"pid"} and $data->{"pid"} != $$ and $self->pid_is_running($data->{"pid"})) 0 1 1
Condition33%2/6
$data->{"pid"} and $data->{"pid"} != $$ and $self->pid_is_running($data->{"pid"})
!ll&&!rl&&r
100
$data->{"pid"} and $data->{"pid"} != $$
!ll&&!rl&&r
010
MC/DC0%0/3
$data->{"pid"} and $data->{"pid"} != $$ and $self->pid_is_running($data->{"pid"})
$data->{"pid"} $data->{"pid"} != $$ $self->pid_is_running($data->{"pid"})
Truth table25%1/4
A and B and C
A= $data->{"pid"}B= $data->{"pid"} != $$C= $self->pid_is_running($data->{"pid"})
ABCresult
0XX0
10X0
1100
1111
71 0 $self->{disconnected} = 1;
72 0 croak "Connection already running in pid $data->{pid}";
73 }
74 }
75 else {
76 68 ▶ if ($store->{clients}{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($store->{"clients"}{$id}) 1 67 68
77 1 $self->{disconnected} = 1;
78 1 croak "Client '$id' already exists";
79 }
80 67 $store->{clients}{$id} = {
81 pid => $$,
82 messages => [],
83 };
84 }
85  
86 68 $store->{clients}{$id}{pid} = $$;
87 }
88  
89 66 ▶sub pending_messages { 0 }
Subroutine pending_messages: called CC=1 SCAR=0.0
Pod undocumented
90  
91 sub ready_messages {
92 68 ▶ my $self = shift;
Subroutine ready_messages: called CC=3 SCAR=11.3
Pod undocumented
93 68 my $data;
94 68 ▶ unless (eval { $data = $self->_client_data; 1 }) { warn $@; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $data = $self->_client_data; 1 } }) 0 68 68
95 68 ▶ return @{$data->{messages}} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@{$data->{'messages'};} ? : 1 67 68
96 }
97  
98 sub get_messages {
99 20 ▶ my $self = shift;
Subroutine get_messages: called CC=2 SCAR=6.9
Pod documented
100 20 $self->pid_check;
101 20 my $data = $self->_client_data;
102 20 my @raw = splice @{$data->{messages}};
103 20 my @out;
104 20 for my $msg (@raw) {
105 22 $self->{stats}{read}{$msg->{from}}++;
106 22 push @out, $msg;
107 }
108 20 return $self->sort_messages(@out);
109 }
110  
111 sub send_message {
112 28 ▶ my $self = shift;
Subroutine send_message: called CC=3 SCAR=11.0
Pod documented
113 28 my $msg = $self->build_message(@_);
114 28 $self->pid_check;
115  
116 28 ▶ my $peer_id = $msg->to or croak "Message has no peer";
Branch: 1 branch
BranchTrueFalseTotal
my $peer_id = $msg->to or croak("Message has no peer") 0 28 28
117 28 my $store = $self->_store;
118  
119 croak "Client '$peer_id' does not exist"
120 28 ▶ unless $store->{clients}{$peer_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $store->{"clients"}{$peer_id} 4 24 28
121  
122 24 push @{$store->{clients}{$peer_id}{messages}}, $msg;
123 24 $self->{stats}{sent}{$peer_id}++;
124 }
125  
126 sub peers {
127 8 ▶ my $self = shift;
Subroutine peers: called CC=1 SCAR=0.0
Pod documented
128 8 my $store = $self->_store;
129 8 return sort grep { $_ ne $self->{id} } keys %{$store->{clients}};
130 }
131  
132 sub peer_exists {
133 4 ▶ my $self = shift;
Subroutine peer_exists: called CC=3 SCAR=11.0
Pod documented
134 4 my ($peer_id) = @_;
135 4 ▶ croak "'peer_id' is required" unless $peer_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $peer_id 0 4 4
136 4 my $store = $self->_store;
137 4 ▶ return $store->{clients}{$peer_id} ? 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$store->{'clients'}{$peer_id} ? : 2 2 4
138 }
139  
140 sub peer_pid {
141 4 ▶ my $self = shift;
Subroutine peer_pid: called CC=2 SCAR=6.9
Pod documented
142 4 my ($peer_id) = @_;
143 4 my $store = $self->_store;
144 4 ▶ my $data = $store->{clients}{$peer_id} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $data = $store->{"clients"}{$peer_id} or return undef 2 2 4
145 2 return $data->{pid};
146 }
147  
148 sub write_stats {
149 72 ▶ my $self = shift;
Subroutine write_stats: called CC=2 SCAR=7.5
Pod documented
150 72 my $store;
151 72 ▶ unless (eval { $store = $self->_store; 1 }) { warn $@; return }
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $store = $self->_store; 1 } }) 0 72 72
152 72 $store->{stats}{$self->{id}} = $self->{stats};
153 }
154  
155 sub read_stats {
156 4 ▶ my $self = shift;
Subroutine read_stats: called CC=2 SCAR=6.9
Pod documented
157 4 my $store = $self->_store;
158 4 ▶ return $store->{stats}{$self->{id}} // {read => {}, sent => {}};
Condition100%2/2
$store->{"stats"}{$self->{"id"}} // {"read", {}, "sent", {}}
l!l
31
MC/DC100%1/1
$store->{"stats"}{$self->{"id"}} // {"read", {}, "sent", {}}
$store->{"stats"}{$self->{"id"}}
Truth table100%2/2
A
A= $store->{"stats"}{$self->{"id"}}
Aresult
11
00
159 }
160  
161 sub all_stats {
162 5 ▶ my $self = shift;
Subroutine all_stats: called CC=2 SCAR=6.9
Pod documented
163 5 my $store = $self->_store;
164 5 my %out;
165 5 for my $id (keys %{$store->{stats}}) {
166 10 $out{$id} = $store->{stats}{$id};
167 }
168 5 return \%out;
169 }
170  
171 sub post_disconnect_hook {
172 65 ▶ my $self = shift;
Subroutine post_disconnect_hook: called CC=2 SCAR=7.5
Pod documented
173 65 my $store;
174 65 ▶ unless (eval { $store = $self->_store; 1 }) { warn $@; return }
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $store = $self->_store; 1 } }) 0 65 65
175 65 delete $store->{clients}{$self->{id}};
176 }
177  
178 5 1;
179  
180 __END__
« blib/lib/IPC/Manager/Role/Service/Requests.pm ↑ Summary blib/lib/IPC/Manager/Role/Client/Connection.pm »