From 291268f3185cb48b6498a9d9121d9567bc0d3419 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Fri, 19 Jan 2024 09:36:23 +0000 Subject: [PATCH] Bug 35843: Correct invalid exception Test plan: Run background job tests. Especially the changed one. Signed-off-by: Marcel de Rooy Signed-off-by: Magnus Enger All test pass when running this: prove t/db_dependent/Koha/BackgroundJobs* Signed-off-by: Martin Renvoize Signed-off-by: Katrin Fischer --- Koha/BackgroundJob.pm | 4 ++-- Koha/BackgroundJob/BatchUpdateItem.pm | 5 +++-- t/db_dependent/Koha/BackgroundJob.t | 15 ++++++++++++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Koha/BackgroundJob.pm b/Koha/BackgroundJob.pm index 7c2f735ada..579b0f9327 100644 --- a/Koha/BackgroundJob.pm +++ b/Koha/BackgroundJob.pm @@ -140,10 +140,10 @@ sub enqueue { my $encoded_args = Encode::encode_utf8( $json_args ); # FIXME We should better leave this to Net::Stomp? my $destination = sprintf( "/queue/%s-%s", $namespace, $job_queue ); $conn->send_with_receipt( { destination => $destination, body => $encoded_args, persistent => 'true' } ) - or Koha::Exceptions::Exception->throw('Job has not been enqueued'); + or Koha::Exceptions::BackgroundJob->throw('Job has not been enqueued'); } catch { $self->status('failed')->store; - if ( ref($_) eq 'Koha::Exceptions::Exception' ) { + if ( ref($_) eq 'Koha::Exceptions::BackgroundJob' ) { $_->rethrow; } else { warn sprintf "The job has not been sent to the message broker: (%s)", $_; diff --git a/Koha/BackgroundJob/BatchUpdateItem.pm b/Koha/BackgroundJob/BatchUpdateItem.pm index 8d4a917c9a..63f6f71d40 100644 --- a/Koha/BackgroundJob/BatchUpdateItem.pm +++ b/Koha/BackgroundJob/BatchUpdateItem.pm @@ -29,6 +29,7 @@ use Koha::DateUtils qw( dt_from_string ); use Koha::SearchEngine::Indexer; use Koha::Items; use Koha::UI::Table::Builder::Items; +use Koha::Exceptions::BackgroundJob; use base 'Koha::BackgroundJob'; @@ -137,8 +138,8 @@ Enqueue the new job sub enqueue { my ( $self, $args ) = @_; - # TODO Raise exception instead - return unless exists $args->{record_ids}; + Koha::Exceptions::BackgroundJob->throw('Job has not been enqueued') + unless $args && exists $args->{record_ids}; my @record_ids = @{ $args->{record_ids} }; diff --git a/t/db_dependent/Koha/BackgroundJob.t b/t/db_dependent/Koha/BackgroundJob.t index 4f1f4d9bbe..2d3c33e41b 100755 --- a/t/db_dependent/Koha/BackgroundJob.t +++ b/t/db_dependent/Koha/BackgroundJob.t @@ -26,6 +26,7 @@ use Test::Exception; use Koha::Database; use Koha::BackgroundJobs; use Koha::BackgroundJob::BatchUpdateItem; +use Koha::BackgroundJob::MARCImportCommitBatch; use t::lib::Mocks; use t::lib::Mocks::Logger; @@ -69,10 +70,22 @@ subtest '_derived_class() tests' => sub { subtest 'enqueue() tests' => sub { - plan tests => 8; + plan tests => 10; $schema->storage->txn_begin; + # Enqueue without args + throws_ok { Koha::BackgroundJob::BatchUpdateItem->new->enqueue } + 'Koha::Exceptions::BackgroundJob', + 'Enqueue BatchUpdateItem without data throws exception'; + + # The following test needs a mock to trigger the exception + my $mock = Test::MockModule->new('Net::Stomp')->mock( 'send_with_receipt', 0 ); + throws_ok { Koha::BackgroundJob::MARCImportCommitBatch->new->enqueue } + 'Koha::Exceptions::BackgroundJob', + 'Enqueue MARCImportCommitBatch with mock throws exception'; + $mock->unmock('send_with_receipt'); + # FIXME: This all feels we need to do it better... my $job_id = Koha::BackgroundJob::BatchUpdateItem->new->enqueue( { record_ids => [ 1, 2 ] } ); my $job = Koha::BackgroundJobs->find($job_id)->_derived_class;