Test/Statsd.pm

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