DispatcherLoopTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 Predis\Client;
  12. use PredisTestCase;
  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 hanging 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. $pubsub = new Consumer($consumer);
  39. $dispatcher = new DispatcherLoop($pubsub);
  40. $function01 = $this->getMock('stdClass', array('__invoke'));
  41. $function01->expects($this->exactly(2))
  42. ->method('__invoke')
  43. ->with($this->logicalOr(
  44. $this->equalTo('01:argument'),
  45. $this->equalTo('01:quit')
  46. ))
  47. ->will($this->returnCallback(function ($arg) use ($dispatcher) {
  48. if ($arg === '01:quit') {
  49. $dispatcher->stop();
  50. }
  51. }));
  52. $function02 = $this->getMock('stdClass', array('__invoke'));
  53. $function02->expects($this->once())
  54. ->method('__invoke')
  55. ->with('02:argument');
  56. $function03 = $this->getMock('stdClass', array('__invoke'));
  57. $function03->expects($this->never())
  58. ->method('__invoke');
  59. $dispatcher->attachCallback('function:01', $function01);
  60. $dispatcher->attachCallback('function:02', $function02);
  61. $dispatcher->attachCallback('function:03', $function03);
  62. $producer->publish('function:01', '01:argument');
  63. $producer->publish('function:02', '02:argument');
  64. $producer->publish('function:01', '01:quit');
  65. $dispatcher->run();
  66. $this->assertEquals('PONG', $consumer->ping());
  67. }
  68. /**
  69. * @group connected
  70. */
  71. public function testDispatcherLoopAgainstRedisServerWithPrefix()
  72. {
  73. $parameters = array(
  74. 'host' => REDIS_SERVER_HOST,
  75. 'port' => REDIS_SERVER_PORT,
  76. 'database' => REDIS_SERVER_DBNUM,
  77. // Prevents suite from handing on broken test
  78. 'read_write_timeout' => 2,
  79. );
  80. $options = array('profile' => REDIS_SERVER_VERSION);
  81. $producerNonPfx = new Client($parameters, $options);
  82. $producerNonPfx->connect();
  83. $producerPfx = new Client($parameters, $options + array('prefix' => 'foobar'));
  84. $producerPfx->connect();
  85. $consumer = new Client($parameters, $options + array('prefix' => 'foobar'));
  86. $pubsub = new Consumer($consumer);
  87. $dispatcher = new DispatcherLoop($pubsub);
  88. $callback = $this->getMock('stdClass', array('__invoke'));
  89. $callback->expects($this->exactly(1))
  90. ->method('__invoke')
  91. ->with($this->equalTo('arg:prefixed'))
  92. ->will($this->returnCallback(function ($arg) use ($dispatcher) {
  93. $dispatcher->stop();
  94. }));
  95. $dispatcher->attachCallback('callback', $callback);
  96. $producerNonPfx->publish('callback', 'arg:non-prefixed');
  97. $producerPfx->publish('callback', 'arg:prefixed');
  98. $dispatcher->run();
  99. $this->assertEquals('PONG', $consumer->ping());
  100. }
  101. }