-
Notifications
You must be signed in to change notification settings - Fork 9
/
04-task.t
250 lines (197 loc) · 5.79 KB
/
04-task.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use strict;
use warnings;
use Storable;
use String::CRC32 ();
use Test::More;
use Test::Exception;
plan tests => 33;
use_ok("Gearman::Client");
use_ok("Gearman::Taskset");
use_ok("Gearman::Util");
my $mn = "Gearman::Task";
use_ok($mn);
can_ok(
$mn, qw/
add_hook
complete
data
exception
fail
final_fail
func handle hash
is_finished
mode
pack_submit_packet
run_hook
set_on_post_hooks
status
taskset
timeout
warning
wipe
/
);
my ($f, $arg) = qw/
foo
bar
/;
my %opt = (
uniq => rand(10),
on_complete => 1,
on_fail => 2,
on_exception => 3,
on_retry => undef,
on_status => 4,
retry_count => 6,
try_timeout => 7,
background => 1,
timeout => int(rand(10)),
);
throws_ok { $mn->new($f, \$arg, { $f => 1 }) } qr/Unknown option/,
"caught unknown option exception";
my $t = new_ok($mn, [$f, \$arg, {%opt}]);
is($t->func, $f, "func");
is(${ $t->{argref} }, $arg, "argref");
foreach (keys %opt) {
is($t->can($_) ? $t->$_ : $t->{$_}, $opt{$_}, $_);
}
is($t->{$_}, 0, $_) for qw/
is_finished
retries_done
/;
subtest "priority mode", sub {
plan tests => 21;
foreach my $p (qw/low normal high/) {
my $s = sprintf("submit_job%s", $p eq "normal" ? '' : '_' . $p);
my $t = new_ok($mn, [$f, undef, { priority => $p }]);
is($t->_priority(), $p, "$p priority");
is($t->mode(), $s, "mode of task in $p prioirty");
$t = new_ok($mn, [$f, undef, { background => 1, priority => $p }]);
is(
$t->mode(),
join('_', $s, "bg"),
"mode of background task in $p prioirty"
);
} ## end foreach my $p (qw/low normal high/)
{
my $s = "submit_job";
my $t = new_ok($mn, [$f, undef]);
is($t->mode(), $s, "mode of task without explicit priority");
$s .= "_high";
$t = new_ok($mn, [$f, undef, { high_priority => 1 }]);
is($t->mode(), $s, "mode of task with high_priority=1");
$t = new_ok($mn, [$f, undef, { background => 1, high_priority => 1 }]);
is(
$t->mode(),
join('_', $s, "bg"),
"mode of background task with high_prioirty=1"
);
}
};
my @h = qw/
on_post_hooks
on_complete
on_fail
on_retry
on_status
hooks
/;
subtest "wipe", sub {
plan tests => scalar(@h);
$t->{$_} = 1 for @h;
$t->wipe();
is($t->{$_}, undef, $_) for @h;
};
subtest "hook", sub {
plan tests => 4;
my $cb = sub { 2 * shift };
ok($t->add_hook($f, $cb), "add hook");
is($t->{hooks}->{$f}, $cb, "is hooks");
$t->run_hook($f, 2);
ok($t->add_hook($f), "add hook");
is($t->{hooks}->{$f}, undef, "no hook");
};
subtest "taskset", sub {
plan tests => 9;
is($t->taskset, undef, "taskset");
throws_ok { $t->taskset($f) } qr/not an instance of Gearman::Taskset/,
"caught taskset($f) exception";
my $c = new_ok("Gearman::Client");
my $ts = new_ok("Gearman::Taskset", [$c]);
ok($t->taskset($ts), "taskset true");
is($t->taskset(), $ts, "is taskset");
is($t->hash(), (String::CRC32::crc32($t->{uniq}) >> 16) & 0x7fff, "hash for uniq: $t->{uniq}");
$t->{uniq} = '-';
is($t->taskset(), $ts);
is($t->hash(), (String::CRC32::crc32($arg) >> 16) & 0x7fff, "hash for uniq: -");
};
subtest "fail", sub {
plan tests => 2 * scalar(@h) - 1;
$t->{is_finished} = 1;
is($t->fail(), undef, "fail returns undef");
$t->{is_finished} = undef;
$t->{on_retry} = sub { is(shift, $t->{retry_count}, "on_retry") };
$t->{retries_done} = 0;
$t->{retry_count} = 1;
$t->fail($f);
is($t->{retries_done}, $t->{retry_count}, "retries_done = retry_count");
$t->{is_finished} = undef;
$t->{on_fail} = sub { is(shift, $f, "on_fail") };
$t->final_fail($f);
is($t->{is_finished}, $f, "is_finished '$f'");
is($t->{$_}, undef, $_) for @h;
};
subtest "exception", sub {
plan tests => 2;
my $exc = Storable::freeze(\$f);
$t->{on_exception} = sub { is(shift, $f, "within on_exception") };
is($t->exception(\$exc), undef, "exception returns undef");
};
subtest "complete", sub {
plan tests => 2;
$t->{is_finished} = undef;
$t->{on_complete} = sub { is(shift, $f, "within on_complete") };
$t->complete($f);
is($t->{is_finished}, "complete", "is complete");
};
subtest "status", sub {
plan tests => 3;
$t->{is_finished} = undef;
$t->{on_status} = sub {
is(shift, $f, "func");
is(shift, $arg, "arg");
};
ok $t->status($f, $arg), "status true";
};
subtest "data", sub {
plan tests => 2;
$t->{is_finished} = undef;
$t->{on_data} = sub { is(shift, $f, "func") };
ok $t->data($f), "data true";
};
subtest "warning", sub {
plan tests => 2;
$t->{is_finished} = undef;
$t->{on_warning} = sub { is(shift, $f, "func") };
ok $t->warning($f), "warning true";
};
subtest "handle", sub {
plan tests => 2;
ok($t->handle($f), "handle true");
is($t->{handle}, $f, "is handle '$f'");
};
subtest "pack_submit_packet", sub {
plan tests => 5;
my $c = new_ok("Gearman::Client");
my $v = Gearman::Util::pack_req_command($t->mode,
join("\0", $t->func, $t->{uniq}, ${ $t->{argref} }));
is $t->pack_submit_packet($c), $v, "is pack_submit_packet(\$c) \$v";
is $t->pack_submit_packet(), $v, "is pack_submit_packet() \$v";
$v = Gearman::Util::pack_req_command($t->mode,
join("\0", $t->func, '', ''));
${ $t->{argref} } = undef;
$t->{uniq} = undef;
is $t->pack_submit_packet($c), $v, "is pack_submit_packet(\$c) \$v";
is $t->pack_submit_packet(), $v, "is pack_submit_packet() \$v";
};
done_testing();