gearman_tasks_integration_test_001.phpt 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. --TEST--
  2. Test Gearman worker methods
  3. --SKIPIF--
  4. <?php
  5. require_once('skipif.inc');
  6. require_once('skipifconnect.inc');
  7. ?>
  8. --FILE--
  9. <?php
  10. require_once('connect.inc');
  11. print "Start" . PHP_EOL;
  12. $job_name = uniqid();
  13. $pid = pcntl_fork();
  14. if ($pid == -1) {
  15. die("Could not fork");
  16. } else if ($pid > 0) {
  17. // Parent. This is the worker
  18. $worker = new GearmanWorker();
  19. print "addServer: " . var_export($worker->addServer($host, $port), true) . PHP_EOL;
  20. print "addFunction: " . var_export(
  21. $worker->addFunction(
  22. $job_name,
  23. function($job) {
  24. print "workload: " . var_export($job->workload(), true) . PHP_EOL;
  25. }
  26. ),
  27. true
  28. ) . PHP_EOL;
  29. for ($i = 0; $i < 6; $i++) {
  30. $worker->work();
  31. }
  32. print "unregister: " . var_export($worker->unregister($job_name), true) . PHP_EOL;
  33. // Wait for child
  34. $exit_status = 0;
  35. if (pcntl_wait($exit_status) <= 0) {
  36. print "pcntl_wait exited with error" . PHP_EOL;
  37. } else if (!pcntl_wifexited($exit_status)) {
  38. print "child exited with error" . PHP_EOL;
  39. }
  40. } else {
  41. //Child. This is the client. Don't echo anything here
  42. $client = new GearmanClient();
  43. if ($client->addServer($host, $port) !== true) {
  44. exit(1); // error
  45. };
  46. $tasks = [];
  47. $tasks[] = $client->addTask($job_name, "normal");
  48. $tasks[] = $client->addTaskBackground($job_name, "normalbg");
  49. $tasks[] = $client->addTaskHigh($job_name, 1);
  50. $tasks[] = $client->addTaskHighBackground($job_name, 2.0);
  51. $tasks[] = $client->addTaskLow($job_name, "low");
  52. $tasks[] = $client->addTaskLowBackground($job_name, true);
  53. $client->runTasks();
  54. if ($client->returnCode() != GEARMAN_SUCCESS) {
  55. exit(2); // error
  56. }
  57. exit(0);
  58. }
  59. print "Done";
  60. --EXPECTF--
  61. Start
  62. addServer: true
  63. addFunction: true
  64. workload: '2'
  65. workload: '1'
  66. workload: 'normalbg'
  67. workload: 'normal'
  68. workload: '1'
  69. workload: 'low'
  70. unregister: true
  71. Done