test_worker.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /*
  3. * Gearman PHP Extension
  4. *
  5. * Copyright (C) 2008 James M. Luedke <contact@jamesluedke.com>,
  6. * Eric Day <eday@oddments.org>
  7. * All rights reserved.
  8. *
  9. * Use and distribution licensed under the PHP license. See
  10. * the LICENSE file in this directory for full text.
  11. */
  12. /* worker */
  13. $worker = gearman_worker_create();
  14. if (! is_object($worker))
  15. {
  16. echo "gearman_worker_create() FAILED\n";
  17. exit(0);
  18. }
  19. else
  20. echo "gearman_worker_create() pass\n";
  21. /*
  22. if(! gearman_worker_add_server($worker))
  23. {
  24. echo "gearman_worker_add_server() FAILED\n";
  25. exit(0);
  26. }
  27. echo "gearman_worker_add_server() pass\n";
  28. */
  29. if(! gearman_worker_add_servers($worker, "127.0.0.1:4730"))
  30. {
  31. echo "gearman_worker_add_servers() FAILED\n";
  32. exit(0);
  33. }
  34. echo "gearman_worker_add_servers() pass\n";
  35. /*
  36. $worker_new = gearman_worker_clone($worker);
  37. if (! is_object($worker_new))
  38. {
  39. echo "gearman_worker_clone() FAILED\n";
  40. exit(0);
  41. }
  42. else
  43. echo "gearman_worker_clone() pass\n";
  44. unset($worker_new);
  45. echo "unset worker pass\n";
  46. $ret = gearman_worker_error($worker);
  47. if ($ret != GEARMAN_SUCCESS)
  48. {
  49. echo "gearman_worker_error() FAILED\n";
  50. exit(0);
  51. }
  52. else
  53. echo "gearman_worker_error() pass\n";
  54. if(! gearman_worker_set_options($worker, GEARMAN_WORKER_NON_BLOCKING, 1))
  55. {
  56. echo "gearman_worker_set_options() FAILED\n";
  57. exit(0);
  58. }
  59. gearman_worker_set_options($worker, GEARMAN_WORKER_NON_BLOCKING, 0);
  60. echo "gearman_worker_set_options() pass\n";
  61. if(! gearman_worker_register($worker, "test_one"))
  62. {
  63. echo "gearman_worker_register() FAILED\n";
  64. exit(0);
  65. }
  66. gearman_worker_register($worker, "test_two");
  67. gearman_worker_register($worker, "test_three");
  68. echo "gearman_worker_register() pass\n";
  69. if(! gearman_worker_unregister($worker, "test_three"))
  70. {
  71. echo "gearman_worker_unregister() FAILED\n";
  72. exit(0);
  73. }
  74. echo "gearman_worker_unregister() pass\n";
  75. if(! gearman_worker_unregister_all($worker))
  76. {
  77. echo "gearman_worker_unregister_all() FAILED\n";
  78. exit(0);
  79. }
  80. echo "gearman_worker_unregister_all() pass\n";
  81. gearman_worker_echo($worker, "gearman_worker_echo");
  82. echo "gearman_worker_echo() pass\n";
  83. */
  84. if (! gearman_worker_add_function($worker, "test_worker", "test_worker", "test_worker"))
  85. {
  86. echo "gearman_worker_add_function() FAILED\n";
  87. exit(0);
  88. }
  89. gearman_worker_add_function($worker, "exit_test", "exit_test", "exit_test");
  90. gearman_worker_add_function($worker, "test_gearman_job", "test_gearman_job", "test_gearman_job");
  91. gearman_worker_add_function($worker, "test_set_callback_fn", "test_set_callback_fn", "test_set_callback_fn");
  92. gearman_worker_add_function($worker, "test_tasks", "test_tasks", "test_tasks");
  93. gearman_worker_add_function($worker, "test_tasks_background", "test_tasks_background", "test_tasks_background");
  94. gearman_worker_add_function($worker, "test_tasks_high", "test_tasks_high", "test_tasks_high");
  95. gearman_worker_add_function($worker, "test_tasks_low", "test_tasks_low", "test_tasks_low");
  96. gearman_worker_add_function($worker, "test_tasks_high_background", "test_tasks_high_background", "test_tasks_high_background");
  97. gearman_worker_add_function($worker, "test_tasks_low_background", "test_tasks_low_background", "test_tasks_low_background");
  98. gearman_worker_add_function($worker, "test_gearman_job_status", "test_gearman_job_status", "test_status");
  99. gearman_worker_add_function($worker, "test_gearman_job_complete", "test_gearman_job_complete", "test_complete");
  100. gearman_worker_add_function($worker, "test_gearman_job_exception", "test_gearman_job_exception", "test_exception");
  101. gearman_worker_add_function($worker, "test_gearman_job_fail", "test_gearman_job_fail", "test_fail");
  102. echo "gearman_worker_add_function() pass\n";
  103. echo "Starting Worker...\n";
  104. $numb_test = 5;
  105. $do_loop= true;
  106. while ($do_loop)
  107. {
  108. if (! gearman_worker_work($worker))
  109. {
  110. echo "gearman_worker_work: FAILED\n";
  111. exit(0);
  112. }
  113. }
  114. echo "gearman_worker_work() pass\n";
  115. /*
  116. * Job Functions
  117. */
  118. /* XXX finish this up once the gearman objects are added
  119. $job = gearman_job_create();
  120. if (! is_object($job))
  121. {
  122. echo "gearman_job_create() FAILED\n";
  123. exit(0);
  124. }
  125. else
  126. echo "gearman_job_create() pass\n";
  127. $data = "job_data";
  128. gearman_job_data($job, $data);
  129. echo "gearman_job_data() pass\n";
  130. gearman_job_warning($job, "gearman_job_warning");
  131. echo "gearman_job_warning() pass\n";
  132. gearman_job_status($job, 1, 2);
  133. gearman_job_status($job, 2, 2);
  134. echo "gearman_job_status() pass\n";
  135. gearman_job_complete($job, "gearman_job_complete");
  136. echo "gearman_job_complete() pass\n";
  137. gearman_job_exception($job, "gearman_job_exception");
  138. echo "gearman_job_exception() pass\n";
  139. geaman_job_fail($job, "gearman_job_fail");
  140. echo "gearman_job_fail() pass\n";
  141. $job_handle= gearman_job_handle($job);
  142. if (! is_string($job_handle))
  143. {
  144. echo "gearman_job_handle() FAILED\n";
  145. exit(0);
  146. }
  147. else
  148. echo "gearman_job_handle() pass\n";
  149. */
  150. /*
  151. * Test Functions
  152. */
  153. function exit_test($job, $data)
  154. {
  155. global $do_loop;
  156. echo "Done Working\n";
  157. $do_loop=false;
  158. }
  159. function test_gearman_job_fail($job, $data)
  160. {
  161. gearman_job_fail($job);
  162. echo "gearman_job_fail() pass\n";
  163. }
  164. function test_gearman_job_exception($job, $data)
  165. {
  166. gearman_job_exception($job, "test_exception");
  167. echo "gearman_job_exception() pass\n";
  168. }
  169. function test_gearman_job_complete($job, $data)
  170. {
  171. gearman_job_complete($job, "test_complete");
  172. echo "gearman_job_complete() pass\n";
  173. }
  174. function test_gearman_job_status($job, $data)
  175. {
  176. gearman_job_send_data($job, "test data");
  177. gearman_job_send_status($job, 1, 4);
  178. sleep(1);
  179. gearman_job_send_status($job, 2, 4);
  180. sleep(1);
  181. gearman_job_send_status($job, 3, 4);
  182. sleep(1);
  183. gearman_job_send_status($job, 4, 4);
  184. sleep(1);
  185. echo "gearman_send_job_status() pass\n";
  186. }
  187. function test_worker($job, $data=NULL)
  188. {
  189. return $job->workload();
  190. }
  191. function test_gearman_job($job, $data)
  192. {
  193. echo "Testing Job Functions\n";
  194. $job_handle = gearman_job_handle($job);
  195. echo "\tgearman_job_handle() pass - $job_handle\n";
  196. $workload = gearman_job_workload($job);
  197. echo "\tgearman_job_workload() pass - $workload\n";
  198. $workload_size = gearman_job_workload_size($job);
  199. echo "\tgearman_job_workload_size() pass - $workload_size\n";
  200. $fname = gearman_job_function_name($job);
  201. echo "\tgearman_job_function_name() pass - $fname\n";
  202. return "test_set_callback_fn";
  203. }
  204. function test_set_callback_fn($job, $data)
  205. {
  206. gearman_job_send_status($job, 1, 1);
  207. echo "\tgearman_job_send_status() pass\n";
  208. sleep(1);
  209. gearman_job_send_warning($job, "test_set_callback_fn warning");
  210. echo "\tgearman_job_send_warning() pass\n";
  211. sleep(1);
  212. gearman_job_send_exception($job, "test_set_callback_fn exception");
  213. echo "\tgearman_job_send_exception() pass\n";
  214. sleep(1);
  215. /* $job->set_return(GEARMAN_WORK_FAIL); == $job->fail() == */
  216. if(! gearman_job_send_fail($job))
  217. echo "\tgearman_job_send_fail() FAILED\n";
  218. else
  219. echo "\tgearman_job_send_fail() pass\n";
  220. sleep(1);
  221. }
  222. function test_tasks($job, $data)
  223. {
  224. $job->sendData("foobar");
  225. gearman_job_send_status($job, 1, 1);
  226. sleep(2);
  227. return "test_tasks";
  228. }
  229. function test_tasks_background($job, $data)
  230. {
  231. echo "\ttest_tasks_background() pass\n";
  232. gearman_job_send_status($job, 1, 2);
  233. sleep(4);
  234. gearman_job_send_status($job, 2, 2);
  235. return "done";
  236. }
  237. function test_tasks_high($job, $data)
  238. {
  239. echo "\ttest_task_high() pass\n";
  240. sleep(1);
  241. }
  242. function test_tasks_low($job, $data)
  243. {
  244. echo "\ttest_task_low() pass\n";
  245. sleep(1);
  246. }
  247. function test_tasks_high_background($job, $data)
  248. {
  249. echo "\ttest_task_high_background() pass\n";
  250. sleep(1);
  251. }
  252. function test_tasks_low_background($job, $data)
  253. {
  254. echo "\ttest_task_low_background() pass\n";
  255. sleep(1);
  256. }
  257. ?>