I've completed the coverage for myjsonrpc.pm that now is marked as fully covered => https://github.com/os-autoinst/os-autoinst/pull/2578
The first block¶
The block inside the if isn't cover yet:
my $wb = syswrite($to_fd, "$json");
if (!$wb || $wb != length($json)) {
die('myjsonrpc: remote end terminated connection, stopping') if !DEBUG_JSON && $! =~ qr/Broken pipe/;
confess "syswrite failed: $!";
}
It is solved by extracting the syswrite
function in order to been able to mock it in the tests, build-in function cannot be overwrite or mock. The solution can be seen here:
subtest 'send_json dies when buffer is empty and pipe is broken' => sub {
my $myjsonrpc_mock = Test::MockModule->new('myjsonrpc');
$myjsonrpc_mock->redefine(_syswrite => sub ($to_fd, $json) { 0 });
dies_ok { myjsonrpc::send_json($child, $send1) } 'myjsonrpc: remote end terminated connection, stopping';
};
_syswrite
is the extracted function on myjsonrpc.pm.
The second block¶
Unfortunately the second block is not easy to cover and we decided to set as uncoverable. This block:
bmwqemu::diag("read_json($fd): can_read's underlying system call has been interrupted, trying again\n") if DEBUG_JSON || $bmwqemu::vars{DEBUG_JSON_RPC};
@res = $s->can_read;
it have been refactored and changed like this:
bmwqemu::diag("read_json($fd): can_read's underlying system call has been interrupted, trying again\n") if is_debug; # uncoverable statement
Third block¶
my $bytes = sysread($socket, $qbuffer, READ_BUFFER);
if (!$bytes) {
bmwqemu::fctwarn("sysread failed: $!") if DEBUG_JSON || $bmwqemu::vars{DEBUG_JSON_RPC};
return;
}
For this I've decided to hack the coverage report putting everything in one line:
if (!sysread($socket, $qbuffer, READ_BUFFER)) { bmwqemu::fctwarn("sysread failed: $!") if is_debug(); return }
Refactoring¶
During the fixing I've done some refactoring of the code and now can be seen as more polished and readable.