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. $connection = $this->createConnectionWithParams(array(
  73. 'read_write_timeout' => 0.5,
  74. ), true);
  75. $connection->executeCommand(
  76. $this->getCommandFactory()->createCommand('brpop', array('foo', 3))
  77. );
  78. }
  79. /**
  80. * @medium
  81. * @group connected
  82. * @expectedException \Predis\Protocol\ProtocolException
  83. */
  84. public function testThrowsExceptionOnProtocolDesynchronizationErrors()
  85. {
  86. $connection = $this->createConnection();
  87. $stream = $connection->getResource();
  88. $connection->writeRequest($this->getCommandFactory()->createCommand('ping'));
  89. stream_socket_recvfrom($stream, 1);
  90. $connection->read();
  91. }
  92. /**
  93. * @group connected
  94. * @requires PHP 5.4
  95. */
  96. public function testPersistentParameterWithFalseLikeValues()
  97. {
  98. $connection1 = $this->createConnectionWithParams(array('persistent' => 0));
  99. $this->assertNonPersistentConnection($connection1);
  100. $connection2 = $this->createConnectionWithParams(array('persistent' => false));
  101. $this->assertNonPersistentConnection($connection2);
  102. $connection3 = $this->createConnectionWithParams(array('persistent' => '0'));
  103. $this->assertNonPersistentConnection($connection3);
  104. $connection4 = $this->createConnectionWithParams(array('persistent' => 'false'));
  105. $this->assertNonPersistentConnection($connection4);
  106. }
  107. /**
  108. * @group connected
  109. * @requires PHP 5.4
  110. */
  111. public function testPersistentParameterWithTrueLikeValues()
  112. {
  113. $connection1 = $this->createConnectionWithParams(array('persistent' => 1));
  114. $this->assertPersistentConnection($connection1);
  115. $connection2 = $this->createConnectionWithParams(array('persistent' => true));
  116. $this->assertPersistentConnection($connection2);
  117. $connection3 = $this->createConnectionWithParams(array('persistent' => '1'));
  118. $this->assertPersistentConnection($connection3);
  119. $connection4 = $this->createConnectionWithParams(array('persistent' => 'true'));
  120. $this->assertPersistentConnection($connection4);
  121. $connection1->disconnect();
  122. }
  123. /**
  124. * @group connected
  125. * @requires PHP 5.4
  126. */
  127. public function testPersistentConnectionsToSameNodeShareResource()
  128. {
  129. $connection1 = $this->createConnectionWithParams(array('persistent' => true));
  130. $connection2 = $this->createConnectionWithParams(array('persistent' => true));
  131. $this->assertPersistentConnection($connection1);
  132. $this->assertPersistentConnection($connection2);
  133. $this->assertSame($connection1->getResource(), $connection2->getResource());
  134. $connection1->disconnect();
  135. }
  136. /**
  137. * @group connected
  138. * @requires PHP 5.4
  139. */
  140. public function testPersistentConnectionsToSameNodeDoNotShareResourceUsingDifferentPersistentID()
  141. {
  142. $connection1 = $this->createConnectionWithParams(array('persistent' => 'conn1'));
  143. $connection2 = $this->createConnectionWithParams(array('persistent' => 'conn2'));
  144. $this->assertPersistentConnection($connection1);
  145. $this->assertPersistentConnection($connection2);
  146. $this->assertNotSame($connection1->getResource(), $connection2->getResource());
  147. }
  148. }