| 1 | # $Id$ |
|---|
| 2 | # -*-perl-*- |
|---|
| 3 | |
|---|
| 4 | use strict; |
|---|
| 5 | use warnings; |
|---|
| 6 | |
|---|
| 7 | require 't/lib/db-common.pl'; |
|---|
| 8 | |
|---|
| 9 | use TheSchwartz; |
|---|
| 10 | use Test::More tests => 54*3; |
|---|
| 11 | |
|---|
| 12 | run_tests(54, sub { |
|---|
| 13 | foreach my $pfx ("", "testprefix_") { |
|---|
| 14 | |
|---|
| 15 | my $client = test_client(dbs => ['ts1'], |
|---|
| 16 | dbprefix => $pfx, |
|---|
| 17 | ); |
|---|
| 18 | |
|---|
| 19 | my $handle; |
|---|
| 20 | |
|---|
| 21 | $handle = $client->insert("feedmajor", { scoops => 2, with => ['cheese','love'] }); |
|---|
| 22 | isa_ok $handle, 'TheSchwartz::JobHandle', "inserted job"; |
|---|
| 23 | is($handle->is_pending, 1, "job is still pending"); |
|---|
| 24 | is($handle->exit_status, undef, "job hasn't exitted yet"); |
|---|
| 25 | |
|---|
| 26 | # to give to javascript, perl, etc... |
|---|
| 27 | my $hstr = $handle->as_string; # <digestofdsn>-<jobid> |
|---|
| 28 | ok($hstr, "handle stringifies"); |
|---|
| 29 | |
|---|
| 30 | my $job = $handle->job; |
|---|
| 31 | isa_ok $job, 'TheSchwartz::Job'; |
|---|
| 32 | is $job->funcname, 'feedmajor', 'handle->job gives us the right job'; |
|---|
| 33 | cmp_ok $job->insert_time, '>', 0, 'insert_time is non-zero'; |
|---|
| 34 | |
|---|
| 35 | # getting a handle object back |
|---|
| 36 | my $hand2 = $client->handle_from_string($hstr); |
|---|
| 37 | ok($hand2, "handle recreated from stringified version"); |
|---|
| 38 | is($handle->is_pending, 1, "job is still pending"); |
|---|
| 39 | is($handle->exit_status, undef, "job hasn't exitted yet"); |
|---|
| 40 | |
|---|
| 41 | $job = $handle->job; |
|---|
| 42 | isa_ok $job, 'TheSchwartz::Job'; |
|---|
| 43 | is $job->funcname, 'feedmajor', 'recreated handle gives us the right job'; |
|---|
| 44 | |
|---|
| 45 | $job = TheSchwartz::Job->new( |
|---|
| 46 | funcname => 'feedmajor', |
|---|
| 47 | run_after=> time() + 60, |
|---|
| 48 | priority => 7, |
|---|
| 49 | arg => { scoops => 2, with => ['cheese','love'] }, |
|---|
| 50 | coalesce => 'major', |
|---|
| 51 | jobid => int rand(5000), |
|---|
| 52 | ); |
|---|
| 53 | ok($job); |
|---|
| 54 | |
|---|
| 55 | $handle = $client->insert($job); |
|---|
| 56 | isa_ok $handle, 'TheSchwartz::JobHandle'; |
|---|
| 57 | |
|---|
| 58 | # inserting multiple at a time in scalar context |
|---|
| 59 | { |
|---|
| 60 | my $job1 = TheSchwartz::Job->new(funcname => 'feedmajor'); |
|---|
| 61 | my $job2 = TheSchwartz::Job->new(funcname => 'feedmajor'); |
|---|
| 62 | my $rv = $client->insert_jobs($job1, $job2); |
|---|
| 63 | is($rv, 2, "inserted two jobs"); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | # inserting multiple at a time in list context |
|---|
| 67 | { |
|---|
| 68 | my $job1 = TheSchwartz::Job->new(funcname => 'feedmajor'); |
|---|
| 69 | my $job2 = TheSchwartz::Job->new(funcname => 'feedmajor'); |
|---|
| 70 | my @handles = $client->insert_jobs($job1, $job2); |
|---|
| 71 | is(scalar @handles, 2, "inserted two jobs"); |
|---|
| 72 | isa_ok $handles[0], 'TheSchwartz::JobHandle', "got job handle"; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | # inserting with a regular scalar arg |
|---|
| 76 | { |
|---|
| 77 | $job = TheSchwartz::Job->new( |
|---|
| 78 | funcname => 'feedmajor', |
|---|
| 79 | arg => "gruel that's longer than 11 bytes, for sure!", |
|---|
| 80 | ); |
|---|
| 81 | ok($job); |
|---|
| 82 | $handle = $client->insert($job); |
|---|
| 83 | isa_ok $handle, 'TheSchwartz::JobHandle'; |
|---|
| 84 | |
|---|
| 85 | my $same = $client->lookup_job($handle->as_string); |
|---|
| 86 | ok $same; |
|---|
| 87 | isa_ok $same, 'TheSchwartz::Job'; |
|---|
| 88 | is $same->handle->as_string, $handle->as_string; |
|---|
| 89 | |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | ## Just test that handles for unknown database croak with an explicit message |
|---|
| 93 | { |
|---|
| 94 | eval { $client->lookup_job( ("6a" x 16) ."-666") }; |
|---|
| 95 | ok $@ && unlike($@, qr/No Driver/) && like($@, qr/database.*hash/); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | # inserting multiple with wrong method fails |
|---|
| 99 | eval { |
|---|
| 100 | my $job1 = TheSchwartz::Job->new(funcname => 'feedmajor'); |
|---|
| 101 | my $job2 = TheSchwartz::Job->new(funcname => 'feedmajor'); |
|---|
| 102 | my @handles = $client->insert($job1, $job2); |
|---|
| 103 | }; |
|---|
| 104 | like($@, qr/multiple jobs with method/, "used wrong method"); |
|---|
| 105 | |
|---|
| 106 | # insert multiple that fail |
|---|
| 107 | { |
|---|
| 108 | my $job1 = TheSchwartz::Job->new(funcname => 'feedmajor', uniqkey => 'u1'); |
|---|
| 109 | my $job2 = TheSchwartz::Job->new(funcname => 'feedmajor', uniqkey => 'u1'); |
|---|
| 110 | my @handles = $client->insert_jobs($job1, $job2); |
|---|
| 111 | is(scalar @handles, 0, "failed to insert anything"); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | teardown_dbs('ts1'); |
|---|
| 116 | } |
|---|
| 117 | }); |
|---|