Perlito5X/Test2/IPC/Driver/Files.pm untested

filter: Statementstmt 0.0% 0 / 243 Branchbran 0.0% 0 / 108 Conditioncond 0.0% 0 / 53 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 484 CC 55 SCAR 39.7
CC
55
Cov
0%
DESTROY
132.0
drop_hub
90.0
send
90.0
CRAP 52.8
1 package Test2::IPC::Driver::Files;
2 use strict;
3 use warnings;
4  
5 our $VERSION = '1.302073';
6  
7  
8 BEGIN { require Test2::IPC::Driver; our @ISA = qw(Test2::IPC::Driver) }
9  
10 use Test2::Util::HashBase qw{tempdir event_id tid pid globals};
11  
12 use Scalar::Util qw/blessed/;
13 use File::Temp();
14 use Storable();
15 use File::Spec();
16 use POSIX();
17  
18 use Test2::Util qw/try get_tid pkg_to_file IS_WIN32 ipc_separator/;
19 use Test2::API qw/test2_ipc_set_pending/;
20  
21 BEGIN {
22 if (IS_WIN32) {
23 my $max_tries = 5;
24  
25 *do_rename = sub {
26 my ($from, $to) = @_;
27  
28 my $err;
29 for (1 .. $max_tries) {
30 return (1) if rename($from, $to);
31 $err = "$!";
32 last if $_ == $max_tries;
33 sleep 1;
34 }
35  
36 return (0, $err);
37 };
38 *do_unlink = sub {
39 my ($file) = @_;
40  
41 my $err;
42 for (1 .. $max_tries) {
43 return (1) if unlink($file);
44 $err = "$!";
45 last if $_ == $max_tries;
46 sleep 1;
47 }
48  
49 return (0, "$!");
50 };
51 }
52 else {
53 *do_rename = sub {
54 my ($from, $to) = @_;
55 return (1) if rename($from, $to);
56 return (0, "$!");
57 };
58 *do_unlink = sub {
59 my ($file) = @_;
60 return (1) if unlink($file);
61 return (0, "$!");
62 };
63 }
64 }
65  
66 sub use_shm { 1 }
67 sub shm_size() { 64 }
68  
69 sub is_viable { 1 }
70  
71 sub init {
72 my $self = shift;
73  
74 my $tmpdir = File::Temp::tempdir(
75 $ENV{T2_TEMPDIR_TEMPLATE} || "test2" . ipc_separator . $$ . ipc_separator . "XXXXXX",
76 CLEANUP => 0,
77 TMPDIR => 1,
78 );
79  
80 $self->abort_trace("Could not get a temp dir") unless $tmpdir;
81  
82 $self->{+TEMPDIR} = File::Spec->canonpath($tmpdir);
83  
84 print STDERR "\nIPC Temp Dir: $tmpdir\n\n"
85 if $ENV{T2_KEEP_TEMPDIR};
86  
87 $self->{+EVENT_ID} = 1;
88  
89 $self->{+TID} = get_tid();
90 $self->{+PID} = $$;
91  
92 $self->{+GLOBALS} = {};
93  
94 return $self;
95 }
96  
97 sub hub_file {
98 my $self = shift;
99 my ($hid) = @_;
100 my $tdir = $self->{+TEMPDIR};
101 return File::Spec->catfile($tdir, "HUB" . ipc_separator . $hid);
102 }
103  
104 sub event_file {
105 my $self = shift;
106 my ($hid, $e) = @_;
107  
108 my $tempdir = $self->{+TEMPDIR};
109 my $type = blessed($e) or $self->abort("'$e' is not a blessed object!");
110  
111 $self->abort("'$e' is not an event object!")
112 unless $type->isa('Test2::Event');
113  
114 my @type = split '::', $type;
115 my $name = join(ipc_separator, $hid, $$, get_tid(), $self->{+EVENT_ID}++, @type);
116  
117 return File::Spec->catfile($tempdir, $name);
118 }
119  
120 sub add_hub {
121 my $self = shift;
122 my ($hid) = @_;
123  
124 my $hfile = $self->hub_file($hid);
125  
126 $self->abort_trace("File for hub '$hid' already exists")
127 if -e $hfile;
128  
129 open(my $fh, '>', $hfile) or $self->abort_trace("Could not create hub file '$hid': $!");
130 print $fh "$$\n" . get_tid() . "\n";
131 close($fh);
132 }
133  
134 sub drop_hub {
135 my $self = shift;
136 my ($hid) = @_;
137  
138 my $tdir = $self->{+TEMPDIR};
139 my $hfile = $self->hub_file($hid);
140  
141 $self->abort_trace("File for hub '$hid' does not exist")
142 unless -e $hfile;
143  
144 open(my $fh, '<', $hfile) or $self->abort_trace("Could not open hub file '$hid': $!");
145 my ($pid, $tid) = <$fh>;
146 close($fh);
147  
148 $self->abort_trace("A hub file can only be closed by the process that started it\nExpected $pid, got $$")
149 unless $pid == $$;
150  
151 $self->abort_trace("A hub file can only be closed by the thread that started it\nExpected $tid, got " . get_tid())
152 unless get_tid() == $tid;
153  
154 if ($ENV{T2_KEEP_TEMPDIR}) {
155 my ($ok, $err) = do_rename($hfile, File::Spec->canonpath("$hfile.complete"));
156 $self->abort_trace("Could not rename file '$hfile' -> '$hfile.complete': $err") unless $ok
157 }
158 else {
159 my ($ok, $err) = do_unlink($hfile);
160 $self->abort_trace("Could not remove file for hub '$hid': $err") unless $ok
161 }
162  
163 opendir(my $dh, $tdir) or $self->abort_trace("Could not open temp dir!");
164 for my $file (readdir($dh)) {
165 next if $file =~ m{\.complete$};
166 next unless $file =~ m{^$hid};
167 $self->abort_trace("Not all files from hub '$hid' have been collected!");
168 }
169 closedir($dh);
170 }
171  
172 sub send {
173 my $self = shift;
174 my ($hid, $e, $global) = @_;
175  
176 my $tempdir = $self->{+TEMPDIR};
177 my $hfile = $self->hub_file($hid);
178 my $dest = $global ? 'GLOBAL' : $hid;
179  
180 $self->abort(<<" EOT") unless $global || -f $hfile;
181 hub '$hid' is not available, failed to send event!
182  
183 There was an attempt to send an event to a hub in a parent process or thread,
184 but that hub appears to be gone. This can happen if you fork, or start a new
185 thread from inside subtest, and the parent finishes the subtest before the
186 child returns.
187  
188 This can also happen if the parent process is done testing before the child
189 finishes. Test2 normally waits automatically in the root process, but will not
190 do so if Test::Builder is loaded for legacy reasons.
191 EOT
192  
193 my $file = $self->event_file($dest, $e);
194 my $ready = File::Spec->canonpath("$file.ready");
195  
196 if ($global) {
197 my $name = $ready;
198 $name =~ s{^.*(GLOBAL)}{GLOBAL};
199 $self->{+GLOBALS}->{$hid}->{$name}++;
200 }
201  
202 my ($old, $blocked);
203 unless(IS_WIN32) {
204 my $to_block = POSIX::SigSet->new(
205 POSIX::SIGINT(),
206 POSIX::SIGALRM(),
207 POSIX::SIGHUP(),
208 POSIX::SIGTERM(),
209 POSIX::SIGUSR1(),
210 POSIX::SIGUSR2(),
211 );
212 $old = POSIX::SigSet->new;
213 $blocked = POSIX::sigprocmask(POSIX::SIG_BLOCK(), $to_block, $old);
214 # Silently go on if we failed to log signals, not much we can do.
215 }
216  
217 # Write and rename the file.
218 my ($ok, $err) = try {
219 Storable::store($e, $file);
220 my ($ok, $err) = do_rename("$file", $ready);
221 unless ($ok) {
222 POSIX::sigprocmask(POSIX::SIG_SETMASK(), $old, POSIX::SigSet->new()) if defined $blocked;
223 $self->abort("Could not rename file '$file' -> '$ready': $err");
224 };
225 test2_ipc_set_pending(substr($file, -(shm_size)));
226 };
227  
228 # If our block was successful we want to restore the old mask.
229 POSIX::sigprocmask(POSIX::SIG_SETMASK(), $old, POSIX::SigSet->new()) if defined $blocked;
230  
231 if (!$ok) {
232 my $src_file = __FILE__;
233 $err =~ s{ at \Q$src_file\E.*$}{};
234 chomp($err);
235 my $tid = get_tid();
236 my $trace = $e->trace->debug;
237 my $type = blessed($e);
238  
239 $self->abort(<<" EOT");
240  
241 *******************************************************************************
242 There was an error writing an event:
243 Destination: $dest
244 Origin PID: $$
245 Origin TID: $tid
246 Event Type: $type
247 Event Trace: $trace
248 File Name: $file
249 Ready Name: $ready
250 Error: $err
251 *******************************************************************************
252  
253 EOT
254 }
255  
256 return 1;
257 }
258  
259 sub cull {
260 my $self = shift;
261 my ($hid) = @_;
262  
263 my $tempdir = $self->{+TEMPDIR};
264  
265 opendir(my $dh, $tempdir) or $self->abort("could not open IPC temp dir ($tempdir)!");
266  
267 my @out;
268 for my $info (sort cmp_events map { $self->should_read_event($hid, $_) } readdir($dh)) {
269 my $full = $info->{full_path};
270 my $obj = $self->read_event_file($full);
271 push @out => $obj;
272  
273 # Do not remove global events
274 next if $info->{global};
275  
276 if ($ENV{T2_KEEP_TEMPDIR}) {
277 my $complete = File::Spec->canonpath("$full.complete");
278 my ($ok, $err) = do_rename($full, $complete);
279 $self->abort("Could not rename IPC file '$full', '$complete': $err") unless $ok;
280 }
281 else {
282 my ($ok, $err) = do_unlink("$full");
283 $self->abort("Could not unlink IPC file '$full': $err") unless $ok;
284 }
285 }
286  
287 closedir($dh);
288 return @out;
289 }
290  
291 sub parse_event_filename {
292 my $self = shift;
293 my ($file) = @_;
294  
295 # The || is to force 0 in false
296 my $complete = substr($file, -9, 9) eq '.complete' || 0 and substr($file, -9, 9, "");
297 my $ready = substr($file, -6, 6) eq '.ready' || 0 and substr($file, -6, 6, "");
298  
299 my @parts = split ipc_separator, $file;
300 my ($global, $hid) = $parts[0] eq 'GLOBAL' ? (1, shift @parts) : (0, join ipc_separator, splice(@parts, 0, 3));
301 my ($pid, $tid, $eid) = splice(@parts, 0, 3);
302 my $type = join '::' => @parts;
303  
304 return {
305 ready => $ready,
306 complete => $complete,
307 global => $global,
308 type => $type,
309 hid => $hid,
310 pid => $pid,
311 tid => $tid,
312 eid => $eid,
313 };
314 }
315  
316 sub should_read_event {
317 my $self = shift;
318 my ($hid, $file) = @_;
319  
320 return if substr($file, 0, 1) eq '.';
321  
322 my $parsed = $self->parse_event_filename($file);
323  
324 return if $parsed->{complete};
325 return unless $parsed->{ready};
326 return unless $parsed->{global} || $parsed->{hid} eq $hid;
327  
328 return if $parsed->{global} && $self->{+GLOBALS}->{$hid}->{$file}++;
329  
330 # Untaint the path.
331 my $full = File::Spec->catfile($self->{+TEMPDIR}, $file);
332 ($full) = ($full =~ m/^(.*)$/gs) if ${^TAINT};
333  
334 $parsed->{full_path} = $full;
335  
336 return $parsed;
337 }
338  
339 sub cmp_events {
340 # Globals first
341 return -1 if $a->{global} && !$b->{global};
342 return 1 if $b->{global} && !$a->{global};
343  
344 return $a->{pid} <=> $b->{pid}
345 || $a->{tid} <=> $b->{tid}
346 || $a->{eid} <=> $b->{eid};
347 }
348  
349 sub read_event_file {
350 my $self = shift;
351 my ($file) = @_;
352  
353 my $obj = Storable::retrieve($file);
354 $self->abort("Got an unblessed object: '$obj'")
355 unless blessed($obj);
356  
357 unless ($obj->isa('Test2::Event')) {
358 my $pkg = blessed($obj);
359 my $mod_file = pkg_to_file($pkg);
360 my ($ok, $err) = try { require $mod_file };
361  
362 $self->abort("Event has unknown type ($pkg), tried to load '$mod_file' but failed: $err")
363 unless $ok;
364  
365 $self->abort("'$obj' is not a 'Test2::Event' object")
366 unless $obj->isa('Test2::Event');
367 }
368  
369 return $obj;
370 }
371  
372 sub waiting {
373 my $self = shift;
374 require Test2::Event::Waiting;
375 $self->send(
376 GLOBAL => Test2::Event::Waiting->new(
377 trace => Test2::Util::Trace->new(frame => [caller()]),
378 ),
379 'GLOBAL'
380 );
381 return;
382 }
383  
384 sub DESTROY {
385 my $self = shift;
386  
387 return unless defined $self->pid;
388 return unless defined $self->tid;
389  
390 return unless $$ == $self->pid;
391 return unless get_tid() == $self->tid;
392  
393 my $tempdir = $self->{+TEMPDIR};
394  
395 opendir(my $dh, $tempdir) or $self->abort("Could not open temp dir! ($tempdir)");
396 while(my $file = readdir($dh)) {
397 next if $file =~ m/^\.+$/;
398 next if $file =~ m/\.complete$/;
399 my $full = File::Spec->catfile($tempdir, $file);
400  
401 my $sep = ipc_separator;
402 if ($file =~ m/^(GLOBAL|HUB$sep)/) {
403 $full =~ m/^(.*)$/;
404 $full = $1; # Untaint it
405 next if $ENV{T2_KEEP_TEMPDIR};
406 my ($ok, $err) = do_unlink($full);
407 $self->abort("Could not unlink IPC file '$full': $err") unless $ok;
408 next;
409 }
410  
411 $self->abort("Leftover files in the directory ($full)!\n");
412 }
413 closedir($dh);
414  
415 if ($ENV{T2_KEEP_TEMPDIR}) {
416 print STDERR "# Not removing temp dir: $tempdir\n";
417 return;
418 }
419  
420 rmdir($tempdir) or warn "Could not remove IPC temp dir ($tempdir)";
421 }
422  
423 1;
424  
425 __END__