StreamConnectionTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. /**
  12. *
  13. */
  14. class StreamConnectionTest extends PredisConnectionTestCase
  15. {
  16. const CONNECTION_CLASS = 'Predis\Connection\StreamConnection';
  17. /**
  18. * @group disconnected
  19. */
  20. public function testConstructorDoesNotOpenConnection()
  21. {
  22. $connection = new StreamConnection($this->getParameters());
  23. $this->assertFalse($connection->isConnected());
  24. }
  25. /**
  26. * @group disconnected
  27. */
  28. public function testSupportsSchemeTCP()
  29. {
  30. $parameters = $this->getParameters(array('scheme' => 'tcp'));
  31. $connection = new StreamConnection($parameters);
  32. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  33. }
  34. /**
  35. * @group disconnected
  36. */
  37. public function testSupportsSchemeRedis()
  38. {
  39. $parameters = $this->getParameters(array('scheme' => 'redis'));
  40. $connection = new StreamConnection($parameters);
  41. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  42. }
  43. /**
  44. * @group disconnected
  45. */
  46. public function testSupportsSchemeUnix()
  47. {
  48. $parameters = $this->getParameters(array('scheme' => 'unix'));
  49. $connection = new StreamConnection($parameters);
  50. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  51. }
  52. /**
  53. * @group disconnected
  54. * @expectedException \InvalidArgumentException
  55. * @expectedExceptionMessage Invalid scheme: 'udp'.
  56. */
  57. public function testThrowsExceptionOnInvalidScheme()
  58. {
  59. $parameters = $this->getParameters(array('scheme' => 'udp'));
  60. new StreamConnection($parameters);
  61. }
  62. /**
  63. * @group disconnected
  64. */
  65. public function testExposesParameters()
  66. {
  67. $parameters = $this->getParameters();
  68. $connection = new StreamConnection($parameters);
  69. $this->assertSame($parameters, $connection->getParameters());
  70. }
  71. /**
  72. * @group disconnected
  73. */
  74. public function testCanBeSerialized()
  75. {
  76. $parameters = $this->getParameters(array('alias' => 'redis', 'read_write_timeout' => 10));
  77. $connection = new StreamConnection($parameters);
  78. $unserialized = unserialize(serialize($connection));
  79. $this->assertEquals($connection, $unserialized);
  80. }
  81. // ******************************************************************** //
  82. // ---- INTEGRATION TESTS --------------------------------------------- //
  83. // ******************************************************************** //
  84. /**
  85. * @group connected
  86. * @requires PHP 5.4
  87. */
  88. public function testAcceptsTcpNodelayParameter()
  89. {
  90. $connection = new StreamConnection($this->getParameters(array('tcp_nodelay' => false)));
  91. $connection->connect();
  92. $this->assertTrue($connection->isConnected());
  93. $connection = new StreamConnection($this->getParameters(array('tcp_nodelay' => true)));
  94. $connection->connect();
  95. $this->assertTrue($connection->isConnected());
  96. }
  97. /**
  98. * @group connected
  99. */
  100. public function testPersistentParameterWithFalseLikeValues()
  101. {
  102. if ($this->isHHVM()) {
  103. $this->markTestSkipped('This test does not currently work on HHVM.');
  104. }
  105. $connection1 = new StreamConnection($this->getParameters(array('persistent' => 0)));
  106. $this->assertNonPersistentConnection($connection1);
  107. $connection2 = new StreamConnection($this->getParameters(array('persistent' => false)));
  108. $this->assertNonPersistentConnection($connection2);
  109. $connection3 = new StreamConnection($this->getParameters(array('persistent' => '0')));
  110. $this->assertNonPersistentConnection($connection3);
  111. $connection4 = new StreamConnection($this->getParameters(array('persistent' => 'false')));
  112. $this->assertNonPersistentConnection($connection4);
  113. }
  114. /**
  115. * @group connected
  116. */
  117. public function testPersistentParameterWithTrueLikeValues()
  118. {
  119. if ($this->isHHVM()) {
  120. $this->markTestSkipped('This test does not currently work on HHVM.');
  121. }
  122. $connection1 = new StreamConnection($this->getParameters(array('persistent' => 1)));
  123. $this->assertPersistentConnection($connection1);
  124. $connection2 = new StreamConnection($this->getParameters(array('persistent' => true)));
  125. $this->assertPersistentConnection($connection2);
  126. $connection3 = new StreamConnection($this->getParameters(array('persistent' => '1')));
  127. $this->assertPersistentConnection($connection3);
  128. $connection4 = new StreamConnection($this->getParameters(array('persistent' => 'true')));
  129. $this->assertPersistentConnection($connection4);
  130. $connection1->disconnect();
  131. }
  132. /**
  133. * @group connected
  134. */
  135. public function testPersistentConnectionsToSameNodeShareResource()
  136. {
  137. if ($this->isHHVM()) {
  138. $this->markTestSkipped('This test does not currently work on HHVM.');
  139. }
  140. $connection1 = new StreamConnection($this->getParameters(array('persistent' => true)));
  141. $connection2 = new StreamConnection($this->getParameters(array('persistent' => true)));
  142. $this->assertPersistentConnection($connection1);
  143. $this->assertPersistentConnection($connection2);
  144. $this->assertSame($connection1->getResource(), $connection2->getResource());
  145. $connection1->disconnect();
  146. }
  147. /**
  148. * @group connected
  149. */
  150. public function testPersistentConnectionsToSameNodeDoNotShareResourceUsingDifferentPersistentID()
  151. {
  152. if ($this->isHHVM()) {
  153. $this->markTestSkipped('This test does not currently work on HHVM.');
  154. }
  155. $connection1 = new StreamConnection($this->getParameters(array('persistent' => 'conn1')));
  156. $connection2 = new StreamConnection($this->getParameters(array('persistent' => 'conn2')));
  157. $this->assertPersistentConnection($connection1);
  158. $this->assertPersistentConnection($connection2);
  159. $this->assertNotSame($connection1->getResource(), $connection2->getResource());
  160. }
  161. /**
  162. * @group connected
  163. * @expectedException \Predis\Protocol\ProtocolException
  164. * @expectedExceptionMessage Unknown response prefix: 'P'.
  165. */
  166. public function testThrowsExceptionOnProtocolDesynchronizationErrors()
  167. {
  168. $connection = $this->getConnection($profile);
  169. $stream = $connection->getResource();
  170. $connection->writeRequest($profile->createCommand('ping'));
  171. fread($stream, 1);
  172. $connection->read();
  173. }
  174. }