File Coverage

blib/lib/Net/Stomp/MooseHelpers/TraceOnly.pm
Criterion Covered Total %
statement 28 33 84.8
branch 2 4 50.0
condition 1 3 33.3
subroutine 10 13 76.9
pod 0 8 0.0
total 41 61 67.2


line stmt bran cond sub pod time code
1             package Net::Stomp::MooseHelpers::TraceOnly;
2             $Net::Stomp::MooseHelpers::TraceOnly::VERSION = '2.9';
3             {
4             $Net::Stomp::MooseHelpers::TraceOnly::DIST = 'Net-Stomp-MooseHelpers';
5             }
6 1     1   1596 use Moose::Role;
  1         3  
  1         10  
7 1     1   4180 use Net::Stomp::Frame;
  1         2  
  1         14  
8 1     1   22 use namespace::autoclean;
  1         2  
  1         8  
9              
10             # ABSTRACT: role to replace the Net::Stomp connection with tracing code
11              
12             with 'Net::Stomp::MooseHelpers::TracerRole';
13              
14              
15             has trace => (
16             is => 'ro',
17             isa => 'Bool',
18             default => 1,
19             );
20              
21             around '_build_connection' => sub {
22             my ($orig,$self,@etc) = @_;
23              
24             my $conn = Net::Stomp::MooseHelpers::TraceOnly::Connection->new({
25             _tracing_object => $self,
26             });
27             return $conn;
28             };
29              
30             package Net::Stomp::MooseHelpers::TraceOnly::Connection;
31             $Net::Stomp::MooseHelpers::TraceOnly::Connection::VERSION = '2.9';
32             {
33             $Net::Stomp::MooseHelpers::TraceOnly::Connection::DIST = 'Net-Stomp-MooseHelpers';
34             }{
35 1     1   173 use Moose;
  1         2  
  1         7  
36 1     1   4620 use Carp;
  1         2  
  1         436  
37             require Net::Stomp;
38              
39             has _tracing_object => ( is => 'rw' );
40              
41             sub connect {
42 1     1 0 2 my ($self) = @_;
43 1         38 $self->session_id("$self-$$");
44 1         36 return Net::Stomp::Frame->new({
45             command => 'CONNECTED',
46             headers => {
47             session => $self->session_id,
48             },
49             body => '',
50             });
51             }
52 0     0 0 0 sub subscribe { return 1 }
53 0     0 0 0 sub unsubscribe { return 1 }
54 0     0 0 0 sub ack { return 1 }
55 1     1 0 4 sub current_host { return 0 }
56 2     2 0 20 sub receipt_timeout { return undef }
57              
58             has _last_frame => (
59             is => 'rw',
60             );
61              
62             sub receive_frame {
63 2     2 0 16 my ($self) = @_;
64              
65             # hack to make send_with_receipt happy
66 2 50 33     81 if ($self->_last_frame && $self->_last_frame->headers->{'receipt'}) {
67 2         71 return Net::Stomp::Frame->new({
68             command => 'RECEIPT',
69             headers => {
70             'receipt-id' => $self->_last_frame->headers->{'receipt'},
71             },
72             body => '',
73             });
74 0         0 $self->_last_frame(undef);
75             }
76 0         0 croak "This a Net::Stomp::MooseHelpers::TraceOnly::Connection, we don't talk to the network";
77             }
78              
79             sub send_frame {
80 7     7 0 266 my ($self,$frame,@etc) = @_;
81              
82 7         242 $self->_last_frame($frame);
83              
84 7 50       227 if (my $o=$self->_tracing_object) {
85 7         27 $o->_save_frame($frame,'send');
86             }
87              
88 7         31 return;
89             };
90              
91             has serial => (
92             isa => 'Int',
93             is => 'rw',
94             default => 0,
95             );
96             has session_id => (
97             isa => 'Str',
98             is => 'rw',
99             );
100              
101             # let's just take the original methods, they'll work
102             *send = \&Net::Stomp::send;
103             *send_transactional = \&Net::Stomp::send_transactional;
104             *send_with_receipt = \&Net::Stomp::send_with_receipt;
105             *_get_next_transaction = \&Net::Stomp::_get_next_transaction;
106              
107             __PACKAGE__->meta->make_immutable;
108             }
109              
110             __END__
111              
112             =pod
113              
114             =encoding UTF-8
115              
116             =head1 NAME
117              
118             Net::Stomp::MooseHelpers::TraceOnly - role to replace the Net::Stomp connection with tracing code
119              
120             =head1 VERSION
121              
122             version 2.9
123              
124             =head1 SYNOPSIS
125              
126             package MyThing;
127             use Moose;with 'Net::Stomp::MooseHelpers::CanConnect';
128             with 'Net::Stomp::MooseHelpers::TraceOnly';
129              
130             $self->trace_basedir('/tmp/stomp_dumpdir');
131              
132             B<NOTE>: a C<CanConnect> consuming this role will never talk to the
133             network, and will C<die> if asked to receive frames.
134              
135             =head1 DESCRIPTION
136              
137             This module I<replaces> the connection object provided by
138             L<Net::Stomp::MooseHelpers::CanConnect> so that it writes to disk
139             every outgoing frame, I<without actually talking to the network>. It
140             will also C<die> if the connection is asked to receive frames.
141              
142             The frames are written as they would be "on the wire" (no encoding
143             conversion happens), one file per frame. Each frame is written into a
144             directory under L</trace_basedir> with a name derived from the frame
145             destination.
146              
147             =head1 ATTRIBUTES
148              
149             =head2 C<trace_basedir>
150              
151             The directory under which frames will be dumped. Accepts strings and
152             L<Path::Class::Dir> objects. If it's not specified, every frame will
153             generate a warning.
154              
155             =begin Pod::Coverage
156              
157             trace
158              
159             connect
160             subscribe
161             unsubscribe
162             ack
163             receive_frame
164             send_frame
165             send
166              
167             =end Pod::Coverage
168              
169             1;
170              
171             =head1 AUTHOR
172              
173             Gianni Ceccarelli <gianni.ceccarelli@net-a-porter.com>
174              
175             =head1 COPYRIGHT AND LICENSE
176              
177             This software is copyright (c) 2014 by Net-a-porter.com.
178              
179             This is free software; you can redistribute it and/or modify it under
180             the same terms as the Perl 5 programming language system itself.
181              
182             =cut