blib/lib/DR/Tnt/Test/TntInstance.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 10 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 166 SCAR 58.3
CC
18
Cov
0%
start
90.0
new
42.0
kill
12.0
CRAP 342.0
1 use utf8;
2 use strict;
3 use warnings;
4  
5 package DR::Tnt::Test::TntInstance;
6 use File::Temp;
7 use File::Spec::Functions 'rel2abs', 'catfile';
8 use Time::HiRes ();
9 use IO::Socket::INET;
10 use POSIX;
11  
12 sub new {
13 my ($class, %opts) = @_;
14  
15 die "-lua or -make_lua option is not defined"
16 unless $opts{-lua} or $opts{-make_lua};
17 $opts{-lua} = rel2abs $opts{-lua} if $opts{-lua};
18 my $self = bless \%opts => ref($class) || $class;
19  
20  
21 if ($self->{-dir}) {
22 die "$self->{-dir} not found" unless -d $self->{-dir};
23 } else {
24 $self->{-dir} = File::Temp::tempdir;
25 $self->{-clean} = [ $self->{-dir} ];
26 }
27  
28 $self->{-log} = rel2abs catfile $self->{-dir}, 'tarantool.log';
29 $self->{-log_seek} = 0;
30 if (-r $self->{-log}) {
31 open my $fh, '<', $self->{-log};
32 seek $fh, 0, 2;
33 $self->{-log_seek} = tell $fh;
34 close $fh;
35 }
36  
37  
38 $self->start;
39 }
40  
41  
42 sub start {
43 my ($self) = @_;
44 if ($self->{pid} = fork) {
45 for (1 .. 10) {
46 Time::HiRes::sleep .1;
47 last unless $self->{-port};
48
49 next unless IO::Socket::INET->new(
50 PeerHost => '127.0.0.1',
51 PeerPort => $self->{-port},
52 Proto => 'tcp',
53 (($^O eq 'MSWin32') ? () : (ReuseAddr => 1)),
54 );
55 last if $self->log =~ /entering the event loop/;
56 }
57  
58 return $self;
59 }
60
61 for (open my $fh, '>>', $self->{-log}) {
62 POSIX::dup2(fileno($fh), fileno(STDOUT));
63 POSIX::dup2(fileno($fh), fileno(STDERR));
64 close $fh;
65 }
66  
67 unless ($self->{-lua}) {
68 $self->{-lua} = catfile $self->{-dir}, 'make_lua.lua';
69 open my $fh, '>:raw', $self->{-lua}
70 or warn "Can't create $self->{-lua}: $!\n";
71 my $lua = $self->{-make_lua};
72 utf8::encode $lua if utf8::is_utf8 $lua;
73 print $fh $lua;
74 close $fh;
75 }
76  
77 chdir $self->{-dir};
78 if ($self->port) {
79 $ENV{PRIMARY_PORT} = $self->port;
80 }
81 $ENV{WORK_DIR} = $self->{-dir};
82 exec tarantool => $self->{-lua};
83 die "Can't start tarantool";
84 }
85  
86 sub stop {
87 my ($self) = @_;
88 $self->kill('TERM');
89 }
90  
91 sub port { $_[0]->{-port} }
92 sub is_started {
93 return $_[0]->log =~ /entering the event loop/;
94 }
95  
96 sub log {
97 my ($self) = @_;
98 return '' unless -r $self->{-log};
99 open my $fh, '<', $self->{-log};
100 seek $fh, 0, $self->{-log_seek};
101 local $/;
102 my $data = <$fh>;
103 return $data;
104 }
105  
106 sub clean {
107 my ($self) = @_;
108 return unless $self->{-clean};
109 for (@{ $self->{-clean} }) {
110 system "rm -fr $_";
111 }
112 }
113  
114 sub pid { $_[0]->{pid} };
115  
116 sub kill {
117 my ($self, $sig) = @_;
118 return unless $self->pid;
119 $sig ||= 'TERM';
120 if (kill $sig, $self->pid) {
121 waitpid $self->pid, 0;
122 } else {
123 warn sprintf "Can't kill -$sig %s\n", $self->pid;
124 }
125 delete $self->{pid};
126 }
127  
128 sub DESTROY {
129 my ($self) = @_;
130 $self->kill('KILL');
131 $self->clean;
132 }
133  
134  
135 1;