| 1 |
package Cassandane::Util::TestURL; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Plack::Loader; |
| 7 |
use Plack::Request; |
| 8 |
use Plack::Response; |
| 9 |
|
| 10 |
use Test::TCP; |
| 11 |
use Carp qw(croak); |
| 12 |
|
| 13 |
use lib '.'; |
| 14 |
use Cassandane::PortManager; |
| 15 |
|
| 16 |
sub new |
| 17 |
{ |
| 18 |
my ($pkg, $args) = @_; |
| 19 |
|
| 20 |
my %attrs; |
| 21 |
|
| 22 |
for my $required (qw(app)) { |
| 23 |
$attrs{$required} = delete $args->{$required}; |
| 24 |
unless ($attrs{$required}) { |
| 25 |
croak("'$required' required for Cassandane::Test::URL->new"); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
my $self = bless {}, __PACKAGE__; |
| 30 |
|
| 31 |
$self->update($attrs{app}); |
| 32 |
|
| 33 |
return $self; |
| 34 |
} |
| 35 |
|
| 36 |
sub url |
| 37 |
{ |
| 38 |
my ($self) = @_; |
| 39 |
|
| 40 |
if ($self->was_unregistered) { |
| 41 |
croak("Cannot call ->url after ->unregister has been called!"); |
| 42 |
} |
| 43 |
|
| 44 |
$self->{url}; |
| 45 |
} |
| 46 |
|
| 47 |
sub _guard { shift->{_guard} } |
| 48 |
|
| 49 |
sub unregister |
| 50 |
{ |
| 51 |
my ($self) = @_; |
| 52 |
|
| 53 |
delete $self->{_guard}; |
| 54 |
$self->{was_unregistered} = 1; |
| 55 |
} |
| 56 |
|
| 57 |
sub was_unregistered { shift->{was_unregistered} } |
| 58 |
|
| 59 |
sub update |
| 60 |
{ |
| 61 |
my ($self, $content_or_app) = @_; |
| 62 |
|
| 63 |
unless (ref $content_or_app) { |
| 64 |
my $content = $content_or_app; |
| 65 |
# Plain ol' successful response |
| 66 |
$content_or_app = sub { |
| 67 |
return [ |
| 68 |
200, |
| 69 |
[], |
| 70 |
[ $content ], |
| 71 |
]; |
| 72 |
}; |
| 73 |
} |
| 74 |
|
| 75 |
{ |
| 76 |
package Shutdown::Guard; |
| 77 |
|
| 78 |
use POSIX qw(_exit); |
| 79 |
|
| 80 |
sub new { bless {}, __PACKAGE__ } |
| 81 |
|
| 82 |
sub DESTROY { |
| 83 |
_exit(0); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
my $host = "127.0.0.1"; |
| 88 |
|
| 89 |
my $app = sub { |
| 90 |
# No matter how we we leave this block, the $guard will go out |
| 91 |
# of scope while in the run phase, which means it can exit Perl |
| 92 |
# before any END block or object destructors are run. |
| 93 |
my $guard = Shutdown::Guard->new; |
| 94 |
|
| 95 |
# Test::TCP uses Term to stop us. We could just let that |
| 96 |
# terminate Perl cleanly, but in case something has put in a |
| 97 |
# signal handle or something else, let's just call exit here. |
| 98 |
# This will also cause our $guard above to go out of scope |
| 99 |
# in a timely manner during the run phase. |
| 100 |
local $SIG{TERM} = sub { exit; }; |
| 101 |
|
| 102 |
my $sock_or_port = shift; |
| 103 |
my $server = Plack::Loader->auto( |
| 104 |
host => $host, |
| 105 |
port => $sock_or_port, |
| 106 |
); |
| 107 |
|
| 108 |
$server->run($content_or_app); |
| 109 |
}; |
| 110 |
|
| 111 |
unless ($self->_guard) { |
| 112 |
if ($self->was_unregistered) { |
| 113 |
# We've already been unregistered. It's no longer safe to call |
| 114 |
# update because our port may have been reused by someone else |
| 115 |
# and our url is no longer useable |
| 116 |
croak("Cannot call ->update after ->unregister has been called!"); |
| 117 |
} |
| 118 |
|
| 119 |
my $guard = Test::TCP->new( |
| 120 |
host => $host, |
| 121 |
code => $app, |
| 122 |
port => Cassandane::PortManager::alloc($host), |
| 123 |
); |
| 124 |
|
| 125 |
my $port = $guard->port; |
| 126 |
|
| 127 |
$self->{url} = "http://$host:$port/"; |
| 128 |
$self->{_guard} = $guard; |
| 129 |
} else { |
| 130 |
$self->_guard->{code} = $app; |
| 131 |
|
| 132 |
$self->_guard->stop; |
| 133 |
$self->_guard->start; |
| 134 |
} |
| 135 |
|
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
1; |