View | Details | Raw Unified | Return to bug 30181
Collapse All | Expand All

(-)a/t/db_dependent/Koha/BackgroundJobs.t (-6 / +11 lines)
Lines 41-48 my $net_stomp = Test::MockModule->new('Net::Stomp'); Link Here
41
$net_stomp->mock( 'send_with_receipt', sub { return 1 } );
41
$net_stomp->mock( 'send_with_receipt', sub { return 1 } );
42
42
43
my $background_job_module = Test::MockModule->new('Koha::BackgroundJob');
43
my $background_job_module = Test::MockModule->new('Koha::BackgroundJob');
44
$background_job_module->mock( '_derived_class',
44
$background_job_module->mock(
45
    sub { t::lib::Koha::BackgroundJob::BatchTest->new } );
45
    'type_to_class_mapping',
46
    sub {
47
        return { batch_test => 't::lib::Koha::BackgroundJob::BatchTest' };
48
    }
49
);
46
50
47
my $data     = { a => 'aaa', b => 'bbb' };
51
my $data     = { a => 'aaa', b => 'bbb' };
48
my $job_size = 10;
52
my $job_size = 10;
Lines 64-81 is( $new_job->size, $job_size, 'job size retrieved correctly' ); Link Here
64
is( $new_job->status, "new",        'job has not started yet, status is new' );
68
is( $new_job->status, "new",        'job has not started yet, status is new' );
65
is( $new_job->type,   "batch_test", 'job type retrieved from ->job_type' );
69
is( $new_job->type,   "batch_test", 'job type retrieved from ->job_type' );
66
70
71
# FIXME: This behavior doesn't seem correct. It shouldn't be the background job's
72
#        responsibility to return 'undef'. Some higher-level check should raise a
73
#        proper exception.
67
# Test cancelled job
74
# Test cancelled job
68
$new_job->status('cancelled')->store;
75
$new_job->status('cancelled')->store;
69
my $processed_job =
76
my $processed_job = $new_job->process;
70
  t::lib::Koha::BackgroundJob::BatchTest->process( { job_id => $new_job->id } );
71
is( $processed_job, undef );
77
is( $processed_job, undef );
72
$new_job->discard_changes;
78
$new_job->discard_changes;
73
is( $new_job->status, "cancelled", "A cancelled job has not been processed" );
79
is( $new_job->status, "cancelled", "A cancelled job has not been processed" );
74
80
75
# Test new job to process
81
# Test new job to process
76
$new_job->status('new')->store;
82
$new_job->status('new')->store;
77
$new_job =
83
$new_job = $new_job->process;
78
  t::lib::Koha::BackgroundJob::BatchTest->process( { job_id => $new_job->id } );
79
is( $new_job->status,             "finished", 'job is new finished!' );
84
is( $new_job->status,             "finished", 'job is new finished!' );
80
is( scalar( @{ $new_job->messages } ), 10,    '10 messages generated' );
85
is( scalar( @{ $new_job->messages } ), 10,    '10 messages generated' );
81
is_deeply(
86
is_deeply(
(-)a/t/lib/Koha/BackgroundJob/BatchTest.pm (-15 / +12 lines)
Lines 30-72 sub job_type { Link Here
30
sub process {
30
sub process {
31
    my ( $self, $args ) = @_;
31
    my ( $self, $args ) = @_;
32
32
33
    my $job = Koha::BackgroundJobs->find( $args->{job_id} );
33
    # FIXME: This should happen when $self->SUPER::process is called instead
34
34
    return
35
    if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) {
35
      unless $self->status ne 'cancelled';
36
        return;
37
    }
38
36
39
    my $job_progress = 0;
37
    my $job_progress = 0;
40
    $job->started_on(dt_from_string)
38
    $self->started_on(dt_from_string)
41
        ->progress($job_progress)
39
        ->progress($job_progress)
42
        ->status('started')
40
        ->status('started')
43
        ->store;
41
        ->store;
44
42
45
    my $report = {
43
    my $report = {
46
        total_records => $job->size,
44
        total_records => $self->size,
47
        total_success => 0,
45
        total_success => 0,
48
    };
46
    };
49
    my @messages;
47
    my @messages;
50
    for my $i ( 0 .. $job->size - 1 ) {
48
    for my $i ( 0 .. $self->size - 1 ) {
51
49
52
        last if $job->get_from_storage->status eq 'cancelled';
50
        last if $self->get_from_storage->status eq 'cancelled';
53
51
54
        push @messages, {
52
        push @messages, {
55
            type => 'success',
53
            type => 'success',
56
            i => $i,
54
            i => $i,
57
        };
55
        };
58
        $report->{total_success}++;
56
        $report->{total_success}++;
59
        $job->progress( ++$job_progress )->store;
57
        $self->progress( ++$job_progress )->store;
60
    }
58
    }
61
59
62
    my $job_data = decode_json $job->data;
60
    my $job_data = decode_json $self->data;
63
    $job_data->{messages} = \@messages;
61
    $job_data->{messages} = \@messages;
64
    $job_data->{report} = $report;
62
    $job_data->{report} = $report;
65
63
66
    $job->ended_on(dt_from_string)
64
    $self->ended_on(dt_from_string)
67
        ->data(encode_json $job_data);
65
        ->data(encode_json $job_data);
68
    $job->status('finished') if $job->status ne 'cancelled';
66
    $self->status('finished') if $self->status ne 'cancelled';
69
    $job->store;
67
    $self->store;
70
}
68
}
71
69
72
sub enqueue {
70
sub enqueue {
73
- 

Return to bug 30181