File Coverage

blib/lib/Net/Stomp/MooseHelpers/TraceOnly.pm
Criterion Covered Total %
statement 32 37 86.4
branch 2 4 50.0
condition 1 3 33.3
subroutine 12 15 80.0
pod 0 8 0.0
total 47 67 70.1


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