PredisConnectionTestCase.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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 PredisTestCase;
  12. /**
  13. * @group realm-connection
  14. */
  15. abstract class PredisConnectionTestCase extends PredisTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testConstructorDoesNotOpenConnection()
  21. {
  22. $connection = $this->createConnection();
  23. $this->assertFalse($connection->isConnected());
  24. }
  25. /**
  26. * @group disconnected
  27. */
  28. public function testSupportsSchemeTCP()
  29. {
  30. $connection = $this->createConnectionWithParams(array('scheme' => 'tcp'));
  31. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  32. }
  33. /**
  34. * @group disconnected
  35. */
  36. public function testSupportsSchemeRedis()
  37. {
  38. $connection = $this->createConnectionWithParams(array('scheme' => 'redis'));
  39. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  40. }
  41. /**
  42. * @group disconnected
  43. */
  44. public function testSupportsSchemeTls()
  45. {
  46. $connection = $this->createConnectionWithParams(array('scheme' => 'tls'));
  47. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  48. }
  49. /**
  50. * @group disconnected
  51. */
  52. public function testSupportsSchemeRediss()
  53. {
  54. $connection = $this->createConnectionWithParams(array('scheme' => 'rediss'));
  55. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  56. }
  57. /**
  58. * @group disconnected
  59. */
  60. public function testSupportsSchemeUnix()
  61. {
  62. $connection = $this->createConnectionWithParams(array('scheme' => 'unix'));
  63. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  64. }
  65. /**
  66. * @group disconnected
  67. * @expectedException \InvalidArgumentException
  68. * @expectedExceptionMessage Invalid scheme: 'udp'.
  69. */
  70. public function testThrowsExceptionOnInvalidScheme()
  71. {
  72. $this->createConnectionWithParams(array('scheme' => 'udp'));
  73. }
  74. /**
  75. * @group disconnected
  76. */
  77. public function testExposesParameters()
  78. {
  79. $parameters = $this->getParameters();
  80. $connection = $this->createConnectionWithParams($parameters);
  81. $this->assertSame($parameters, $connection->getParameters());
  82. }
  83. /**
  84. * @group disconnected
  85. */
  86. public function testCanBeSerialized()
  87. {
  88. $parameters = $this->getParameters(array(
  89. 'alias' => 'redis',
  90. 'read_write_timeout' => 10,
  91. ));
  92. $connection = $this->createConnectionWithParams($parameters);
  93. $unserialized = unserialize(serialize($connection));
  94. $this->assertInstanceOf(static::CONNECTION_CLASS, $unserialized);
  95. $this->assertEquals($parameters, $unserialized->getParameters());
  96. }
  97. // ******************************************************************** //
  98. // ---- INTEGRATION TESTS --------------------------------------------- //
  99. // ******************************************************************** //
  100. /**
  101. * @group connected
  102. * @requires PHP 5.4
  103. */
  104. public function testAcceptsTcpNodelayParameter()
  105. {
  106. $connection = $this->createConnectionWithParams(array('tcp_nodelay' => false));
  107. $connection->connect();
  108. $this->assertTrue($connection->isConnected());
  109. $connection = $this->createConnectionWithParams(array('tcp_nodelay' => true));
  110. $connection->connect();
  111. $this->assertTrue($connection->isConnected());
  112. }
  113. /**
  114. * @group connected
  115. */
  116. public function testConnectForcesConnection()
  117. {
  118. $connection = $this->createConnection();
  119. $this->assertFalse($connection->isConnected());
  120. $connection->connect();
  121. $this->assertTrue($connection->isConnected());
  122. }
  123. /**
  124. * @group connected
  125. */
  126. public function testDoesNotThrowExceptionOnConnectWhenAlreadyConnected()
  127. {
  128. $connection = $this->createConnection();
  129. $connection->connect();
  130. $this->assertTrue($connection->isConnected());
  131. $connection->connect();
  132. $this->assertTrue($connection->isConnected());
  133. }
  134. /**
  135. * @group connected
  136. */
  137. public function testDisconnectForcesDisconnection()
  138. {
  139. $connection = $this->createConnection();
  140. $connection->connect();
  141. $this->assertTrue($connection->isConnected());
  142. $connection->disconnect();
  143. $this->assertFalse($connection->isConnected());
  144. }
  145. /**
  146. * @group disconnected
  147. */
  148. public function testDoesNotThrowExceptionOnDisconnectWhenAlreadyDisconnected()
  149. {
  150. $connection = $this->createConnection();
  151. $this->assertFalse($connection->isConnected());
  152. $connection->disconnect();
  153. $this->assertFalse($connection->isConnected());
  154. }
  155. /**
  156. * @group connected
  157. */
  158. public function testGetResourceForcesConnection()
  159. {
  160. $connection = $this->createConnection();
  161. $this->assertFalse($connection->isConnected());
  162. $this->assertInternalType('resource', $connection->getResource());
  163. $this->assertTrue($connection->isConnected());
  164. }
  165. /**
  166. * @group connected
  167. */
  168. public function testSendingCommandForcesConnection()
  169. {
  170. $connection = $this->createConnection();
  171. $commands = $this->getCommandFactory();
  172. $cmdPing = $commands->createCommand('ping');
  173. $this->assertEquals('PONG', $connection->executeCommand($cmdPing));
  174. $this->assertTrue($connection->isConnected());
  175. }
  176. /**
  177. * @group connected
  178. */
  179. public function testExecutesCommandOnServer()
  180. {
  181. $commands = $this->getCommandFactory();
  182. $cmdPing = $this->getMock($commands->getCommandClass('ping'), array('parseResponse'));
  183. $cmdPing->expects($this->never())
  184. ->method('parseResponse');
  185. $connection = $this->createConnection();
  186. $this->assertEquals('PONG', $connection->executeCommand($cmdPing));
  187. }
  188. /**
  189. * @group connected
  190. */
  191. public function testExecutesCommandWithHolesInArguments()
  192. {
  193. $commands = $this->getCommandFactory();
  194. $cmdDel = $commands->createCommand('mget', array(0 => 'key:0', 2 => 'key:2'));
  195. $connection = $this->createConnection();
  196. $this->assertSame(array(null, null), $connection->executeCommand($cmdDel));
  197. }
  198. /**
  199. * @group connected
  200. */
  201. public function testExecutesMultipleCommandsOnServer()
  202. {
  203. $commands = $this->getCommandFactory();
  204. $cmdPing = $commands->createCommand('ping');
  205. $cmdEcho = $commands->createCommand('echo', array('echoed'));
  206. $cmdGet = $commands->createCommand('get', array('foobar'));
  207. $cmdRpush = $commands->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol'));
  208. $cmdLrange = $commands->createCommand('lrange', array('metavars', 0, -1));
  209. $connection = $this->createConnection(true);
  210. $this->assertEquals('PONG', $connection->executeCommand($cmdPing));
  211. $this->assertSame('echoed', $connection->executeCommand($cmdEcho));
  212. $this->assertNull($connection->executeCommand($cmdGet));
  213. $this->assertSame(3, $connection->executeCommand($cmdRpush));
  214. $this->assertSame(array('foo', 'hoge', 'lol'), $connection->executeCommand($cmdLrange));
  215. }
  216. /**
  217. * @group connected
  218. */
  219. public function testWritesCommandToServer()
  220. {
  221. $commands = $this->getCommandFactory();
  222. $cmdEcho = $this->getMock($commands->getCommandClass('echo'), array('parseResponse'));
  223. $cmdEcho->setArguments(array('ECHOED'));
  224. $cmdEcho
  225. ->expects($this->never())
  226. ->method('parseResponse');
  227. $connection = $this->createConnection();
  228. $connection->writeRequest($cmdEcho);
  229. $connection->disconnect();
  230. }
  231. /**
  232. * @group connected
  233. */
  234. public function testReadsCommandFromServer()
  235. {
  236. $commands = $this->getCommandFactory();
  237. $cmdEcho = $this->getMock($commands->getCommandClass('echo'), array('parseResponse'));
  238. $cmdEcho->setArguments(array('ECHOED'));
  239. $cmdEcho
  240. ->expects($this->never())
  241. ->method('parseResponse');
  242. $connection = $this->createConnection();
  243. $connection->writeRequest($cmdEcho);
  244. $this->assertSame('ECHOED', $connection->readResponse($cmdEcho));
  245. }
  246. /**
  247. * @group connected
  248. */
  249. public function testIsAbleToWriteMultipleCommandsAndReadThemBackForPipelining()
  250. {
  251. $commands = $this->getCommandFactory();
  252. $cmdPing = $this->getMock($commands->getCommandClass('ping'), array('parseResponse'));
  253. $cmdPing->expects($this->never())
  254. ->method('parseResponse');
  255. $cmdEcho = $this->getMock($commands->getCommandClass('echo'), array('parseResponse'));
  256. $cmdEcho->setArguments(array('ECHOED'));
  257. $cmdEcho->expects($this->never())
  258. ->method('parseResponse');
  259. $connection = $this->createConnection();
  260. $connection->writeRequest($cmdPing);
  261. $connection->writeRequest($cmdEcho);
  262. $this->assertEquals('PONG', $connection->readResponse($cmdPing));
  263. $this->assertSame('ECHOED', $connection->readResponse($cmdEcho));
  264. }
  265. /**
  266. * @group connected
  267. */
  268. public function testSendsInitializationCommandsOnConnection()
  269. {
  270. $commands = $this->getCommandFactory();
  271. $cmdPing = $this->getMock($commands->getCommandClass('ping'), array('getArguments'));
  272. $cmdPing
  273. ->expects($this->once())
  274. ->method('getArguments')
  275. ->will($this->returnValue(array()));
  276. $cmdEcho = $this->getMock($commands->getCommandClass('echo'), array('getArguments'));
  277. $cmdEcho
  278. ->expects($this->once())
  279. ->method('getArguments')
  280. ->will($this->returnValue(array('ECHOED')));
  281. $connection = $this->createConnection();
  282. $connection->addConnectCommand($cmdPing);
  283. $connection->addConnectCommand($cmdEcho);
  284. $connection->connect();
  285. }
  286. /**
  287. * @group connected
  288. */
  289. public function testReadsStatusResponses()
  290. {
  291. $commands = $this->getCommandFactory();
  292. $connection = $this->createConnection(true);
  293. $connection->writeRequest($commands->createCommand('set', array('foo', 'bar')));
  294. $this->assertInstanceOf('Predis\Response\Status', $connection->read());
  295. $connection->writeRequest($commands->createCommand('ping'));
  296. $this->assertInstanceOf('Predis\Response\Status', $connection->read());
  297. $connection->writeRequest($commands->createCommand('multi'));
  298. $connection->writeRequest($commands->createCommand('ping'));
  299. $this->assertInstanceOf('Predis\Response\Status', $connection->read());
  300. $this->assertInstanceOf('Predis\Response\Status', $connection->read());
  301. }
  302. /**
  303. * @group connected
  304. */
  305. public function testReadsBulkResponses()
  306. {
  307. $commands = $this->getCommandFactory();
  308. $connection = $this->createConnection(true);
  309. $connection->executeCommand($commands->createCommand('set', array('foo', 'bar')));
  310. $connection->writeRequest($commands->createCommand('get', array('foo')));
  311. $this->assertSame('bar', $connection->read());
  312. $connection->writeRequest($commands->createCommand('get', array('hoge')));
  313. $this->assertNull($connection->read());
  314. }
  315. /**
  316. * @group connected
  317. */
  318. public function testReadsIntegerResponses()
  319. {
  320. $commands = $this->getCommandFactory();
  321. $connection = $this->createConnection(true);
  322. $connection->executeCommand($commands->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol')));
  323. $connection->writeRequest($commands->createCommand('llen', array('metavars')));
  324. $this->assertSame(3, $connection->read());
  325. }
  326. /**
  327. * @group connected
  328. */
  329. public function testReadsErrorResponsesAsResponseErrorObjects()
  330. {
  331. $commands = $this->getCommandFactory();
  332. $connection = $this->createConnection(true);
  333. $connection->executeCommand($commands->createCommand('set', array('foo', 'bar')));
  334. $connection->writeRequest($commands->createCommand('rpush', array('foo', 'baz')));
  335. $this->assertInstanceOf('Predis\Response\Error', $error = $connection->read());
  336. $this->assertRegExp('/[ERR|WRONGTYPE] Operation against a key holding the wrong kind of value/', $error->getMessage());
  337. }
  338. /**
  339. * @group connected
  340. */
  341. public function testReadsMultibulkResponsesAsArrays()
  342. {
  343. $commands = $this->getCommandFactory();
  344. $connection = $this->createConnection(true);
  345. $connection->executeCommand($commands->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol')));
  346. $connection->writeRequest($commands->createCommand('lrange', array('metavars', 0, -1)));
  347. $this->assertSame(array('foo', 'hoge', 'lol'), $connection->read());
  348. }
  349. /**
  350. * @group connected
  351. * @group slow
  352. * @expectedException \Predis\Connection\ConnectionException
  353. * @expectedExceptionMessageRegExp /.* \[tcp:\/\/169.254.10.10:6379\]/
  354. */
  355. public function testThrowsExceptionOnConnectionTimeout()
  356. {
  357. // TODO: float timeouts for connect() under HHVM 3.6.6 are broken and,
  358. // unfortunately, this is the version still being used by Travis CI.
  359. if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.6.6', '<=')) {
  360. $timeout = 1;
  361. } else {
  362. $timeout = 0.1;
  363. }
  364. $connection = $this->createConnectionWithParams(array(
  365. 'host' => '169.254.10.10',
  366. 'timeout' => $timeout,
  367. ), false);
  368. $connection->connect();
  369. }
  370. /**
  371. * @group connected
  372. * @group slow
  373. * @expectedException \Predis\Connection\ConnectionException
  374. * @expectedExceptionMessageRegExp /.* \[tcp:\/\/\[0:0:0:0:0:ffff:a9fe:a0a\]:6379\]/
  375. */
  376. public function testThrowsExceptionOnConnectionTimeoutIPv6()
  377. {
  378. // TODO: float timeouts for connect() under HHVM 3.6.6 are broken and,
  379. // unfortunately, this is the version still being used by Travis CI.
  380. if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.6.6', '<=')) {
  381. $timeout = 1;
  382. } else {
  383. $timeout = 0.1;
  384. }
  385. $connection = $this->createConnectionWithParams(array(
  386. 'host' => '0:0:0:0:0:ffff:a9fe:a0a',
  387. 'timeout' => $timeout,
  388. ), false);
  389. $connection->connect();
  390. }
  391. /**
  392. * @group connected
  393. * @group slow
  394. * @expectedException \Predis\Connection\ConnectionException
  395. * @expectedExceptionMessageRegExp /.* \[unix:\/tmp\/nonexistent\/redis\.sock]/
  396. */
  397. public function testThrowsExceptionOnUnixDomainSocketNotFound()
  398. {
  399. $connection = $this->createConnectionWithParams(array(
  400. 'scheme' => 'unix',
  401. 'path' => '/tmp/nonexistent/redis.sock',
  402. ), false);
  403. $connection->connect();
  404. }
  405. /**
  406. * @group connected
  407. * @group slow
  408. * @expectedException \Predis\Connection\ConnectionException
  409. */
  410. public function testThrowsExceptionOnReadWriteTimeout()
  411. {
  412. $commands = $this->getCommandFactory();
  413. $connection = $this->createConnectionWithParams(array(
  414. 'read_write_timeout' => 0.5,
  415. ), true);
  416. $connection->executeCommand($commands->createCommand('brpop', array('foo', 3)));
  417. }
  418. /**
  419. * @medium
  420. * @group connected
  421. * @expectedException \Predis\Protocol\ProtocolException
  422. */
  423. public function testThrowsExceptionOnProtocolDesynchronizationErrors()
  424. {
  425. $connection = $this->createConnection();
  426. $stream = $connection->getResource();
  427. $connection->writeRequest($this->getCommandFactory()->createCommand('ping'));
  428. fread($stream, 1);
  429. $connection->read();
  430. }
  431. // ******************************************************************** //
  432. // ---- HELPER METHODS ------------------------------------------------ //
  433. // ******************************************************************** //
  434. /**
  435. * Returns a named array with the default connection parameters and their values.
  436. *
  437. * @return array Default connection parameters.
  438. */
  439. protected function getDefaultParametersArray()
  440. {
  441. return array(
  442. 'scheme' => 'tcp',
  443. 'host' => REDIS_SERVER_HOST,
  444. 'port' => REDIS_SERVER_PORT,
  445. 'database' => REDIS_SERVER_DBNUM,
  446. 'read_write_timeout' => 2,
  447. );
  448. }
  449. /**
  450. * Asserts that the connection is using a persistent resource stream.
  451. *
  452. * This assertion will trigger a connect() operation if the connection has
  453. * not been open yet.
  454. *
  455. * @param NodeConnectionInterface $connection Connection instance.
  456. */
  457. protected function assertPersistentConnection(NodeConnectionInterface $connection)
  458. {
  459. if (version_compare(PHP_VERSION, '5.4.0') < 0 || $this->isHHVM()) {
  460. $this->markTestSkipped('This test does not currently work on HHVM.');
  461. }
  462. $this->assertSame('persistent stream', get_resource_type($connection->getResource()));
  463. }
  464. /**
  465. * Asserts that the connection is not using a persistent resource stream.
  466. *
  467. * This assertion will trigger a connect() operation if the connection has
  468. * not been open yet.
  469. *
  470. * @param NodeConnectionInterface $connection Connection instance.
  471. */
  472. protected function assertNonPersistentConnection(NodeConnectionInterface $connection)
  473. {
  474. if (version_compare(PHP_VERSION, '5.4.0') < 0 || $this->isHHVM()) {
  475. $this->markTestSkipped('This test does not currently work on HHVM.');
  476. }
  477. $this->assertSame('stream', get_resource_type($connection->getResource()));
  478. }
  479. /**
  480. * Creates a new connection instance.
  481. *
  482. * @param bool $initialize Push default initialization commands (SELECT and FLUSHDB).
  483. *
  484. * @return NodeConnectionInterface
  485. */
  486. protected function createConnection($initialize = false)
  487. {
  488. return $this->createConnectionWithParams(array(), $initialize);
  489. }
  490. /**
  491. * Creates a new connection instance using additional connection parameters.
  492. *
  493. * @param mixed $parameters Additional connection parameters.
  494. * @param bool $initialize Push default initialization commands (SELECT and FLUSHDB).
  495. *
  496. * @return NodeConnectionInterface
  497. */
  498. protected function createConnectionWithParams($parameters, $initialize = false)
  499. {
  500. $class = static::CONNECTION_CLASS;
  501. $commands = $this->getCommandFactory();
  502. if (!$parameters instanceof ParametersInterface) {
  503. $parameters = $this->getParameters($parameters);
  504. }
  505. $connection = new $class($parameters);
  506. if ($initialize) {
  507. $connection->addConnectCommand(
  508. $commands->createCommand('select', array($parameters->database))
  509. );
  510. $connection->addConnectCommand(
  511. $commands->createCommand('flushdb')
  512. );
  513. }
  514. return $connection;
  515. }
  516. }