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.58_01'; # TRIAL
3 25     25   128 use strict;
  25         48  
  25         831  
4 25     25   135 use warnings;
  25         44  
  25         921  
5              
6             # ABSTRACT: helpers for custom service authors
7              
8              
9 25     25   176 use Params::Validate qw(:all);
  25         38  
  25         5624  
10 25     25   148 use Time::HiRes qw(sleep);
  25         48  
  25         155  
11 25     25   2563 use Ubic::Result qw(result);
  25         40  
  25         1271  
12              
13 25     25   147 use parent qw(Exporter);
  25         39  
  25         215  
14             our @EXPORT_OK = qw(wait_for_status);
15              
16             sub wait_for_status {
17 53     53 1 3158 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 53   50     571 my $trials = $params->{trials} || 1;
24 53   50     285 my $step = $params->{step} || 0.1;
25 53         110 my $service = $params->{service};
26 53         185 my $expect_status = $params->{expect_status};
27 53         88 my @expect_status;
28 53 50       199 if (ref $expect_status) {
29 53         221 @expect_status = @$expect_status;
30             }
31             else {
32 0         0 @expect_status = ($expect_status);
33             }
34              
35 53         81 my $time = 0;
36 53         119 my $status;
37 53         277 for my $trial (1..$trials) {
38 53         302 $status = result($service->status);
39 53         235 my $status_str = $status->status;
40 53 50       106 last if grep { $_ eq $status_str } @expect_status;
  80         291  
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 53         239 return $status;
49             }
50              
51              
52             1;
53              
54             __END__