blib/lib/DBIx/Loop/Loop/IOAsync.pm

filter: Statementstmt 90.3% 47 / 52 Branchbran 75.0% 9 / 12 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 81.2% 13 / 16 Podpod 0.0% 0 / 9 totaltotal 70.7% 70 / 99 CC 9 SCAR 7.4
CC
9
Cov
81%
add_writer
6.0
add_reader
2.1
_fh
2.0
CRAP 2.1
« blib/lib/DBIx/Loop.pm ↑ Summary blib/arch/DBIx/Loop/Install/Files.pm »
1 package DBIx::Loop::Loop::IOAsync;
2  
3 18 ▶use 5.008003;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 18 our $VERSION = '0.09';
9  
10 # A loop adapter over IO::Async::Loop, implementing the DBIx::Loop seam:
11 # add_reader / add_writer / remove / timer / new_future / await. The backends
12 # only ever call these five-plus-await methods; this is the whole coupling to
13 # IO::Async. (Phase 05 adds Mojo / AnyEvent / Hyperman adapters against the
14 # same seam, plus a conformance suite.)
15  
16 18 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
17 18 my ($class, %opt) = @_;
18 18 my $loop = $opt{loop};
19 18 ▶ if (!$loop) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($loop) 18 0 18
20 18 require IO::Async::Loop;
21 18 $loop = IO::Async::Loop->new;
22 }
23 18 return bless { loop => $loop, fh => {} }, $class;
24 }
25  
26 0 ▶sub loop { $_[0]{loop} }
Subroutine loop: not called CC=1 SCAR=6.9
Pod undocumented
27  
28 # watch a raw fd (owned by C) for readiness. We dup it into a Perl filehandle
29 # so IO::Async can watch it; remove() closes only our dup, never C's fd.
30 53 ▶sub _fh {
Subroutine _fh: called CC=2 SCAR=7.1
31 53 my ($self, $fd, $mode) = @_;
32 53 ▶ open my $fh, $mode, $fd or Carp::croak("DBIx::Loop: fdopen($fd): $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $mode, $fd or Carp::croak("DBIx::Loop: fdopen($fd): $!") 53 0 53
33 53 return $fh;
34 }
35  
36 53 ▶sub add_reader {
Subroutine add_reader: called CC=2 SCAR=7.6
Pod undocumented
37 53 my ($self, $fd, $cb) = @_;
38 53 ▶ my $fh = $self->{fh}{$fd} ||= $self->_fh($fd, '<&');
Condition33%1/3
$self->{"fh"}{$fd} ||= $self->_fh($fd, "<&")
l!l&&r!l&&!r
0530
MC/DC0%0/2
$self->{"fh"}{$fd} ||= $self->_fh($fd, "<&")
$self->{"fh"}{$fd} $self->_fh($fd, "<&")
Truth table33%1/3
A ||= B
A= $self->{"fh"}{$fd}B= $self->_fh($fd, "<&")
ABresult
1X1
011
000
39 53 $self->{loop}->watch_io(handle => $fh, on_read_ready => $cb);
40 }
41  
42 0 ▶sub add_writer {
Subroutine add_writer: not called CC=2 SCAR=17.9
Pod undocumented
43 0 my ($self, $fd, $cb) = @_;
44 0 ▶ my $fh = $self->{fh}{$fd} ||= $self->_fh($fd, '>&');
Condition0%0/3
$self->{"fh"}{$fd} ||= $self->_fh($fd, ">&")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"fh"}{$fd} ||= $self->_fh($fd, ">&")
$self->{"fh"}{$fd} $self->_fh($fd, ">&")
Truth table0%0/3
A ||= B
A= $self->{"fh"}{$fd}B= $self->_fh($fd, ">&")
ABresult
1X1
011
000
45 0 $self->{loop}->watch_io(handle => $fh, on_write_ready => $cb);
46 }
47  
48 51 ▶sub remove {
Subroutine remove: called CC=2 SCAR=7.0
Pod undocumented
49 51 my ($self, $fd) = @_;
50 51 ▶ my $fh = delete $self->{fh}{$fd} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = delete $self->{"fh"}{$fd} or return 51 0 51
51 $self->{loop}->unwatch_io(
52 51 handle => $fh, on_read_ready => 1, on_write_ready => 1);
53 51 close $fh;
54 51 return;
55 }
56  
57 3 ▶sub timer {
Subroutine timer: called CC=1 SCAR=0.0
Pod undocumented
58 3 my ($self, $after, $cb) = @_;
59 3 return $self->{loop}->watch_time(after => $after, code => $cb);
60 }
61  
62 # A pending future of this ecosystem's native class.
63 #
64 # IO::Async before 0.805 stores the loop by poking $future->{loop}, and Future
65 # 0.50+ no longer implements a Future as a hashref - so on that pairing (which
66 # a smoker will happily assemble: IO::Async 0.801 with a current Future)
67 # $loop->new_future dies "Not a HASH reference" before it returns anything.
68 # That is IO::Async's business, not ours, but it is not worth dying over: fall
69 # back to a plain Future, which is the same class from the caller's side and
70 # differs only in not carrying its own ->await (we await on the loop anyway).
71 4 ▶sub _native_future {
Subroutine _native_future: called CC=2 SCAR=6.9
72 4 my ($self) = @_;
73 4 my $f = eval { $self->{loop}->new_future };
74 4 ▶ return $f if $f;
Branch: 1 branch
BranchTrueFalseTotal
if $f 3 1 4
75 1 require Future;
76 1 return Future->new;
77 }
78  
79 # native future for this ecosystem (IO::Async's own); the phase-2 pool uses the
80 # canonical DBIx::Loop::Future directly and does not call this yet.
81 0 ▶sub new_future { $_[0]->_native_future }
Subroutine new_future: not called CC=1 SCAR=6.9
Pod undocumented
82  
83 # bridge a DBIx::Loop::Future to an IO::Async Future
84 4 ▶sub to_native {
Subroutine to_native: called CC=1 SCAR=0.0
Pod undocumented
85 4 my ($self, $future) = @_;
86 4 my $nf = $self->_native_future;
87 4 ▶ $future->on_ready(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
88 4 my ($f) = @_;
89 4 ▶ $f->is_done ? $nf->done($f->get) : $nf->fail($f->failure);
Branch: 1 branch
BranchTrueFalseTotal
$f->is_done ? : 3 1 4
90 4 });
91 4 return $nf;
92 }
93  
94 # run the loop until $future is ready (for a blocking ->get in tests / scripts)
95 701 ▶sub await {
Subroutine await: called CC=2 SCAR=6.9
Pod undocumented
96 701 my ($self, $future) = @_;
97 701 ▶ $self->{loop}->loop_once until $future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
$future->is_ready or do { $self->{"loop"}->loop_once } 701 773 1474
98 701 return $future;
99 }
100  
101 18 1;
102  
103 __END__
« blib/lib/DBIx/Loop.pm ↑ Summary blib/arch/DBIx/Loop/Install/Files.pm »