File Coverage

lib/Ubic/Service/Utils.pm
Criterion Covered Total %
statement 34 39 87.1
branch 2 6 33.3
condition 2 4 50.0
subroutine 7 7 100.0
pod 1 1 100.0
total 46 57 80.7


line stmt bran cond sub pod time code
1             package Ubic::Service::Utils;
2             $Ubic::Service::Utils::VERSION = '1.60';
3 23     23   73 use strict;
  23         23  
  23         502  
4 23     23   64 use warnings;
  23         22  
  23         493  
5              
6             # ABSTRACT: helpers for custom service authors
7              
8              
9 23     23   68 use Params::Validate qw(:all);
  23         22  
  23         3097  
10 23     23   90 use Time::HiRes qw(sleep);
  23         24  
  23         114  
11 23     23   1590 use Ubic::Result qw(result);
  23         28  
  23         797  
12              
13 23     23   81 use parent qw(Exporter);
  23         21  
  23         108  
14             our @EXPORT_OK = qw(wait_for_status);
15              
16             sub wait_for_status {
17 41     41 1 1798 my $params = validate(@_, {
18             service => { isa => 'Ubic::Service' },
19             trials => { type => SCALAR | UNDEF, optional => 1 },
20             step => { type => SCALAR | UNDEF, optional => 1 },
21             expect_status => { type => SCALAR | ARRAYREF },
22             });
23 41   50     389 my $trials = $params->{trials} || 1;
24 41   50     264 my $step = $params->{step} || 0.1;
25 41         64 my $service = $params->{service};
26 41         103 my $expect_status = $params->{expect_status};
27 41         81 my @expect_status;
28 41 50       170 if (ref $expect_status) {
29 41         117 @expect_status = @$expect_status;
30             }
31             else {
32 0         0 @expect_status = ($expect_status);
33             }
34              
35 41         68 my $time = 0;
36 41         54 my $status;
37 41         176 for my $trial (1..$trials) {
38 41         187 $status = result($service->status);
39 41         136 my $status_str = $status->status;
40 41 50       100 last if grep { $_ eq $status_str } @expect_status;
  62         199  
41              
42 0 0       0 if ($trial < $trials) {
43 0         0 my $sleep = $step * $trial;
44 0         0 sleep($sleep);
45 0         0 $time += $sleep;
46             }
47             }
48 41         155 return $status;
49             }
50              
51              
52             1;
53              
54             __END__