root/trunk/t/coalesce.t @ 164

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

PostgreSQL patch from clkao

Line 
1# -*-perl-*-
2
3use strict;
4use warnings;
5
6require 't/lib/db-common.pl';
7
8use TheSchwartz;
9use Test::More tests => 14 * 3;
10
11run_tests(14, sub {
12    my $client = test_client(dbs => ['ts1']);
13
14    my @keys = qw(foo bar baz);
15    my $n = 0;
16    for (1..10) {
17        my $key = $keys[$n++ % 3];
18        my $job = TheSchwartz::Job->new(
19                                        funcname => 'Worker::CoalesceTest',
20                                        arg      => { key => $key, num => $_ },
21                                        coalesce => $key
22                                        );
23        my $h = $client->insert($job);
24        ok($h, "inserted $h ($_ = $key)");
25    }
26
27    $client->reset_abilities;
28    $client->can_do("Worker::CoalesceTest");
29
30    Worker::CoalesceTest->set_client($client);
31
32    for (1..3) {
33        my $rv = eval { $client->work_once; };
34        ok($rv, "did stuff");
35    }
36    my $rv = eval { $client->work_once; };
37    ok(!$rv, "nothing to do now");
38
39    teardown_dbs('ts1');
40});
41
42############################################################################
43package Worker::CoalesceTest;
44use base 'TheSchwartz::Worker';
45
46my $client;
47sub set_client { $client = $_[1]; }
48
49sub work {
50    my ($class, $job) = @_;
51    my $args = $job->arg;
52
53    my $key = $args->{key};
54    $job->completed;
55
56    if ($key eq "foo") {
57        while (my $job = $client->find_job_with_coalescing_prefix("Worker::CoalesceTest", "f")) {
58            $job->completed;
59        }
60    } else {
61        while (my $job = $client->find_job_with_coalescing_value("Worker::CoalesceTest", $key)) {
62            $job->completed;
63        }
64    }
65}
66
67sub keep_exit_status_for { 20 }  # keep exit status for 20 seconds after on_complete
68
69sub grab_for { 10 }
70
71sub max_retries { 1 }
72
73sub retry_delay { my $class = shift; my $fails = shift; return 2 ** $fails; }
74
Note: See TracBrowser for help on using the browser.