blib/lib/Froody/Server/Test.pm

filter: Statementstmt 91.4% 43 / 47 Branchbran 70.0% 7 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 75.0% 3 / 4 totaltotal 88.7% 63 / 71 CC 8 SCAR 11.8
CC
8
Cov
88%
stop
6.0
start
3.7
BEGIN
1.0
CRAP 3.2
« blib/lib/Froody/Response/Content.pm ↑ Summary blib/lib/Froody/Implementation.pm »
1 =head1 NAME
2  
3 Froody::Server::Test
4  
5 =head1 SYNOPSIS
6  
7 =head1 DESCRIPTION
8  
9 =cut
10  
11 package Froody::Server::Test;
12 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Froody::Dispatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Froody::Server::Standalone;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =head1 METHODS
18  
19 =over
20  
21 =item client( 'Froody::Service', 'Froody::Service', .. )
22  
23 Starts a standalone Froody server, implementing the passed service classes,
24 and returns a Froody::Client object that will talk to the server, for local
25 testing of implementations. The server will be stopped on script exit.
26  
27 =cut
28  
29 # This is our random port, which is an attempt to not tromp over other users who might
30 # be testing their own froody clients at the same time.
31 4 our $port = 30_000 + ( $> % 400 );
32  
33 4 my $child;
34  
35 =item start
36  
37 Starts the Test server with a list of configuration details as specified
38 by the C<config> method of L<Froody::Dispatch>. Returns the pid of the
39 started process, or 0 if it happens to be the child process.
40 This will only start the server once, without calling C<stop> first.
41  
42 =cut
43  
44 3 ▶sub start {
Subroutine start: called CC=3 SCAR=13.0
Pod documented
45 3 my ($class, @impl) = @_;
46 3 ▶ return $child if defined $child;
Branch: 1 branch
BranchTrueFalseTotal
if defined $child 1 2 3
47 2 ▶ unless ($child = fork()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($child = fork) 0 2 2
48
49 # loading Froody::Server::Standalone does some things
50 # to our enviroment (for example, it piddles with the SIG handlers
51 # so we only want to do that in the child.)
52
53 0 my $server = Froody::Server::Standalone->new();
54 0 $server->config({
55 modules => \@impl,
56 });
57 0 $server->port($port);
58 0 $server->run;
59 }
60 2 return $child;
61 }
62  
63 =item endpoint
64  
65 Returns the endpoint for the Test server
66  
67 =cut
68  
69 2 ▶sub endpoint {
Subroutine endpoint: called CC=1 SCAR=0.0
Pod documented
70 2 my $class = shift;
71 2 return "http://localhost:$port/";
72 }
73  
74 # start the web server
75  
76 =item client
77  
78 Returns a C<Froody::Dispatch> based client that points to the test
79 server. As a side-effect, it will start up the test server if it has
80 not already been started.
81  
82 =cut
83  
84 2 ▶sub client {
Subroutine client: called CC=1 SCAR=0.0
Pod documented
85 2 my ($class, @impl) = @_;
86
87 2 $class->start(@impl);
88
89 2 sleep 1;
90 2 my $client = Froody::Dispatch->new();
91 2 $client->repository( Froody::Repository->new() );
92 2 $client->add_endpoint("http://localhost:$port");
93 2 return $client;
94 }
95  
96 # stop is documented
97 4 ▶sub stop {
Subroutine stop: called CC=6 SCAR=18.0
Pod undocumented
98 4 ▶ if ($child) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child) 2 2 4
99 # what the gosh-darn signals numbered on this box then?
100 4 ▶ use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 2 ▶ defined $Config{sig_name} || die "No sigs?";
Branch: 1 branch
BranchTrueFalseTotal
defined $Froody::Server::Test::Config{"sig_name"} or die "No sigs?" 2 0 2
102 2 my ($i, %signo);
103 2 foreach my $name (split(' ', $Config{sig_name})) {
104 136 $signo{$name} = $i;
105 136 $i++;
106 }
107
108 # die with more and more nastyness
109 2 for my $signal (qw(TERM KILL)) {
110 4 kill $signo{$signal}, $child;
111 4 ▶ exit unless kill 0, $child;
Branch: 1 branch
BranchTrueFalseTotal
unless kill 0, $child 0 4 4
112 4 sleep 1;
113 }
114
115 2 $child = undef;
116 }
117 }
118  
119 3 ▶END { stop() }
Subroutine END: called CC=1 SCAR=0.0
120  
121  
122 =back
123  
124 =head1 BUGS
125  
126 None known.
127  
128 Please report any bugs you find via the CPAN RT system.
129 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Froody>
130  
131 =head1 AUTHOR
132  
133 Copyright Fotango 2005. All rights reserved.
134  
135 Please see the main L<Froody> documentation for details of who has worked
136 on this project.
137  
138 This module is free software; you can redistribute it and/or modify it under
139 the same terms as Perl itself.
140  
141 =cut
142  
143 4 1;
144  
« blib/lib/Froody/Response/Content.pm ↑ Summary blib/lib/Froody/Implementation.pm »