PredisConnectionTestCase.php 19 KB

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