root/trunk/t/priority.t @ 164

Revision 164, 1.9 kB (checked in by miyagawa, 7 months ago)

PostgreSQL patch from clkao

  • Property svn:keywords set to Id Revision
Line 
1# -*-perl-*-
2
3use strict;
4use warnings;
5
6require 't/lib/db-common.pl';
7
8use TheSchwartz;
9use Test::More tests => 31*3;
10
11our $record_expected;
12
13run_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############################################################################
49package Worker::PriorityTest;
50use base 'TheSchwartz::Worker';
51use Test::More;
52
53use strict;
54my $client;
55sub set_client { $client = $_[1]; }
56
57sub 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
66sub keep_exit_status_for { 20 }  # keep exit status for 20 seconds after on_complete
67
68sub grab_for { 10 }
69
70sub max_retries { 1 }
71
72sub retry_delay { my $class = shift; my $fails = shift; return 2 ** $fails; }
73
Note: See TracBrowser for help on using the browser.