blib/lib/Ubic/Service/Utils.pm untested

filter: Statementstmt 0.0% 0 / 42 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 4 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 60 SCAR 30.0
CC
4
Cov
0%
wait_for_status
20.0
BEGIN
2.0
BEGIN
2.0
CRAP 20.0
1 package Ubic::Service::Utils;
2 $Ubic::Service::Utils::VERSION = '1.59';
3 use strict;
4 use warnings;
5  
6 # ABSTRACT: helpers for custom service authors
7  
8  
9 use Params::Validate qw(:all);
10 use Time::HiRes qw(sleep);
11 use Ubic::Result qw(result);
12  
13 use parent qw(Exporter);
14 our @EXPORT_OK = qw(wait_for_status);
15  
16 sub wait_for_status {
17 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 my $trials = $params->{trials} || 1;
24 my $step = $params->{step} || 0.1;
25 my $service = $params->{service};
26 my $expect_status = $params->{expect_status};
27 my @expect_status;
28 if (ref $expect_status) {
29 @expect_status = @$expect_status;
30 }
31 else {
32 @expect_status = ($expect_status);
33 }
34  
35 my $time = 0;
36 my $status;
37 for my $trial (1..$trials) {
38 $status = result($service->status);
39 my $status_str = $status->status;
40 last if grep { $_ eq $status_str } @expect_status;
41  
42 if ($trial < $trials) {
43 my $sleep = $step * $trial;
44 sleep($sleep);
45 $time += $sleep;
46 }
47 }
48 return $status;
49 }
50  
51  
52 1;
53  
54 __END__