Jifty/TestServer.pm

filter: Statementstmt 40.9% 27 / 66 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 57.1% 8 / 14 Podpod 100% 3 / 3 totaltotal 35.8% 38 / 106 CC 13 SCAR 38.0
CC
13
Cov
31%
started_ok
110.0
BUILD
12.0
DEMOLISH
6.0
CRAP 44.7
« Jifty/API.pm ↑ Summary Jifty/Test/WWW/Selenium.pm »
1 package Jifty::TestServer;
2 1 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Test::Script::Run 'get_perl_cmd';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Plack::Loader;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 has port => (is => "rw", isa => "Int");
12  
13 =head1 NAME
14  
15 Jifty::TestServer - Starting and stopping jifty server for tests
16  
17 =head1 DESCRIPTION
18  
19 =head1 METHOD
20  
21 =head2 started_ok
22  
23 Like started_ok in C<Test::HTTP::Server::Simple>, start the server and
24 return the URL.
25  
26 =head2 BUILD
27  
28 Sets up the server instance.
29  
30 =cut
31  
32 0 ▶sub BUILD {
Subroutine BUILD: not called CC=3 SCAR=24.8
Pod documented
33 0 my $self = shift;
34 0 ▶ Jifty->config->framework('Web')->{'Port'} = $self->port if $self->port;
Branch: 1 branch
BranchTrueFalseTotal
if $self->port 0 0 0
35 0 ▶ $self->port( Jifty->config->framework('Web')->{'Port'} || 8888 );
Condition0%0/2
'Jifty'->config->framework('Web')->{'Port'} || 8888
l!l
00
MC/DC0%0/1
'Jifty'->config->framework('Web')->{'Port'} || 8888
'Jifty'->config->framework('Web')->{'Port'}
Truth table0%0/2
A
A= 'Jifty'->config->framework('Web')->{'Port'}
Aresult
11
00
36 }
37  
38 0 ▶sub started_ok {
Subroutine started_ok: not called CC=10 SCAR=47.0
Pod documented
39 0 my $self = shift;
40 0 my $text = shift;
41 0 ▶ $text = 'started server' unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
42  
43 0 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 0 0
44 # dirty hack until Test::Builder->skip_rest comes true
45  
46 0 my $why = "live test doesn't work on Win32 at the moment";
47  
48 0 my $Tester = Test::Builder->new;
49 0 $Tester->skip($why);
50  
51 0 ▶ unless ($Tester->{No_Plan}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Tester->{"No_Plan"}) 0 0 0
52 0 ▶ for (my $ct = $Tester->{Curr_Test};
Branch: 1 branch
BranchTrueFalseTotal
if ($ct < $Tester->{"Expected_Tests"}) 0 0 0
53 $ct < $Tester->{Expected_Tests};
54 $ct++
55 ) {
56 0 $Tester->skip($why); # skip rest of the test
57 }
58 }
59 0 exit(0);
60 }
61  
62 $self->{plack_server} = Plack::Loader->load
63 ($ENV{JIFTY_TEST_SERVER},
64 port => $self->port,
65 0 ▶ server_ready => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 kill 'USR1' => getppid();
67 0 });
68 0 $Jifty::SERVER = $self;
69  
70 0 my $pid = fork();
71 0 ▶ die "failed to fork" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
72  
73 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 0 0 0
74 # We are expecting a USR1 from the child process after it's
75 # ready to listen.
76 0 my $handled;
77 0 ▶ $SIG{USR1} = sub { $handled = 1};
Subroutine __ANON__: not called CC=1 SCAR=6.9
78 0 sleep 15;
79 0 ▶ Test::More::diag "did not get expected USR1 for test server readiness"
Branch: 1 branch
BranchTrueFalseTotal
unless $handled 0 0 0
80 unless $handled;
81 0 ▶ $self->{cleanup} = [sub { kill TERM => $pid }];
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 0 my $Tester = Test::Builder->new;
83 0 $Tester->ok(1, $text);
84 # XXX: pull from jifty::config maybe
85 0 return "http://localhost:".$self->port;
86 } else {
87 0 Jifty->handle->dbh->{'InactiveDestroy'} = 1;
88 0 Jifty->setup_database_connection;
89 }
90  
91 0 require POSIX;
92 0 ▶ if ( $^O !~ /MSWin32/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($^O =~ /MSWin32/) 0 0 0
93 0 ▶ POSIX::setsid()
Branch: 1 branch
BranchTrueFalseTotal
POSIX::setsid() or die "Can't start a new session: $!" 0 0 0
94 or die "Can't start a new session: $!";
95 }
96  
97 0 $self->{plack_server}->run(Jifty->handler->psgi_app);
98 0 exit;
99 }
100  
101 =head2 DEMOLISH
102  
103 Calls any cleanup handlers before exiting
104  
105 =cut
106  
107 0 ▶sub DEMOLISH {
Subroutine DEMOLISH: not called CC=2 SCAR=17.9
Pod documented
108 0 $_->() for @{$_[0]->{cleanup}}
109 }
110  
111 1 __PACKAGE__->meta->make_immutable;
112 1 ▶no Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
113 1 1;
« Jifty/API.pm ↑ Summary Jifty/Test/WWW/Selenium.pm »