File Coverage

blib/lib/Testcontainers/Wait/Base.pm
Criterion Covered Total %
statement 18 33 54.5
branch 0 4 0.0
condition 0 3 0.0
subroutine 6 7 85.7
pod 0 1 0.0
total 24 48 50.0


line stmt bran cond sub pod time code
1             package Testcontainers::Wait::Base;
2             # ABSTRACT: Base role for wait strategies
3              
4 4     4   18265 use strict;
  4         31  
  4         177  
5 4     4   22 use warnings;
  4         7  
  4         245  
6 4     4   25 use Moo::Role;
  4         9  
  4         48  
7 4     4   2411 use Carp qw( croak );
  4         9  
  4         306  
8 4     4   29 use Log::Any qw( $log );
  4         8  
  4         30  
9 4     4   1224 use Time::HiRes qw( time sleep );
  4         8  
  4         40  
10              
11             our $VERSION = '0.001';
12              
13             =head1 DESCRIPTION
14              
15             Base role that all wait strategies must consume. Provides the common
16             C interface and the polling loop infrastructure.
17              
18             =cut
19              
20             has startup_timeout => (
21             is => 'rw',
22             default => 60,
23             );
24              
25             =attr startup_timeout
26              
27             Maximum seconds to wait before timing out. Default: 60.
28             Can be overridden per-strategy or via the C option in C.
29              
30             =cut
31              
32             has poll_interval => (
33             is => 'rw',
34             default => 0.1,
35             );
36              
37             =attr poll_interval
38              
39             Seconds between poll attempts. Default: 0.1 (100ms).
40              
41             =cut
42              
43             requires 'check';
44              
45             sub wait_until_ready {
46 0     0 0   my ($self, $container, $timeout) = @_;
47 0   0       $timeout //= $self->startup_timeout;
48              
49 0           my $deadline = time() + $timeout;
50 0           my $strategy_name = ref($self) =~ s/.*:://r;
51              
52 0           $log->debugf("Waiting for %s strategy (timeout: %ds)", $strategy_name, $timeout);
53              
54 0           while (time() < $deadline) {
55 0           my $ready = eval { $self->check($container) };
  0            
56 0 0         if ($@) {
57 0           $log->tracef("Wait check failed: %s", $@);
58             }
59              
60 0 0         if ($ready) {
61 0           $log->debugf("%s strategy: container is ready", $strategy_name);
62 0           return 1;
63             }
64              
65 0           sleep($self->poll_interval);
66             }
67              
68 0           croak sprintf(
69             "Timeout after %ds waiting for container %s (%s strategy)",
70             $timeout, $container->id, $strategy_name
71             );
72             }
73              
74             =method wait_until_ready($container, $timeout)
75              
76             Poll the C method until it returns true or the timeout is reached.
77             Dies with a timeout error if the container doesn't become ready in time.
78              
79             =cut
80              
81             1;