gearman_client_integration_test_001.phpt 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. --TEST--
  2. --SKIPIF--
  3. <?php
  4. require_once('skipif.inc');
  5. require_once('skipifconnect.inc');
  6. ?>
  7. --FILE--
  8. <?php
  9. $host = 'localhost';
  10. $port = '4730';
  11. $job_name = uniqid();
  12. $pid = pcntl_fork();
  13. if ($pid == -1) {
  14. die("Could not fork");
  15. } else if ($pid > 0) {
  16. // Parent. This should be the Worker
  17. $worker = new GearmanWorker();
  18. $worker->addServer($host, $port);
  19. print "addFunction: " . var_export(
  20. $worker->addFunction(
  21. $job_name,
  22. function($job) {
  23. print "workload: " . var_export($job->workload(), true) . PHP_EOL;
  24. }
  25. ),
  26. true
  27. ) . PHP_EOL;
  28. for($i=0; $i<6; $i++) {
  29. $worker->work();
  30. }
  31. print "unregister: " . var_export($worker->unregister($job_name), true) . PHP_EOL;
  32. // Wait for child
  33. $exit_status = 0;
  34. if (pcntl_wait($exit_status) <= 0) {
  35. print "pcntl_wait exited with error" . PHP_EOL;
  36. } else if (!pcntl_wifexited($exit_status)) {
  37. print "child exited with error" . PHP_EOL;
  38. }
  39. print "OK" . PHP_EOL;
  40. } else {
  41. // Child. This is the Client
  42. $client = new GearmanClient();
  43. $client->addServer($host, $port);
  44. $job_types = ['doNormal', 'doHigh', 'doLow'];
  45. foreach ($job_types as $job_type) {
  46. $unique_key = "{$job_name}_{$job_type}";
  47. $workload = "Workload for $job_type";
  48. $handle = $client->$job_type($job_name, $workload, $unique_key);
  49. }
  50. // Background jobs can run into a race condition if they complete out of
  51. // order
  52. $job_types = ['doBackground', 'doHighBackground', 'doLowBackground'];
  53. foreach ($job_types as $job_type) {
  54. $unique_key = "{$job_name}_{$job_type}";
  55. $workload = "Workload for $job_type";
  56. $handle = $client->$job_type($job_name, $workload, $unique_key);
  57. do {
  58. usleep(10000);
  59. list($is_known, $is_running, $numerator, $denominator) =
  60. $client->jobStatus($handle);
  61. } while ($is_known === true || $is_running === true);
  62. }
  63. }
  64. ?>
  65. --EXPECT--
  66. addFunction: true
  67. workload: 'Workload for doNormal'
  68. workload: 'Workload for doHigh'
  69. workload: 'Workload for doLow'
  70. workload: 'Workload for doBackground'
  71. workload: 'Workload for doHighBackground'
  72. workload: 'Workload for doLowBackground'
  73. unregister: true
  74. OK