PredisConnectionTestCase.php 18 KB

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