Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move check_asserted_screen into CommandHandler #2196

Merged
merged 1 commit into from Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 49 additions & 1 deletion OpenQA/Isotovideo/CommandHandler.pm
Expand Up @@ -9,12 +9,13 @@ use testapi 'diag';
use OpenQA::Isotovideo::Interface;
use Cpanel::JSON::XS;
use Mojo::File 'path';
use Time::HiRes qw(gettimeofday tv_interval);

use constant AUTOINST_STATUSFILE => 'autoinst-status.json';


# io handles for sending data to command server and backend
has [qw(cmd_srv_fd backend_fd answer_fd)] => undef;
has [qw(test_fd cmd_srv_fd backend_fd answer_fd)] => undef;

# the name of the current test (full name includes category prefix, eg. installation-)
has [qw(current_test_name current_test_full_name)];
Expand Down Expand Up @@ -53,6 +54,15 @@ has backend_requester => undef;
# whether the test has already been completed and whether it has died
has [qw(test_completed test_died)] => 0;

# the time of the last asserted screen
has [qw(last_check_seconds last_check_microseconds)] => 0;

sub new ($class, @args) {
my $self = $class->SUPER::new(@args);
$self->_update_last_check;
kalikiana marked this conversation as resolved.
Show resolved Hide resolved
return $self;
}

sub clear_tags_and_timeout ($self) {
$self->tags(undef);
$self->timeout(undef);
Expand Down Expand Up @@ -350,4 +360,42 @@ sub update_status_file ($self) {
rename $tmp, AUTOINST_STATUSFILE or die $!;
}

sub _calc_check_delta ($self) {
# an estimate of eternity
my $delta = $self->last_check_seconds ? tv_interval([$self->last_check_seconds, $self->last_check_microseconds]) : 100;
# sleep the remains of one second if $delta > 0
my $timeout = $delta > 0 ? 1 - $delta : 0;
$self->timeout($timeout < 0 ? 0 : $timeout);
return $delta;
}

sub _update_last_check ($self) {
my ($last_check_seconds, $last_check_microseconds) = gettimeofday;
$self->last_check_seconds($last_check_seconds);
$self->last_check_microseconds($last_check_microseconds);
}

sub check_asserted_screen ($self) {
if ($self->no_wait) {
# prevent CPU overload by waiting at least a little bit
$self->timeout(0.1);
}
else {
$self->_calc_check_delta;
# come back later, avoid too often called function
return if $self->timeout > 0.05;
}
$self->_update_last_check;
my $rsp = $bmwqemu::backend->_send_json({cmd => 'check_asserted_screen'}) || {};
# the test needs that information
$rsp->{tags} = $self->tags;
if ($rsp->{found} || $rsp->{timeout}) {
myjsonrpc::send_json($self->test_fd, {ret => $rsp});
$self->clear_tags_and_timeout();
}
else {
$self->_calc_check_delta unless $self->no_wait;
}
}

1;
39 changes: 2 additions & 37 deletions isotovideo
Expand Up @@ -75,7 +75,6 @@ use testapi ();
use Getopt::Long;
require IPC::System::Simple;
use POSIX qw(:sys_wait_h _exit);
use Time::HiRes qw(gettimeofday tv_interval sleep time);
use Try::Tiny;
use IO::Select;
use Mojo::File qw(curfile);
Expand Down Expand Up @@ -278,6 +277,7 @@ $testfd->write("GO\n");

$command_handler = OpenQA::Isotovideo::CommandHandler->new(
cmd_srv_fd => $cmd_srv_fd,
test_fd => $testfd,
backend_fd => $backend->process->channel_in,
);
$command_handler->on(tests_done => sub (@) {
Expand All @@ -287,39 +287,6 @@ $command_handler->on(tests_done => sub (@) {
$loop = 0;
});

my ($last_check_seconds, $last_check_microseconds) = gettimeofday;
sub _calc_check_delta () {
# an estimate of eternity
my $delta = $last_check_seconds ? tv_interval([$last_check_seconds, $last_check_microseconds], [gettimeofday]) : 100;
# sleep the remains of one second if $delta > 0
my $timeout = $delta > 0 ? 1 - $delta : 0;
$command_handler->timeout($timeout < 0 ? 0 : $timeout);
return $delta;
}

sub check_asserted_screen ($no_wait = undef) {
if ($no_wait) {
# prevent CPU overload by waiting at least a little bit
$command_handler->timeout(0.1);
}
else {
_calc_check_delta;
# come back later, avoid too often called function
return if $command_handler->timeout > 0.05;
}
($last_check_seconds, $last_check_microseconds) = gettimeofday;
my $rsp = $bmwqemu::backend->_send_json({cmd => 'check_asserted_screen'}) || {};
# the test needs that information
$rsp->{tags} = $command_handler->tags;
if ($rsp->{found} || $rsp->{timeout}) {
myjsonrpc::send_json($testfd, {ret => $rsp});
$command_handler->clear_tags_and_timeout();
}
else {
_calc_check_delta unless $no_wait;
}
}

$return_code = 0;

# enter the main loop: process messages from autotest, command server and backend
Expand All @@ -339,9 +306,7 @@ while ($loop) {
}
$command_handler->process_command($readable, $rsp);
}
if (defined($command_handler->tags)) {
check_asserted_screen($command_handler->no_wait);
}
$command_handler->check_asserted_screen if defined($command_handler->tags);
}

# tell the command server that it should no longer process isotovideo commands since we've
Expand Down
5 changes: 5 additions & 0 deletions t/19-isotovideo-command-processing.t
Expand Up @@ -347,6 +347,11 @@ subtest '_is_configured_to_pause_on_timeout' => sub {
is $result, 0, '_is_configured_to_pause_on_timeout returned 0';
};

subtest check_asserted_screen => sub {
$command_handler->check_asserted_screen;
ok($command_handler->timeout, 'Timeout was set');
};

done_testing;

END {
Expand Down