Test/Statsd.pm

filter: Statementstmt 94.0% 79 / 84 Branchbran 66.6% 8 / 12 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 94.7% 18 / 19 Podpod 0.0% 0 / 6 totaltotal 81.8% 108 / 132 CC 11 SCAR 7.3
CC
11
Cov
87%
stop_statsd
3.4
new
3.2
start_statsd
3.0
CRAP 2.1
« Net/Statsd/Server/Backend/Test.pm ↑ Summary Net/Statsd/Server/Backend.pm »
1 package Test::Statsd;
2 14 $Test::Statsd::VERSION = '0.20';
3 14 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 14 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use AnyEvent::Strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use AnyEvent::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use IO::Socket::INET ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 14 ▶sub new {
Subroutine new: called CC=3 SCAR=11.8
Pod undocumented
15 14 my ($class, $opt) = @_;
16 14 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0140
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
17 14 ▶ $opt ||= {};
Condition50%1/2
$opt ||= {}
l!l
140
MC/DC0%0/1
$opt ||= {}
$opt
Truth table50%1/2
A
A= $opt
Aresult
11
00
18 my $self = {
19 binary => $opt->{binary},
20 config => $opt->{config},
21 14 _statsd_pid => undef,
22 };
23 14 bless $self, $class;
24 }
25  
26 # A read callback (read_cb) can optionally be used in special
27 # cases when you don't want the TCP server to be shut down
28 # when the first flush data is received (see delete-idle-stats test
29 # for an example).
30  
31 7 ▶sub wait_and_collect_flush_data {
Subroutine wait_and_collect_flush_data: called CC=2 SCAR=7.0
Pod undocumented
32 7 my ($self, $port, $read_cb) = @_;
33  
34 7 $self->{_flush_data} = "";
35  
36 # Pretend to be a carbon/graphite daemon
37 7 ▶ $port ||= 40003;
Condition50%1/2
$port ||= 40003
l!l
07
MC/DC0%0/1
$port ||= 40003
$port
Truth table50%1/2
A
A= $port
Aresult
11
00
38  
39 7 my $srv;
40 7 my $cv = AE::cv;
41  
42 9 ▶ $srv = tcp_server undef, $port, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
43 9 my ($fh, $host, $port) = @_;
44 9 my $hdl;
45 $hdl = AnyEvent::Handle->new(
46 fh => $fh,
47 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 0 warn "Socket error: $!\n";
49 0 $_[0]->destroy
50 },
51 9 ▶ on_read => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
52 9 my ($ae_handle) = @_;
53 # Store graphite data into a private object member
54 9 $self->{_flush_data} .= $ae_handle->rbuf;
55 9 ▶ if ($read_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read_cb) { } 4 5 9
56 4 $read_cb->($hdl, $cv, $self->{_flush_data});
57 # We need to clear the received data now, or our
58 # reader will be surprised receiving the old + new
59 # buffer in the n > 1 round.
60 4 $self->{_flush_data} = "";
61 4 $ae_handle->{rbuf} = "";
62 } else {
63 # Calling send() on the condvar stops the TCP server
64 5 $cv->send();
65 }
66 },
67 2 ▶ on_eof => sub { $hdl->destroy },
Subroutine __ANON__: called CC=1 SCAR=0.0
68 9 );
69 7 };
70 7 $cv->recv();
71 7 return $self->{_flush_data};
72 }
73  
74 9 ▶sub hashify {
Subroutine hashify: called CC=3 SCAR=11.0
Pod undocumented
75 9 my ($self, $str) = @_;
76 9 my @lines = split m{\r?\n}, $str;
77 9 my $stats;
78 9 for (@lines) {
79 73 $_ =~ s{^ \s* (\S*) \s* $}{$1}x;
80 73 ▶ next unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 73 73
81 73 my ($key, $val, $ts) = split;
82 73 $stats->{$key} = $val;
83 }
84 9 return $stats;
85 }
86  
87 14 ▶sub start_statsd {
Subroutine start_statsd: called CC=3 SCAR=11.1
Pod undocumented
88 14 my ($self) = @_;
89  
90 14 my $pid = fork;
91 14 ▶ if (! defined $pid) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $pid) { } 0 14 14
elsif ($pid == 0) { } 7 7 14
92 0 die "Fork failed: $! Aborting.";
93 }
94  
95 # Child
96 elsif ($pid == 0) {
97 7 my @binary = split " ", $self->{binary};
98 7 my $config = $self->{config};
99 7 exec @binary, $config, '2>&1 1>/dev/null';
100 }
101  
102 # Parent
103 else {
104 7 $self->{_statsd_pid} = $pid;
105 # Allow for child statsd to start up
106 7 Time::HiRes::usleep(500_000);
107 }
108 }
109  
110 7 ▶sub stop_statsd {
Subroutine stop_statsd: called CC=3 SCAR=12.3
Pod undocumented
111 7 my ($self) = @_;
112  
113 7 my $pid = $self->{_statsd_pid};
114 7 ▶ if (! $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 7 7
115 0 die "Statsd was never started?";
116 }
117  
118 7 ▶ if (! kill(15, $pid)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (kill 15, $pid) 0 7 7
119 0 die "Failed to stop statsd (pid: $pid). "
120 . "Please do something manually ($!)";
121 }
122  
123 7 return 1;
124 }
125  
126 106 ▶sub send_udp {
Subroutine send_udp: called CC=1 SCAR=0.0
Pod undocumented
127 106 my ($self, $host, $port, $payload) = @_;
128  
129 106 my $sock = IO::Socket::INET->new(
130 Proto => 'udp',
131 PeerAddr => $host,
132 PeerPort => $port,
133 );
134  
135 106 my $len = $sock->send($payload);
136 106 $sock->close();
137  
138 106 return $len == length($payload);
139 }
140  
141 14 1;
142  
143 =pod
144  
145 =head1 NAME
146  
147 Test::Statsd - Test harness for any statsd server daemon
148  
149 =head1 DESCRIPTION
150  
151 Embeds the logic to perform integration tests of any statsd
152 daemon that can be launched from the command line.
153  
154 Usage:
155  
156 my $t = Test::Statsd->new({
157 binary => './bin/statsd',
158 config => './bin/sample-config.json'
159 });
160  
161 # Brings up the statsd server in the background
162 # with the specified configuration, and stores its pid
163 $t->start_statsd();
164  
165
166 =cut
« Net/Statsd/Server/Backend/Test.pm ↑ Summary Net/Statsd/Server/Backend.pm »