From cab526116ffc1c4e48da1ea5a85b45af213588a3 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 Content-Type: text/plain; charset=utf-8 Test plan: Run background job tests. Especially the changed one. Signed-off-by: Marcel de Rooy --- 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; -- 2.30.2