blib/lib/Lemonldap/NG/Common/MessageBroker/NoBroker.pm untested

filter: Statementstmt 0.0% 0 / 33 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 75 CC 7 SCAR 23.5
CC
7
Cov
0%
publish
20.0
getNextMessage
12.0
waitForNextMessage
6.0
CRAP 10.5
1 package Lemonldap::NG::Common::MessageBroker::NoBroker;
2  
3 # This pseudo message broker dispatches messages into current node only.
4 # It also inserts periodically "newConf" into llng_events to permit to
5 # detect configuration changes using the checkTime parameter.
6  
7 use strict;
8 use Lemonldap::NG::Common::Conf::Constants;
9  
10 our $VERSION = '2.23.0';
11 our $lastCheck = time;
12 our $channels = {};
13  
14 sub new {
15 my ( $class, $conf, $logger ) = @_;
16 $channels->{ $conf->{eventQueueName} } //= [];
17 $channels->{ $conf->{statusQueueName} } = 'no';
18 return bless {
19 checkTime => $conf->{checkTime},
20 eventQueueName => $conf->{eventQueueName},
21 logger => $logger,
22 }, $class;
23 }
24  
25 sub publish {
26 my ( $self, $channel, $msg ) = @_;
27 die 'eventStatus not authorized without broker'
28 if $channels->{$channel} eq 'no' and $ENV{FORCE_STATUS} ne 'force status';
29 die unless $channel and $msg;
30 $channels->{$channel} = []
31 unless ref( $channels->{$channel} );
32 push @{ $channels->{$channel} }, $msg;
33 }
34  
35 sub subscribe { }
36  
37 sub getNextMessage {
38 my ( $self, $channel, $delay ) = @_;
39 if ( time >= $lastCheck + $self->{checkTime} ) {
40 $self->publish( $self->{eventQueueName}, { action => 'newConf' } );
41 $lastCheck = time;
42 }
43 if ( ref( $channels->{$channel} )
44 and @{ $channels->{$channel} } )
45 {
46 return shift @{ $channels->{$channel} };
47 }
48 }
49  
50 sub waitForNextMessage {
51 my ( $self, $channel ) = @_;
52 while (1) {
53 if ( my $msg = $self->getNextMessage($channel) ) {
54 return $msg;
55 }
56 sleep 1;
57 }
58 }
59  
60 1;