PhpiredisStreamConnectionTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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\Connection;
  11. use Predis\Command\RawCommand;
  12. use Predis\Response\Error as ErrorResponse;
  13. /**
  14. * @group ext-phpiredis
  15. * @requires extension phpiredis
  16. */
  17. class PhpiredisStreamConnectionTest extends PredisConnectionTestCase
  18. {
  19. const CONNECTION_CLASS = 'Predis\Connection\PhpiredisStreamConnection';
  20. /**
  21. * @group disconnected
  22. * @expectedException \InvalidArgumentException
  23. * @expectedExceptionMessage SSL encryption is not supported by this connection backend.
  24. */
  25. public function testSupportsSchemeTls()
  26. {
  27. $connection = $this->createConnectionWithParams(array('scheme' => 'tls'));
  28. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  29. }
  30. /**
  31. * @group disconnected
  32. * @expectedException \InvalidArgumentException
  33. * @expectedExceptionMessage SSL encryption is not supported by this connection backend.
  34. */
  35. public function testSupportsSchemeRediss()
  36. {
  37. $connection = $this->createConnectionWithParams(array('scheme' => 'rediss'));
  38. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  39. }
  40. /**
  41. * @group disconnected
  42. * @expectedException \Predis\Connection\ConnectionException
  43. * @expectedExceptionMessage `SELECT` failed: ERR invalid DB index [tcp://127.0.0.1:6379]
  44. */
  45. public function testThrowsExceptionOnInitializationCommandFailure()
  46. {
  47. $cmdSelect = RawCommand::create('SELECT', '1000');
  48. $connection = $this->getMockBuilder(static::CONNECTION_CLASS)
  49. ->setMethods(array('executeCommand', 'createResource'))
  50. ->setConstructorArgs(array(new Parameters()))
  51. ->getMock();
  52. $connection->method('executeCommand')
  53. ->with($cmdSelect)
  54. ->will($this->returnValue(
  55. new ErrorResponse('ERR invalid DB index')
  56. ));
  57. $connection->method('createResource');
  58. $connection->addConnectCommand($cmdSelect);
  59. $connection->connect();
  60. }
  61. // ******************************************************************** //
  62. // ---- INTEGRATION TESTS --------------------------------------------- //
  63. // ******************************************************************** //
  64. /**
  65. * @group connected
  66. * @group slow
  67. * @requires PHP 5.4
  68. * @expectedException \Predis\Connection\ConnectionException
  69. */
  70. public function testThrowsExceptionOnReadWriteTimeout()
  71. {
  72. $profile = $this->getCurrentProfile();
  73. $connection = $this->createConnectionWithParams(array(
  74. 'read_write_timeout' => 0.5,
  75. ), true);
  76. $connection->executeCommand($profile->createCommand('brpop', array('foo', 3)));
  77. }
  78. /**
  79. * @medium
  80. * @group connected
  81. * @expectedException \Predis\Protocol\ProtocolException
  82. */
  83. public function testThrowsExceptionOnProtocolDesynchronizationErrors()
  84. {
  85. $connection = $this->createConnection();
  86. $stream = $connection->getResource();
  87. $connection->writeRequest($this->getCurrentProfile()->createCommand('ping'));
  88. stream_socket_recvfrom($stream, 1);
  89. $connection->read();
  90. }
  91. /**
  92. * @group connected
  93. * @requires PHP 5.4
  94. */
  95. public function testPersistentParameterWithFalseLikeValues()
  96. {
  97. $connection1 = $this->createConnectionWithParams(array('persistent' => 0));
  98. $this->assertNonPersistentConnection($connection1);
  99. $connection2 = $this->createConnectionWithParams(array('persistent' => false));
  100. $this->assertNonPersistentConnection($connection2);
  101. $connection3 = $this->createConnectionWithParams(array('persistent' => '0'));
  102. $this->assertNonPersistentConnection($connection3);
  103. $connection4 = $this->createConnectionWithParams(array('persistent' => 'false'));
  104. $this->assertNonPersistentConnection($connection4);
  105. }
  106. /**
  107. * @group connected
  108. * @requires PHP 5.4
  109. */
  110. public function testPersistentParameterWithTrueLikeValues()
  111. {
  112. $connection1 = $this->createConnectionWithParams(array('persistent' => 1));
  113. $this->assertPersistentConnection($connection1);
  114. $connection2 = $this->createConnectionWithParams(array('persistent' => true));
  115. $this->assertPersistentConnection($connection2);
  116. $connection3 = $this->createConnectionWithParams(array('persistent' => '1'));
  117. $this->assertPersistentConnection($connection3);
  118. $connection4 = $this->createConnectionWithParams(array('persistent' => 'true'));
  119. $this->assertPersistentConnection($connection4);
  120. $connection1->disconnect();
  121. }
  122. /**
  123. * @group connected
  124. * @requires PHP 5.4
  125. */
  126. public function testPersistentConnectionsToSameNodeShareResource()
  127. {
  128. $connection1 = $this->createConnectionWithParams(array('persistent' => true));
  129. $connection2 = $this->createConnectionWithParams(array('persistent' => true));
  130. $this->assertPersistentConnection($connection1);
  131. $this->assertPersistentConnection($connection2);
  132. $this->assertSame($connection1->getResource(), $connection2->getResource());
  133. $connection1->disconnect();
  134. }
  135. /**
  136. * @group connected
  137. * @requires PHP 5.4
  138. */
  139. public function testPersistentConnectionsToSameNodeDoNotShareResourceUsingDifferentPersistentID()
  140. {
  141. $connection1 = $this->createConnectionWithParams(array('persistent' => 'conn1'));
  142. $connection2 = $this->createConnectionWithParams(array('persistent' => 'conn2'));
  143. $this->assertPersistentConnection($connection1);
  144. $this->assertPersistentConnection($connection2);
  145. $this->assertNotSame($connection1->getResource(), $connection2->getResource());
  146. }
  147. }