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