DispatcherLoopTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\PubSub;
  11. use PredisTestCase;
  12. use Predis\Client;
  13. /**
  14. * @group realm-pubsub
  15. */
  16. class DispatcherLoopTest extends PredisTestCase
  17. {
  18. // ******************************************************************** //
  19. // ---- INTEGRATION TESTS --------------------------------------------- //
  20. // ******************************************************************** //
  21. /**
  22. * @group connected
  23. */
  24. public function testDispatcherLoopAgainstRedisServer()
  25. {
  26. $parameters = array(
  27. 'host' => REDIS_SERVER_HOST,
  28. 'port' => REDIS_SERVER_PORT,
  29. 'database' => REDIS_SERVER_DBNUM,
  30. // Prevents suite from handing on broken test
  31. 'read_write_timeout' => 2,
  32. );
  33. $options = array('profile' => REDIS_SERVER_VERSION);
  34. $producer = new Client($parameters, $options);
  35. $producer->connect();
  36. $consumer = new Client($parameters, $options);
  37. $consumer->connect();
  38. $dispatcher = new DispatcherLoop($consumer);
  39. $function01 = $this->getMock('stdClass', array('__invoke'));
  40. $function01->expects($this->exactly(2))
  41. ->method('__invoke')
  42. ->with($this->logicalOr(
  43. $this->equalTo('01:argument'),
  44. $this->equalTo('01:quit')
  45. ))
  46. ->will($this->returnCallback(function ($arg) use ($dispatcher) {
  47. if ($arg === '01:quit') {
  48. $dispatcher->stop();
  49. }
  50. }));
  51. $function02 = $this->getMock('stdClass', array('__invoke'));
  52. $function02->expects($this->once())
  53. ->method('__invoke')
  54. ->with('02:argument');
  55. $function03 = $this->getMock('stdClass', array('__invoke'));
  56. $function03->expects($this->never())
  57. ->method('__invoke');
  58. $dispatcher->attachCallback('function:01', $function01);
  59. $dispatcher->attachCallback('function:02', $function02);
  60. $dispatcher->attachCallback('function:03', $function03);
  61. $producer->publish('function:01', '01:argument');
  62. $producer->publish('function:02', '02:argument');
  63. $producer->publish('function:01', '01:quit');
  64. $dispatcher->run();
  65. $this->assertTrue($consumer->ping());
  66. }
  67. /**
  68. * @group connected
  69. */
  70. public function testDispatcherLoopAgainstRedisServerWithPrefix()
  71. {
  72. $parameters = array(
  73. 'host' => REDIS_SERVER_HOST,
  74. 'port' => REDIS_SERVER_PORT,
  75. 'database' => REDIS_SERVER_DBNUM,
  76. // Prevents suite from handing on broken test
  77. 'read_write_timeout' => 2,
  78. );
  79. $options = array('profile' => REDIS_SERVER_VERSION);
  80. $producerNonPfx = new Client($parameters, $options);
  81. $producerNonPfx->connect();
  82. $producerPfx = new Client($parameters, $options + array('prefix' => 'foobar'));
  83. $producerPfx->connect();
  84. $consumer = new Client($parameters, $options + array('prefix' => 'foobar'));
  85. $dispatcher = new DispatcherLoop($consumer);
  86. $callback = $this->getMock('stdClass', array('__invoke'));
  87. $callback->expects($this->exactly(1))
  88. ->method('__invoke')
  89. ->with($this->equalTo('arg:prefixed'))
  90. ->will($this->returnCallback(function ($arg) use ($dispatcher) {
  91. $dispatcher->stop();
  92. }));
  93. $dispatcher->attachCallback('callback', $callback);
  94. $producerNonPfx->publish('callback', 'arg:non-prefixed');
  95. $producerPfx->publish('callback', 'arg:prefixed');
  96. $dispatcher->run();
  97. $this->assertTrue($consumer->ping());
  98. }
  99. }