test_client.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. /* client */
  13. echo "# TESTING CLIENT INTERFACE\n";
  14. $client = gearman_client_create();
  15. if (! is_object($client))
  16. {
  17. echo "gearman_client_create() FAILED\n";
  18. exit(0);
  19. }
  20. else
  21. echo "gearman_client_create() pass\n";
  22. /*
  23. $client_new = gearman_client_clone($client);
  24. if (! is_object($client_new))
  25. {
  26. echo "gearman_client_clone() FAILED\n";
  27. exit(0);
  28. }
  29. else
  30. echo "gearman_client_clone() pass\n";
  31. unset($client_new);
  32. echo "unset client pass\n";
  33. */
  34. $ret= gearman_client_error($client);
  35. if ($ret != GEARMAN_SUCCESS)
  36. {
  37. echo "gearman_client_error() FAILED\n";
  38. exit(0);
  39. }
  40. else
  41. echo "gearman_client_error() pass\n";
  42. $ret= gearman_client_get_errno($client);
  43. if ($ret != 0)
  44. {
  45. echo "gearman_client_get_errno() FAILED\n";
  46. exit(0);
  47. }
  48. else
  49. echo "gearman_client_get_errno() pass\n";
  50. if (! gearman_client_add_options($client, GEARMAN_CLIENT_UNBUFFERED_RESULT))
  51. {
  52. echo "gearman_client_set_options() FAILED\n";
  53. exit(0);
  54. }
  55. gearman_client_remove_options($client, GEARMAN_CLIENT_UNBUFFERED_RESULT);
  56. echo "gearman_client_set_options() pass\n";
  57. /*
  58. if (! gearman_client_add_server($client))
  59. {
  60. echo "gearman_client_add_server() FAILED\n";
  61. exit(0);
  62. }
  63. echo "gearman_client_add_server() pass\n";
  64. */
  65. if (! gearman_client_add_servers($client, "127.0.0.1:4730"))
  66. {
  67. echo "gearman_client_add_servers() FAILED\n";
  68. exit(0);
  69. }
  70. echo "gearman_client_add_servers() pass\n";
  71. $value = gearman_client_do_normal($client, "test_worker", "gearman_client_do");
  72. if ($client->returnCode() != GEARMAN_SUCCESS)
  73. {
  74. echo "gearman_client_do_normal() FAILED\n";
  75. exit(0);
  76. }
  77. else
  78. echo "gearman_client_do_normal() pass -> $value\n";
  79. $job_handle = gearman_client_do_background($client, "test_worker", "gearman_client_do_background");
  80. if ($client->returnCode() != GEARMAN_SUCCESS)
  81. {
  82. echo "gearman_client_do_background() FAILED\n";
  83. exit(0);
  84. }
  85. else
  86. echo "gearman_client_do_background() pass -> $job_handle\n";
  87. $value = gearman_client_do_high($client, "test_worker", "gearman_client_do_high");
  88. if ($client->returnCode() != GEARMAN_SUCCESS)
  89. {
  90. echo "gearman_client_do_high() FAILED\n";
  91. exit(0);
  92. }
  93. else
  94. echo "gearman_client_do_high() pass -> $value\n";
  95. $value = gearman_client_do_low($client, "test_worker", "gearman_client_do_low");
  96. if ($client->returnCode() != GEARMAN_SUCCESS)
  97. {
  98. echo "gearman_client_do_low() FAILED\n";
  99. exit(0);
  100. }
  101. else
  102. echo "gearman_client_do_low() pass -> $value\n";
  103. $value = gearman_client_do_high_background($client, "test_worker", "gearman_client_do_high_background");
  104. if ($client->returnCode() != GEARMAN_SUCCESS)
  105. {
  106. echo "gearman_client_do_high_background() FAILED\n";
  107. exit(0);
  108. }
  109. else
  110. echo "gearman_client_do_high_background() pass -> $value\n";
  111. $value = gearman_client_do_low_background($client, "test_worker", "gearman_client_do_low_background");
  112. if ($client->returnCode() != GEARMAN_SUCCESS)
  113. {
  114. echo "gearman_client_do_low_background() FAILED\n";
  115. exit(0);
  116. }
  117. else
  118. echo "gearman_client_do_low_background() pass -> $value\n";
  119. $job_handle = gearman_client_do_background($client, "test_gearman_job_status", "test_gearman_job_status");
  120. if ($client->returnCode() != GEARMAN_SUCCESS)
  121. {
  122. echo "gearman_client_do_background() FAILED -> while checking status\n";
  123. exit(0);
  124. }
  125. sleep(1);
  126. list($is_known, $is_running, $numerator, $denominator) = gearman_client_job_status($client, $job_handle);
  127. if ($client->returnCode() != GEARMAN_SUCCESS || ! $is_known || ! $is_running || ! $numerator || ! $denominator)
  128. {
  129. echo "gearman_client_job_status() FAILED\n";
  130. }
  131. else
  132. echo "gearman_client_job_status() pass -> is_known: $is_known is_running: $is_running $numerator / $denominator\n";
  133. if ($job_handle != gearman_client_do_job_handle($client))
  134. {
  135. echo "gearman_client_do_job_handle() FAILED\n";
  136. exit(0);
  137. }
  138. else
  139. echo "gearman_client_do_job_handle() pass -> $job_handle\n";
  140. gearman_client_ping($client, "gearman_client_ping");
  141. echo "gearman_client_ping() pass\n";
  142. gearman_client_add_task($client, "test_gearman_job", "test_gearman_job", "test_gearman_job");
  143. echo "gearman_client_add_task() pass\n";
  144. /* run a task see if they all work */
  145. $res = gearman_client_run_tasks($client);
  146. echo "gearman_client_run_tasks() pass\n";
  147. /* clear all the callbacks so they dont mess with other test */
  148. gearman_client_clear_callbacks($client);
  149. echo "gearman_client_clear_callbacks() pass\n";
  150. /* set all of our callback functions */
  151. /*
  152. gearman_client_set_workload_fn($client, "test_gearman_client_set_workload_fn");
  153. echo "gearman_client_set_workload_fn() pass\n";
  154. */
  155. gearman_client_set_created_callback($client, "test_gearman_client_set_created_callback");
  156. // XXX gearman_client_set_data_fn($client, "test_gearman_client_set_data_fn");
  157. gearman_client_set_warning_callback($client, "test_gearman_client_set_warning_callback");
  158. gearman_client_set_status_callback($client, "test_gearman_client_set_status_callback");
  159. gearman_client_set_complete_callback($client, "test_gearman_client_set_complete_callback");
  160. gearman_client_set_exception_callback($client, "test_gearman_client_set_exception_callback");
  161. gearman_client_set_fail_callback($client, "test_gearman_client_set_fail_callback");
  162. gearman_client_add_task($client, "test_set_callback_callback", "test_set_callback_callback", "test_set_callback_callback");
  163. /* run a task see if they all work */
  164. $res = gearman_client_run_tasks($client);
  165. // gearman_client_clear_fn($client);
  166. /* test tasks interface */
  167. gearman_client_clear_callback($client);
  168. gearman_client_set_complete_callback($client, "test_gearman_tasks");
  169. gearman_client_add_task($client, "test_tasks", "test_tasks", "test_tasks");
  170. $res = gearman_client_run_tasks($client);
  171. gearman_client_clear_callback($client);
  172. # You can turn off auto task destruction by unsetting this flag on a gearman client.
  173. gearman_client_remove_options($client, GEARMAN_CLIENT_FREE_TASKS);
  174. $task = gearman_client_add_task_background($client, "test_tasks_background", "test_tasks_background", "test_tasks_background");
  175. gearman_client_set_status_callback($client, "test_gearman_task_status");
  176. $res = gearman_client_run_tasks($client);
  177. $job_handle = gearman_task_job_handle($task);
  178. gearman_client_add_task_status($client, $job_handle);
  179. echo "\tgearman_client_add_task_status() pass\n";
  180. $res = gearman_client_run_tasks($client);
  181. function test_gearman_task_status($task)
  182. {
  183. $is_known = gearman_task_is_known($task);
  184. if ($is_known)
  185. echo "\tgearman_task_is_known() pass\n";
  186. else
  187. echo "\tgearman_task_is_known() FAILED\n";
  188. sleep(1);
  189. $is_running = gearman_task_is_running($task);
  190. if ($is_running)
  191. echo "\tgearman_task_is_running() pass\n";
  192. else
  193. echo "\tgearman_task_is_running() pass\n";
  194. }
  195. echo "gearman_client_add_task_background() pass\n";
  196. gearman_client_clear_callback($client);
  197. gearman_client_add_task_high($client, "test_tasks_high", "test_tasks_high", "test_tasks_high");
  198. echo "gearman_client_add_task_high() pass\n";
  199. gearman_client_add_task_low($client, "test_tasks_low", "test_tasks_low", "test_tasks_low");
  200. echo "gearman_client_add_task_low() pass\n";
  201. gearman_client_add_task_high_background($client, "test_tasks_high_background", "test_tasks_high_background", "test_tasks_high_background");
  202. echo "gearman_client_add_task_high_background() pass\n";
  203. gearman_client_add_task_low_background($client, "test_tasks_low_background", "test_tasks_low_background", "test_tasks_low_background");
  204. echo "gearman_client_add_task_low_background() pass\n";
  205. $res = gearman_client_run_tasks($client);
  206. /* XXX add task high seems to break things
  207. */
  208. $value = gearman_client_do($client, "exit_test", "exit_test");
  209. echo "DONE $ret\n";
  210. /* XXX does not seem to call cb as i would expect
  211. $data = "gearman_job_exception";
  212. gearman_client_add_task($client, "test_gearman_job_exception", $data, NULL);
  213. $data = "gearman_job_fail";
  214. gearman_client_add_task($client, "test_gearman_job_fail", $data, NULL);
  215. */
  216. /* XXX can not test till I have the gearman low level functions done
  217. $task_new = gearman_task_create();
  218. gearman_task_fn_arg($task_new);
  219. echo "gearman_task_fn_arg() pass\n";
  220. */
  221. /*
  222. * Test Functions
  223. */
  224. function test_gearman_client_set_fail_callback($task)
  225. {
  226. echo "\tgearman_client_set_fail_callback() pass\n";
  227. }
  228. function test_gearman_client_set_exception_callback($task)
  229. {
  230. echo "\tgearman_client_set_exception_callback() pass\n";
  231. }
  232. function test_gearman_client_set_complete_callback($task)
  233. {
  234. echo "\tgearman_client_set_complete_callback() pass\n";
  235. }
  236. function test_gearman_client_set_status_callback($task)
  237. {
  238. echo "\tgearman_client_set_status_callback() pass\n";
  239. }
  240. function test_gearman_client_set_warning_callback($task)
  241. {
  242. echo "\tgearman_client_set_warning_callback() pass\n";
  243. }
  244. function test_gearman_client_set_data_callback($task)
  245. {
  246. echo "\tgearman_client_set_data_callback() pass\n";
  247. }
  248. function test_gearman_client_set_created_callback($task)
  249. {
  250. echo "\tgearman_client_set_created_callback() pass\n";
  251. }
  252. function test_gearman_client_set_workload_callback($task)
  253. {
  254. echo "\tgearman_client_set_workload_callback() pass\n";
  255. }
  256. function test_gearman_tasks($task)
  257. {
  258. $is_known= false;
  259. $is_running= false;
  260. echo "Testing Task Functions\n";
  261. $func = gearman_task_function_name($task);
  262. echo "\tgearman_task_function_name() pass - $func\n";
  263. $uuid = gearman_task_unique($task);
  264. echo "\tgearman_task_unique() pass - $uuid\n";
  265. $job_handle = gearman_task_job_handle($task);
  266. echo "\tgearman_task_job_handle() pass - $job_handle\n";
  267. $numerator = gearman_task_numerator($task);
  268. echo "\tgearman_task_numerator() pass - $numerator\n";
  269. $denominator = gearman_task_denominator($task);
  270. echo "\tgearman_task_denominator() pass - $denominator\n";
  271. $data = gearman_task_data($task);
  272. echo "\tgearman_task_data() pass - $data\n";
  273. $data_size = gearman_task_data_size($task);
  274. echo "\tgearman_task_data_size() pass - $data_size\n";
  275. /* XXX Not sure how to test task data yet
  276. gearman_task_take_data($task);
  277. echo "gearman_task_take_data() pass\n";
  278. gearman_task_send_data()
  279. gearman_task_recv_data()
  280. */
  281. }
  282. ?>