| 1 | # -*-perl-*- |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | |
|---|
| 6 | require 't/lib/db-common.pl'; |
|---|
| 7 | |
|---|
| 8 | use TheSchwartz; |
|---|
| 9 | use Test::More tests => 31*3; |
|---|
| 10 | |
|---|
| 11 | our $record_expected; |
|---|
| 12 | |
|---|
| 13 | run_tests(31, sub { |
|---|
| 14 | my $client = test_client(dbs => ['ts1']); |
|---|
| 15 | |
|---|
| 16 | # Define that we want to use priority selection |
|---|
| 17 | # limit batch size to 1 so we always process jobs in |
|---|
| 18 | # priority order |
|---|
| 19 | $client->set_prioritize(1); |
|---|
| 20 | $TheSchwartz::FIND_JOB_BATCH_SIZE = 1; |
|---|
| 21 | |
|---|
| 22 | for (1..10) { |
|---|
| 23 | my $job = TheSchwartz::Job->new( |
|---|
| 24 | funcname => 'Worker::PriorityTest', |
|---|
| 25 | arg => { num => $_ }, |
|---|
| 26 | ( $_ == 1 ? () : ( priority => $_ ) ), |
|---|
| 27 | ); |
|---|
| 28 | my $h = $client->insert($job); |
|---|
| 29 | ok($h, "inserted job (priority $_)"); |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | $client->reset_abilities; |
|---|
| 33 | $client->can_do("Worker::PriorityTest"); |
|---|
| 34 | |
|---|
| 35 | Worker::PriorityTest->set_client($client); |
|---|
| 36 | |
|---|
| 37 | for (1..10) { |
|---|
| 38 | $record_expected = 11 - $_ == 1 ? undef : 11 - $_; |
|---|
| 39 | my $rv = eval { $client->work_once; }; |
|---|
| 40 | ok($rv, "did stuff"); |
|---|
| 41 | } |
|---|
| 42 | my $rv = eval { $client->work_once; }; |
|---|
| 43 | ok(!$rv, "nothing to do now"); |
|---|
| 44 | |
|---|
| 45 | teardown_dbs('ts1'); |
|---|
| 46 | }); |
|---|
| 47 | |
|---|
| 48 | ############################################################################ |
|---|
| 49 | package Worker::PriorityTest; |
|---|
| 50 | use base 'TheSchwartz::Worker'; |
|---|
| 51 | use Test::More; |
|---|
| 52 | |
|---|
| 53 | use strict; |
|---|
| 54 | my $client; |
|---|
| 55 | sub set_client { $client = $_[1]; } |
|---|
| 56 | |
|---|
| 57 | sub work { |
|---|
| 58 | my ($class, $job) = @_; |
|---|
| 59 | my $priority = $job->priority; |
|---|
| 60 | |
|---|
| 61 | ok((!defined($main::record_expected) && (!defined($priority))) |
|---|
| 62 | || ($priority == $main::record_expected), "priority matches expected priority"); |
|---|
| 63 | $job->completed; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | sub keep_exit_status_for { 20 } # keep exit status for 20 seconds after on_complete |
|---|
| 67 | |
|---|
| 68 | sub grab_for { 10 } |
|---|
| 69 | |
|---|
| 70 | sub max_retries { 1 } |
|---|
| 71 | |
|---|
| 72 | sub retry_delay { my $class = shift; my $fails = shift; return 2 ** $fails; } |
|---|
| 73 | |
|---|