PredisConnectionTestCase.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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. $profile = $this->getCurrentProfile();
  172. $cmdPing = $profile->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. $profile = $this->getCurrentProfile();
  182. $cmdPing = $this->getMock($profile->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. $profile = $this->getCurrentProfile();
  194. $cmdDel = $profile->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. $profile = $this->getCurrentProfile();
  204. $cmdPing = $profile->createCommand('ping');
  205. $cmdEcho = $profile->createCommand('echo', array('echoed'));
  206. $cmdGet = $profile->createCommand('get', array('foobar'));
  207. $cmdRpush = $profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol'));
  208. $cmdLrange = $profile->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. $profile = $this->getCurrentProfile();
  222. $cmdEcho = $this->getMock($profile->getCommandClass('echo'), array('parseResponse'));
  223. $cmdEcho->setArguments(array('ECHOED'));
  224. $cmdEcho->expects($this->never())
  225. ->method('parseResponse');
  226. $connection = $this->createConnection();
  227. $connection->writeRequest($cmdEcho);
  228. $connection->disconnect();
  229. }
  230. /**
  231. * @group connected
  232. */
  233. public function testReadsCommandFromServer()
  234. {
  235. $profile = $this->getCurrentProfile();
  236. $cmdEcho = $this->getMock($profile->getCommandClass('echo'), array('parseResponse'));
  237. $cmdEcho->setArguments(array('ECHOED'));
  238. $cmdEcho->expects($this->never())
  239. ->method('parseResponse');
  240. $connection = $this->createConnection();
  241. $connection->writeRequest($cmdEcho);
  242. $this->assertSame('ECHOED', $connection->readResponse($cmdEcho));
  243. }
  244. /**
  245. * @group connected
  246. */
  247. public function testIsAbleToWriteMultipleCommandsAndReadThemBackForPipelining()
  248. {
  249. $profile = $this->getCurrentProfile();
  250. $cmdPing = $this->getMock($profile->getCommandClass('ping'), array('parseResponse'));
  251. $cmdPing->expects($this->never())
  252. ->method('parseResponse');
  253. $cmdEcho = $this->getMock($profile->getCommandClass('echo'), array('parseResponse'));
  254. $cmdEcho->setArguments(array('ECHOED'));
  255. $cmdEcho->expects($this->never())
  256. ->method('parseResponse');
  257. $connection = $this->createConnection();
  258. $connection->writeRequest($cmdPing);
  259. $connection->writeRequest($cmdEcho);
  260. $this->assertEquals('PONG', $connection->readResponse($cmdPing));
  261. $this->assertSame('ECHOED', $connection->readResponse($cmdEcho));
  262. }
  263. /**
  264. * @group connected
  265. */
  266. public function testSendsInitializationCommandsOnConnection()
  267. {
  268. $profile = $this->getCurrentProfile();
  269. $cmdPing = $this->getMock($profile->getCommandClass('ping'), array('getArguments'));
  270. $cmdPing->expects($this->once())
  271. ->method('getArguments')
  272. ->will($this->returnValue(array()));
  273. $cmdEcho = $this->getMock($profile->getCommandClass('echo'), array('getArguments'));
  274. $cmdEcho->expects($this->once())
  275. ->method('getArguments')
  276. ->will($this->returnValue(array('ECHOED')));
  277. $connection = $this->createConnection();
  278. $connection->addConnectCommand($cmdPing);
  279. $connection->addConnectCommand($cmdEcho);
  280. $connection->connect();
  281. }
  282. /**
  283. * @group connected
  284. */
  285. public function testReadsStatusResponses()
  286. {
  287. $profile = $this->getCurrentProfile();
  288. $connection = $this->createConnection(true);
  289. $connection->writeRequest($profile->createCommand('set', array('foo', 'bar')));
  290. $this->assertInstanceOf('Predis\Response\Status', $connection->read());
  291. $connection->writeRequest($profile->createCommand('ping'));
  292. $this->assertInstanceOf('Predis\Response\Status', $connection->read());
  293. $connection->writeRequest($profile->createCommand('multi'));
  294. $connection->writeRequest($profile->createCommand('ping'));
  295. $this->assertInstanceOf('Predis\Response\Status', $connection->read());
  296. $this->assertInstanceOf('Predis\Response\Status', $connection->read());
  297. }
  298. /**
  299. * @group connected
  300. */
  301. public function testReadsBulkResponses()
  302. {
  303. $profile = $this->getCurrentProfile();
  304. $connection = $this->createConnection(true);
  305. $connection->executeCommand($profile->createCommand('set', array('foo', 'bar')));
  306. $connection->writeRequest($profile->createCommand('get', array('foo')));
  307. $this->assertSame('bar', $connection->read());
  308. $connection->writeRequest($profile->createCommand('get', array('hoge')));
  309. $this->assertNull($connection->read());
  310. }
  311. /**
  312. * @group connected
  313. */
  314. public function testReadsIntegerResponses()
  315. {
  316. $profile = $this->getCurrentProfile();
  317. $connection = $this->createConnection(true);
  318. $connection->executeCommand($profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol')));
  319. $connection->writeRequest($profile->createCommand('llen', array('metavars')));
  320. $this->assertSame(3, $connection->read());
  321. }
  322. /**
  323. * @group connected
  324. */
  325. public function testReadsErrorResponsesAsResponseErrorObjects()
  326. {
  327. $profile = $this->getCurrentProfile();
  328. $connection = $this->createConnection(true);
  329. $connection->executeCommand($profile->createCommand('set', array('foo', 'bar')));
  330. $connection->writeRequest($profile->createCommand('rpush', array('foo', 'baz')));
  331. $this->assertInstanceOf('Predis\Response\Error', $error = $connection->read());
  332. $this->assertRegExp('/[ERR|WRONGTYPE] Operation against a key holding the wrong kind of value/', $error->getMessage());
  333. }
  334. /**
  335. * @group connected
  336. */
  337. public function testReadsMultibulkResponsesAsArrays()
  338. {
  339. $profile = $this->getCurrentProfile();
  340. $connection = $this->createConnection(true);
  341. $connection->executeCommand($profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol')));
  342. $connection->writeRequest($profile->createCommand('lrange', array('metavars', 0, -1)));
  343. $this->assertSame(array('foo', 'hoge', 'lol'), $connection->read());
  344. }
  345. /**
  346. * @group connected
  347. * @group slow
  348. * @expectedException \Predis\Connection\ConnectionException
  349. * @expectedExceptionMessageRegExp /.* \[tcp:\/\/169.254.10.10:6379\]/
  350. */
  351. public function testThrowsExceptionOnConnectionTimeout()
  352. {
  353. // TODO: float timeouts for connect() under HHVM 3.6.6 are broken and,
  354. // unfortunately, this is the version still being used by Travis CI.
  355. if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.6.6', '<=')) {
  356. $timeout = 1;
  357. } else {
  358. $timeout = 0.1;
  359. }
  360. $connection = $this->createConnectionWithParams(array(
  361. 'host' => '169.254.10.10',
  362. 'timeout' => $timeout,
  363. ), false);
  364. $connection->connect();
  365. }
  366. /**
  367. * @group connected
  368. * @group slow
  369. * @expectedException \Predis\Connection\ConnectionException
  370. * @expectedExceptionMessageRegExp /.* \[tcp:\/\/\[0:0:0:0:0:ffff:a9fe:a0a\]:6379\]/
  371. */
  372. public function testThrowsExceptionOnConnectionTimeoutIPv6()
  373. {
  374. // TODO: float timeouts for connect() under HHVM 3.6.6 are broken and,
  375. // unfortunately, this is the version still being used by Travis CI.
  376. if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.6.6', '<=')) {
  377. $timeout = 1;
  378. } else {
  379. $timeout = 0.1;
  380. }
  381. $connection = $this->createConnectionWithParams(array(
  382. 'host' => '0:0:0:0:0:ffff:a9fe:a0a',
  383. 'timeout' => $timeout,
  384. ), false);
  385. $connection->connect();
  386. }
  387. /**
  388. * @group connected
  389. * @group slow
  390. * @expectedException \Predis\Connection\ConnectionException
  391. * @expectedExceptionMessageRegExp /.* \[unix:\/tmp\/nonexistent\/redis\.sock]/
  392. */
  393. public function testThrowsExceptionOnUnixDomainSocketNotFound()
  394. {
  395. $connection = $this->createConnectionWithParams(array(
  396. 'scheme' => 'unix',
  397. 'path' => '/tmp/nonexistent/redis.sock',
  398. ), false);
  399. $connection->connect();
  400. }
  401. /**
  402. * @group connected
  403. * @group slow
  404. * @expectedException \Predis\Connection\ConnectionException
  405. */
  406. public function testThrowsExceptionOnReadWriteTimeout()
  407. {
  408. $profile = $this->getCurrentProfile();
  409. $connection = $this->createConnectionWithParams(array(
  410. 'read_write_timeout' => 0.5,
  411. ), true);
  412. $connection->executeCommand($profile->createCommand('brpop', array('foo', 3)));
  413. }
  414. /**
  415. * @medium
  416. * @group connected
  417. * @expectedException \Predis\Protocol\ProtocolException
  418. */
  419. public function testThrowsExceptionOnProtocolDesynchronizationErrors()
  420. {
  421. $connection = $this->createConnection();
  422. $stream = $connection->getResource();
  423. $connection->writeRequest($this->getCurrentProfile()->createCommand('ping'));
  424. fread($stream, 1);
  425. $connection->read();
  426. }
  427. // ******************************************************************** //
  428. // ---- HELPER METHODS ------------------------------------------------ //
  429. // ******************************************************************** //
  430. /**
  431. * Returns a named array with the default connection parameters and their values.
  432. *
  433. * @return array Default connection parameters.
  434. */
  435. protected function getDefaultParametersArray()
  436. {
  437. return array(
  438. 'scheme' => 'tcp',
  439. 'host' => REDIS_SERVER_HOST,
  440. 'port' => REDIS_SERVER_PORT,
  441. 'database' => REDIS_SERVER_DBNUM,
  442. 'read_write_timeout' => 2,
  443. );
  444. }
  445. /**
  446. * Asserts that the connection is using a persistent resource stream.
  447. *
  448. * This assertion will trigger a connect() operation if the connection has
  449. * not been open yet.
  450. *
  451. * @param NodeConnectionInterface $connection Connection instance.
  452. */
  453. protected function assertPersistentConnection(NodeConnectionInterface $connection)
  454. {
  455. if (version_compare(PHP_VERSION, '5.4.0') < 0 || $this->isHHVM()) {
  456. $this->markTestSkipped('This test does not currently work on HHVM.');
  457. }
  458. $this->assertSame('persistent stream', get_resource_type($connection->getResource()));
  459. }
  460. /**
  461. * Asserts that the connection is not using a persistent resource stream.
  462. *
  463. * This assertion will trigger a connect() operation if the connection has
  464. * not been open yet.
  465. *
  466. * @param NodeConnectionInterface $connection Connection instance.
  467. */
  468. protected function assertNonPersistentConnection(NodeConnectionInterface $connection)
  469. {
  470. if (version_compare(PHP_VERSION, '5.4.0') < 0 || $this->isHHVM()) {
  471. $this->markTestSkipped('This test does not currently work on HHVM.');
  472. }
  473. $this->assertSame('stream', get_resource_type($connection->getResource()));
  474. }
  475. /**
  476. * Creates a new connection instance.
  477. *
  478. * @param bool $initialize Push default initialization commands (SELECT and FLUSHDB).
  479. *
  480. * @return NodeConnectionInterface
  481. */
  482. protected function createConnection($initialize = false)
  483. {
  484. return $this->createConnectionWithParams(array(), $initialize);
  485. }
  486. /**
  487. * Creates a new connection instance using additional connection parameters.
  488. *
  489. * @param mixed $parameters Additional connection parameters.
  490. * @param bool $initialize Push default initialization commands (SELECT and FLUSHDB).
  491. *
  492. * @return NodeConnectionInterface
  493. */
  494. protected function createConnectionWithParams($parameters, $initialize = false)
  495. {
  496. $class = static::CONNECTION_CLASS;
  497. $profile = $this->getCurrentProfile();
  498. if (!$parameters instanceof ParametersInterface) {
  499. $parameters = $this->getParameters($parameters);
  500. }
  501. $connection = new $class($parameters);
  502. if ($initialize) {
  503. $connection->addConnectCommand(
  504. $profile->createCommand('select', array($parameters->database))
  505. );
  506. $connection->addConnectCommand(
  507. $profile->createCommand('flushdb')
  508. );
  509. }
  510. return $connection;
  511. }
  512. }