PredisConnectionTestCase.php 19 KB

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