File Coverage

lib/WebService/Shippo/Async.pm
Criterion Covered Total %
statement 26 44 59.0
branch 0 8 0.0
condition n/a
subroutine 9 13 69.2
pod 0 3 0.0
total 35 68 51.4


line stmt bran cond sub pod time code
1 7     7   97 use strict;
  7         26  
  7         394  
2 7     7   64 use warnings;
  7         24  
  7         403  
3 7     7   63 use MRO::Compat 'c3';
  7         22  
  7         568  
4              
5             package WebService::Shippo::Async;
6 7     7   67 use Carp ( 'confess' );
  7         30  
  7         845  
7 7     7   67 use List::Util ( 'any' );
  7         82  
  7         1022  
8 7     7   67 use Params::Callbacks ( 'callbacks' );
  7         23  
  7         508  
9 7     7   10183 use Time::HiRes ( 'gettimeofday', 'tv_interval' );
  7         15879  
  7         46  
10              
11             {
12             my $value = 60;
13              
14             sub timeout
15             {
16 0     0 0   my ( $invocant, $new_value ) = @_;
17 0 0         return $value unless @_ > 1;
18 0           $value = $new_value;
19 0           return $invocant;
20             }
21              
22             sub timeout_exceeded
23             {
24 0     0 0   my ( $invocant, $tv_start ) = @_;
25 0           return tv_interval( $tv_start ) > $value;
26             }
27             }
28              
29             sub wait_while_status_in
30             {
31 0     0 0   my ( $callbacks, $invocant, @states ) = &callbacks;
32 0           my $start_time = [ gettimeofday() ];
33 0           my $delay = 0.5;
34 0           my $backoff = 0.25;
35 0           while ( !$invocant->timeout_exceeded( $start_time ) ) {
36             return $invocant
37 0 0   0     unless any { /^$invocant->{object_status}$/ } @states;
  0            
38 0           sleep( $delay );
39 0           $delay += $backoff;
40 0 0         last unless $callbacks->transform( $invocant->refresh );
41             }
42 0 0         confess 'Asynchronus operation timed-out'
43             if $invocant->timeout_exceeded( $start_time );
44 0           return;
45             }
46              
47             BEGIN {
48 7     7   3847 no warnings 'once';
  7         89  
  7         414  
49 7     7   19 *Shippo::Async:: = *WebService::Shippo::Async::;
50 7         192 *wait_if_status_in = *wait_while_status_in;
51             }
52              
53             1;