This repository has been archived by the owner on Jan 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
test_worker.php
300 lines (257 loc) · 7.66 KB
/
test_worker.php
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
/*
* Gearman PHP Extension
*
* Copyright (C) 2008 James M. Luedke <[email protected]>,
* Eric Day <[email protected]>
* All rights reserved.
*
* Use and distribution licensed under the PHP license. See
* the LICENSE file in this directory for full text.
*/
/* worker */
$worker = gearman_worker_create();
if (! is_object($worker))
{
echo "gearman_worker_create() FAILED\n";
exit(0);
}
else
echo "gearman_worker_create() pass\n";
/*
if(! gearman_worker_add_server($worker))
{
echo "gearman_worker_add_server() FAILED\n";
exit(0);
}
echo "gearman_worker_add_server() pass\n";
*/
if(! gearman_worker_add_servers($worker, "127.0.0.1:4730"))
{
echo "gearman_worker_add_servers() FAILED\n";
exit(0);
}
echo "gearman_worker_add_servers() pass\n";
/*
$worker_new = gearman_worker_clone($worker);
if (! is_object($worker_new))
{
echo "gearman_worker_clone() FAILED\n";
exit(0);
}
else
echo "gearman_worker_clone() pass\n";
unset($worker_new);
echo "unset worker pass\n";
$ret = gearman_worker_error($worker);
if ($ret != GEARMAN_SUCCESS)
{
echo "gearman_worker_error() FAILED\n";
exit(0);
}
else
echo "gearman_worker_error() pass\n";
if(! gearman_worker_set_options($worker, GEARMAN_WORKER_NON_BLOCKING, 1))
{
echo "gearman_worker_set_options() FAILED\n";
exit(0);
}
gearman_worker_set_options($worker, GEARMAN_WORKER_NON_BLOCKING, 0);
echo "gearman_worker_set_options() pass\n";
if(! gearman_worker_register($worker, "test_one"))
{
echo "gearman_worker_register() FAILED\n";
exit(0);
}
gearman_worker_register($worker, "test_two");
gearman_worker_register($worker, "test_three");
echo "gearman_worker_register() pass\n";
if(! gearman_worker_unregister($worker, "test_three"))
{
echo "gearman_worker_unregister() FAILED\n";
exit(0);
}
echo "gearman_worker_unregister() pass\n";
if(! gearman_worker_unregister_all($worker))
{
echo "gearman_worker_unregister_all() FAILED\n";
exit(0);
}
echo "gearman_worker_unregister_all() pass\n";
gearman_worker_echo($worker, "gearman_worker_echo");
echo "gearman_worker_echo() pass\n";
*/
if (! gearman_worker_add_function($worker, "test_worker", "test_worker", "test_worker"))
{
echo "gearman_worker_add_function() FAILED\n";
exit(0);
}
gearman_worker_add_function($worker, "exit_test", "exit_test", "exit_test");
gearman_worker_add_function($worker, "test_gearman_job", "test_gearman_job", "test_gearman_job");
gearman_worker_add_function($worker, "test_set_callback_fn", "test_set_callback_fn", "test_set_callback_fn");
gearman_worker_add_function($worker, "test_tasks", "test_tasks", "test_tasks");
gearman_worker_add_function($worker, "test_tasks_background", "test_tasks_background", "test_tasks_background");
gearman_worker_add_function($worker, "test_tasks_high", "test_tasks_high", "test_tasks_high");
gearman_worker_add_function($worker, "test_tasks_low", "test_tasks_low", "test_tasks_low");
gearman_worker_add_function($worker, "test_tasks_high_background", "test_tasks_high_background", "test_tasks_high_background");
gearman_worker_add_function($worker, "test_tasks_low_background", "test_tasks_low_background", "test_tasks_low_background");
gearman_worker_add_function($worker, "test_gearman_job_status", "test_gearman_job_status", "test_status");
gearman_worker_add_function($worker, "test_gearman_job_complete", "test_gearman_job_complete", "test_complete");
gearman_worker_add_function($worker, "test_gearman_job_exception", "test_gearman_job_exception", "test_exception");
gearman_worker_add_function($worker, "test_gearman_job_fail", "test_gearman_job_fail", "test_fail");
echo "gearman_worker_add_function() pass\n";
echo "Starting Worker...\n";
$numb_test = 5;
$do_loop= true;
while ($do_loop)
{
if (! gearman_worker_work($worker))
{
echo "gearman_worker_work: FAILED\n";
exit(0);
}
}
echo "gearman_worker_work() pass\n";
/*
* Job Functions
*/
/* XXX finish this up once the gearman objects are added
$job = gearman_job_create();
if (! is_object($job))
{
echo "gearman_job_create() FAILED\n";
exit(0);
}
else
echo "gearman_job_create() pass\n";
$data = "job_data";
gearman_job_data($job, $data);
echo "gearman_job_data() pass\n";
gearman_job_warning($job, "gearman_job_warning");
echo "gearman_job_warning() pass\n";
gearman_job_status($job, 1, 2);
gearman_job_status($job, 2, 2);
echo "gearman_job_status() pass\n";
gearman_job_complete($job, "gearman_job_complete");
echo "gearman_job_complete() pass\n";
gearman_job_exception($job, "gearman_job_exception");
echo "gearman_job_exception() pass\n";
geaman_job_fail($job, "gearman_job_fail");
echo "gearman_job_fail() pass\n";
$job_handle= gearman_job_handle($job);
if (! is_string($job_handle))
{
echo "gearman_job_handle() FAILED\n";
exit(0);
}
else
echo "gearman_job_handle() pass\n";
*/
/*
* Test Functions
*/
function exit_test($job, $data)
{
global $do_loop;
echo "Done Working\n";
$do_loop=false;
}
function test_gearman_job_fail($job, $data)
{
gearman_job_fail($job);
echo "gearman_job_fail() pass\n";
}
function test_gearman_job_exception($job, $data)
{
gearman_job_exception($job, "test_exception");
echo "gearman_job_exception() pass\n";
}
function test_gearman_job_complete($job, $data)
{
gearman_job_complete($job, "test_complete");
echo "gearman_job_complete() pass\n";
}
function test_gearman_job_status($job, $data)
{
gearman_job_send_data($job, "test data");
gearman_job_send_status($job, 1, 4);
sleep(1);
gearman_job_send_status($job, 2, 4);
sleep(1);
gearman_job_send_status($job, 3, 4);
sleep(1);
gearman_job_send_status($job, 4, 4);
sleep(1);
echo "gearman_send_job_status() pass\n";
}
function test_worker($job, $data=NULL)
{
return $job->workload();
}
function test_gearman_job($job, $data)
{
echo "Testing Job Functions\n";
$job_handle = gearman_job_handle($job);
echo "\tgearman_job_handle() pass - $job_handle\n";
$workload = gearman_job_workload($job);
echo "\tgearman_job_workload() pass - $workload\n";
$workload_size = gearman_job_workload_size($job);
echo "\tgearman_job_workload_size() pass - $workload_size\n";
$fname = gearman_job_function_name($job);
echo "\tgearman_job_function_name() pass - $fname\n";
return "test_set_callback_fn";
}
function test_set_callback_fn($job, $data)
{
gearman_job_send_status($job, 1, 1);
echo "\tgearman_job_send_status() pass\n";
sleep(1);
gearman_job_send_warning($job, "test_set_callback_fn warning");
echo "\tgearman_job_send_warning() pass\n";
sleep(1);
gearman_job_send_exception($job, "test_set_callback_fn exception");
echo "\tgearman_job_send_exception() pass\n";
sleep(1);
/* $job->set_return(GEARMAN_WORK_FAIL); == $job->fail() == */
if(! gearman_job_send_fail($job))
echo "\tgearman_job_send_fail() FAILED\n";
else
echo "\tgearman_job_send_fail() pass\n";
sleep(1);
}
function test_tasks($job, $data)
{
$job->sendData("foobar");
gearman_job_send_status($job, 1, 1);
sleep(2);
return "test_tasks";
}
function test_tasks_background($job, $data)
{
echo "\ttest_tasks_background() pass\n";
gearman_job_send_status($job, 1, 2);
sleep(4);
gearman_job_send_status($job, 2, 2);
return "done";
}
function test_tasks_high($job, $data)
{
echo "\ttest_task_high() pass\n";
sleep(1);
}
function test_tasks_low($job, $data)
{
echo "\ttest_task_low() pass\n";
sleep(1);
}
function test_tasks_high_background($job, $data)
{
echo "\ttest_task_high_background() pass\n";
sleep(1);
}
function test_tasks_low_background($job, $data)
{
echo "\ttest_task_low_background() pass\n";
sleep(1);
}
?>