gearman_tasks_integration_test_002.phpt 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. --TEST--
  2. Test Gearman worker methods
  3. --SKIPIF--
  4. <?php
  5. require_once('skipif.inc');
  6. require_once('skipifconnect.inc');
  7. require_once('skipifversion.inc');
  8. ?>
  9. --FILE--
  10. <?php
  11. require_once('connect.inc');
  12. print "Start" . PHP_EOL;
  13. $job_name = uniqid();
  14. $contexts = [
  15. "success",
  16. "fail",
  17. "exception"
  18. ];
  19. $pid = pcntl_fork();
  20. if ($pid == -1) {
  21. die("Could not fork");
  22. } else if ($pid == 0) {
  23. // Child. This is the worker.
  24. // Don't echo anything here
  25. $worker = new GearmanWorker();
  26. $worker->addServer($host, $port);
  27. $worker->addFunction(
  28. $job_name,
  29. function($job) {
  30. if ($job->workload() == "success") {
  31. return "done";
  32. } else if ($job->workload() == "exception") {
  33. $job->sendException("unhandled");
  34. return "exception";
  35. } else if ($job->workload() == "fail") {
  36. $job->sendFail();
  37. return "fail";
  38. }
  39. }
  40. );
  41. for ($i = 0; $i < count($contexts); $i++) {
  42. $worker->work();
  43. }
  44. $worker->unregister($job_name);
  45. exit(0);
  46. } else {
  47. //Parent. This is the client.
  48. $client = new GearmanClient();
  49. if ($client->addServer($host, $port) !== true) {
  50. exit(1); // error
  51. };
  52. $client->setCompleteCallback(function($task) {
  53. print "Complete: " . $task->data() . PHP_EOL;
  54. });
  55. $client->setDataCallback(function($task) {
  56. print "Data: " . $task->data() . PHP_EOL;
  57. });
  58. $client->setExceptionCallback(function($task) {
  59. print "Exception: " . $task->data() . PHP_EOL;
  60. });
  61. $client->setFailCallback(function($task) {
  62. print "Fail" . PHP_EOL;
  63. });
  64. $tasks = [];
  65. foreach ($contexts as $c) {
  66. $tasks[] = $client->addTask($job_name, $c);
  67. }
  68. $client->runTasks();
  69. print "returnCode: " . var_export($client->returnCode(), true) . PHP_EOL;
  70. print "clearCallbacks: " . var_export($client->clearCallbacks(), true) . PHP_EOL;
  71. // Wait for child
  72. $exit_status = 0;
  73. if (pcntl_wait($exit_status) <= 0) {
  74. print "pcntl_wait exited with error" . PHP_EOL;
  75. } else if (!pcntl_wifexited($exit_status)) {
  76. print "child exited with error" . PHP_EOL;
  77. }
  78. }
  79. print "Done";
  80. --EXPECTF--
  81. Start
  82. Exception: unhandled
  83. Fail
  84. Complete: done
  85. returnCode: 0
  86. clearCallbacks: true
  87. Done