gearman_worker_016.phpt 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. --TEST--
  2. GearmanWorker::addFunction(), context param
  3. --SKIPIF--
  4. <?php if (!extension_loaded("gearman")) print "skip";
  5. require_once('skipifconnect.inc');
  6. ?>
  7. --FILE--
  8. <?php
  9. $host = 'localhost';
  10. $port = 4730;
  11. $job = 'AddFunctionArrCtxTest';
  12. $workload = '{"workload":"test"}';
  13. $client = new GearmanClient();
  14. $client->addServer($host, $port);
  15. $handle = $client->doBackground($job, $workload);
  16. $client->doBackground($job, $workload);
  17. $client->doBackground($job, $workload);
  18. $client->doBackground($job, $workload);
  19. print "GearmanWorker::doBackground() (OO): ".(preg_match('/^H:'.gethostname().':\d+$/', $handle) === 1? 'Success' : 'Failure').PHP_EOL;
  20. $worker = new TestWorker();
  21. print "GearmanWorker::work() (OO, array ctx): " .($worker->work() === true ? 'Success' : 'Failure') . PHP_EOL;
  22. print "GearmanWorker::work() (OO, array ctx): " .($worker->work() === true ? 'Success' : 'Failure') . PHP_EOL;
  23. print "GearmanWorker::work() (OO, array ctx): " .($worker->work() === true ? 'Success' : 'Failure') . PHP_EOL;
  24. print "OK";
  25. class TestWorker extends \GearmanWorker
  26. {
  27. public function __construct()
  28. {
  29. global $job;
  30. parent::__construct();
  31. $this->addServer();
  32. $this->addFunction($job, [$this, 'test'], ['firstArg' => 'firstValue']);
  33. }
  34. public function test($job, $context)
  35. {
  36. echo "Starting job {$job->workload()}". PHP_EOL;
  37. $firstArg = $context['firstArg'];
  38. echo "FirstArg is $firstArg" . PHP_EOL;
  39. }
  40. }
  41. ?>
  42. --EXPECT--
  43. GearmanWorker::doBackground() (OO): Success
  44. Starting job {"workload":"test"}
  45. FirstArg is firstValue
  46. GearmanWorker::work() (OO, array ctx): Success
  47. Starting job {"workload":"test"}
  48. FirstArg is firstValue
  49. GearmanWorker::work() (OO, array ctx): Success
  50. Starting job {"workload":"test"}
  51. FirstArg is firstValue
  52. GearmanWorker::work() (OO, array ctx): Success
  53. OK