PipelineTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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\Pipeline;
  11. use PHPUnit_Framework_TestCase as StandardTestCase;
  12. use Predis\Client;
  13. use Predis\ClientException;
  14. use Predis\Profile\ServerProfile;
  15. use Predis\Response;
  16. /**
  17. *
  18. */
  19. class PipelineTest extends StandardTestCase
  20. {
  21. /**
  22. * @group disconnected
  23. */
  24. public function testConstructor()
  25. {
  26. $client = new Client();
  27. $pipeline = new Pipeline($client);
  28. $this->assertSame($client, $pipeline->getClient());
  29. }
  30. /**
  31. * @group disconnected
  32. */
  33. public function testCallDoesNotSendCommandsWithoutExecute()
  34. {
  35. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  36. $connection->expects($this->never())->method('writeCommand');
  37. $connection->expects($this->never())->method('readResponse');
  38. $pipeline = new Pipeline(new Client($connection));
  39. $pipeline->echo('one');
  40. $pipeline->echo('two');
  41. $pipeline->echo('three');
  42. }
  43. /**
  44. * @group disconnected
  45. */
  46. public function testCallReturnsPipelineForFluentInterface()
  47. {
  48. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  49. $connection->expects($this->never())->method('writeCommand');
  50. $connection->expects($this->never())->method('readResponse');
  51. $pipeline = new Pipeline(new Client($connection));
  52. $this->assertSame($pipeline, $pipeline->echo('one'));
  53. $this->assertSame($pipeline, $pipeline->echo('one')->echo('two')->echo('three'));
  54. }
  55. /**
  56. * @group disconnected
  57. */
  58. public function testDoesNotParseComplexResponseObjects()
  59. {
  60. $object = $this->getMock('Predis\Response\ObjectInterface');
  61. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  62. $connection->expects($this->once())
  63. ->method('readResponse')
  64. ->will($this->returnValue($object));
  65. $pipeline = new Pipeline(new Client($connection));
  66. $pipeline->ping();
  67. $this->assertSame(array($object), $pipeline->execute());
  68. }
  69. /**
  70. * @group disconnected
  71. * @expectedException Predis\Response\ServerException
  72. * @expectedExceptionMessage ERR Test error
  73. */
  74. public function testThrowsServerExceptionOnResponseErrorByDefault()
  75. {
  76. $error = new Response\Error('ERR Test error');
  77. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  78. $connection->expects($this->once())
  79. ->method('readResponse')
  80. ->will($this->returnValue($error));
  81. $pipeline = new Pipeline(new Client($connection));
  82. $pipeline->ping();
  83. $pipeline->ping();
  84. $pipeline->execute();
  85. }
  86. /**
  87. * @group disconnected
  88. */
  89. public function testReturnsResponseErrorWithClientExceptionsSetToFalse()
  90. {
  91. $error = $this->getMock('Predis\Response\ErrorInterface');
  92. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  93. $connection->expects($this->exactly(2))
  94. ->method('readResponse')
  95. ->will($this->returnValue($error));
  96. $client = new Client($connection, array('exceptions' => false));
  97. $pipeline = new Pipeline($client);
  98. $pipeline->ping();
  99. $pipeline->ping();
  100. $this->assertSame(array($error, $error), $pipeline->execute());
  101. }
  102. /**
  103. * @group disconnected
  104. */
  105. public function testExecuteCommandDoesNotSendCommandsWithoutExecute()
  106. {
  107. $profile = ServerProfile::getDefault();
  108. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  109. $connection->expects($this->never())->method('writeCommand');
  110. $connection->expects($this->never())->method('readResponse');
  111. $pipeline = new Pipeline(new Client($connection));
  112. $pipeline->executeCommand($profile->createCommand('echo', array('one')));
  113. $pipeline->executeCommand($profile->createCommand('echo', array('two')));
  114. $pipeline->executeCommand($profile->createCommand('echo', array('three')));
  115. }
  116. /**
  117. * @group disconnected
  118. */
  119. public function testExecuteWithEmptyBuffer()
  120. {
  121. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  122. $connection->expects($this->never())->method('writeCommand');
  123. $connection->expects($this->never())->method('readResponse');
  124. $pipeline = new Pipeline(new Client($connection));
  125. $this->assertSame(array(), $pipeline->execute());
  126. }
  127. /**
  128. * @group disconnected
  129. */
  130. public function testExecuteWithFilledBuffer()
  131. {
  132. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  133. $connection->expects($this->exactly(3))
  134. ->method('writeCommand');
  135. $connection->expects($this->exactly(3))
  136. ->method('readResponse')
  137. ->will($this->returnCallback($this->getReadCallback()));
  138. $pipeline = new Pipeline(new Client($connection));
  139. $pipeline->echo('one');
  140. $pipeline->echo('two');
  141. $pipeline->echo('three');
  142. $pipeline->flushPipeline();
  143. $this->assertSame(array('one', 'two', 'three'), $pipeline->execute());
  144. }
  145. /**
  146. * @group disconnected
  147. */
  148. public function testFlushWithFalseArgumentDiscardsBuffer()
  149. {
  150. $pipeline = new Pipeline(new Client());
  151. $pipeline->echo('one');
  152. $pipeline->echo('two');
  153. $pipeline->echo('three');
  154. $pipeline->flushPipeline(false);
  155. $this->assertSame(array(), $pipeline->execute());
  156. }
  157. /**
  158. * @group disconnected
  159. */
  160. public function testFlushHandlesPartialBuffers()
  161. {
  162. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  163. $connection->expects($this->exactly(4))
  164. ->method('writeCommand');
  165. $connection->expects($this->exactly(4))
  166. ->method('readResponse')
  167. ->will($this->returnCallback($this->getReadCallback()));
  168. $pipeline = new Pipeline(new Client($connection));
  169. $pipeline->echo('one');
  170. $pipeline->echo('two');
  171. $pipeline->flushPipeline();
  172. $pipeline->echo('three');
  173. $pipeline->echo('four');
  174. $this->assertSame(array('one', 'two', 'three', 'four'), $pipeline->execute());
  175. }
  176. /**
  177. * @group disconnected
  178. */
  179. public function testSwitchesToMasterWithReplicationConnection()
  180. {
  181. $connection = $this->getMock('Predis\Connection\ReplicationConnectionInterface');
  182. $connection->expects($this->once())
  183. ->method('switchTo')
  184. ->with('master');
  185. $connection->expects($this->exactly(3))
  186. ->method('writeCommand');
  187. $connection->expects($this->exactly(3))
  188. ->method('readResponse')
  189. ->will($this->returnValue('PONG'));
  190. $pipeline = new Pipeline(new Client($connection));
  191. $pipeline->ping();
  192. $pipeline->ping();
  193. $pipeline->ping();
  194. $this->assertSame(array(true, true, true), $pipeline->execute());
  195. }
  196. /**
  197. * @group disconnected
  198. */
  199. public function testExecuteAcceptsCallableArgument()
  200. {
  201. $test = $this;
  202. $pipeline = new Pipeline(new Client());
  203. $callable = function ($pipe) use ($test, $pipeline) {
  204. $test->assertSame($pipeline, $pipe);
  205. $pipe->flushPipeline(false);
  206. };
  207. $pipeline->execute($callable);
  208. }
  209. /**
  210. * @group disconnected
  211. * @expectedException InvalidArgumentException
  212. */
  213. public function testExecuteDoesNotAcceptNonCallableArgument()
  214. {
  215. $noncallable = new \stdClass();
  216. $pipeline = new Pipeline(new Client());
  217. $pipeline->execute($noncallable);
  218. }
  219. /**
  220. * @group disconnected
  221. * @expectedException Predis\ClientException
  222. */
  223. public function testExecuteInsideCallableArgumentThrowsException()
  224. {
  225. $pipeline = new Pipeline(new Client());
  226. $pipeline->execute(function ($pipe) {
  227. $pipe->execute();
  228. });
  229. }
  230. /**
  231. * @group disconnected
  232. */
  233. public function testExecuteWithCallableArgumentRunsPipelineInCallable()
  234. {
  235. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  236. $connection->expects($this->exactly(4))
  237. ->method('writeCommand');
  238. $connection->expects($this->exactly(4))
  239. ->method('readResponse')
  240. ->will($this->returnCallback($this->getReadCallback()));
  241. $pipeline = new Pipeline(new Client($connection));
  242. $replies = $pipeline->execute(function ($pipe) {
  243. $pipe->echo('one');
  244. $pipe->echo('two');
  245. $pipe->echo('three');
  246. $pipe->echo('four');
  247. });
  248. $this->assertSame(array('one', 'two', 'three', 'four'), $replies);
  249. }
  250. /**
  251. * @group disconnected
  252. */
  253. public function testExecuteWithCallableArgumentHandlesExceptions()
  254. {
  255. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  256. $connection->expects($this->never())->method('writeCommand');
  257. $connection->expects($this->never())->method('readResponse');
  258. $pipeline = new Pipeline(new Client($connection));
  259. $exception = null;
  260. $replies = null;
  261. try {
  262. $replies = $pipeline->execute(function ($pipe) {
  263. $pipe->echo('one');
  264. throw new ClientException('TEST');
  265. $pipe->echo('two');
  266. });
  267. } catch (\Exception $ex) {
  268. $exception = $ex;
  269. }
  270. $this->assertInstanceOf('Predis\ClientException', $exception);
  271. $this->assertSame('TEST', $exception->getMessage());
  272. $this->assertNull($replies);
  273. }
  274. // ******************************************************************** //
  275. // ---- INTEGRATION TESTS --------------------------------------------- //
  276. // ******************************************************************** //
  277. /**
  278. * @group connected
  279. */
  280. public function testIntegrationWithFluentInterface()
  281. {
  282. $pipeline = $this->getClient()->pipeline();
  283. $results = $pipeline->echo('one')
  284. ->echo('two')
  285. ->echo('three')
  286. ->execute();
  287. $this->assertSame(array('one', 'two', 'three'), $results);
  288. }
  289. /**
  290. * @group connected
  291. */
  292. public function testIntegrationWithCallableBlock()
  293. {
  294. $client = $this->getClient();
  295. $results = $client->pipeline(function ($pipe) {
  296. $pipe->set('foo', 'bar');
  297. $pipe->get('foo');
  298. });
  299. $this->assertSame(array(true, 'bar'), $results);
  300. $this->assertTrue($client->exists('foo'));
  301. }
  302. /**
  303. * @group connected
  304. */
  305. public function testOutOfBandMessagesInsidePipeline()
  306. {
  307. $oob = null;
  308. $client = $this->getClient();
  309. $results = $client->pipeline(function ($pipe) use (&$oob) {
  310. $pipe->set('foo', 'bar');
  311. $oob = $pipe->getClient()->echo('oob message');
  312. $pipe->get('foo');
  313. });
  314. $this->assertSame(array(true, 'bar'), $results);
  315. $this->assertSame('oob message', $oob);
  316. $this->assertTrue($client->exists('foo'));
  317. }
  318. /**
  319. * @group connected
  320. */
  321. public function testIntegrationWithClientExceptionInCallableBlock()
  322. {
  323. $client = $this->getClient();
  324. try {
  325. $client->pipeline(function ($pipe) {
  326. $pipe->set('foo', 'bar');
  327. throw new ClientException('TEST');
  328. });
  329. } catch (\Exception $ex) {
  330. $exception = $ex;
  331. }
  332. $this->assertInstanceOf('Predis\ClientException', $exception);
  333. $this->assertSame('TEST', $exception->getMessage());
  334. $this->assertFalse($client->exists('foo'));
  335. }
  336. /**
  337. * @group connected
  338. */
  339. public function testIntegrationWithServerExceptionInCallableBlock()
  340. {
  341. $client = $this->getClient();
  342. try {
  343. $client->pipeline(function ($pipe) {
  344. $pipe->set('foo', 'bar');
  345. // LPUSH on a string key fails, but won't stop
  346. // the pipeline to send the commands.
  347. $pipe->lpush('foo', 'bar');
  348. $pipe->set('hoge', 'piyo');
  349. });
  350. } catch (\Exception $ex) {
  351. $exception = $ex;
  352. }
  353. $this->assertInstanceOf('Predis\Response\ServerException', $exception);
  354. $this->assertTrue($client->exists('foo'));
  355. $this->assertTrue($client->exists('hoge'));
  356. }
  357. /**
  358. * @group connected
  359. */
  360. public function testIntegrationWithServerErrorInCallableBlock()
  361. {
  362. $client = $this->getClient(array(), array('exceptions' => false));
  363. $results = $client->pipeline(function ($pipe) {
  364. $pipe->set('foo', 'bar');
  365. $pipe->lpush('foo', 'bar'); // LPUSH on a string key fails.
  366. $pipe->get('foo');
  367. });
  368. $this->assertTrue($results[0]);
  369. $this->assertInstanceOf('Predis\Response\Error', $results[1]);
  370. $this->assertSame('bar', $results[2]);
  371. }
  372. // ******************************************************************** //
  373. // ---- HELPER METHODS ------------------------------------------------ //
  374. // ******************************************************************** //
  375. /**
  376. * Returns a client instance connected to the specified Redis
  377. * server instance to perform integration tests.
  378. *
  379. * @return array Additional connection parameters.
  380. * @return array Additional client options.
  381. * @return Client New client instance.
  382. */
  383. protected function getClient(array $parameters = array(), array $options = array())
  384. {
  385. $parameters = array_merge(array(
  386. 'scheme' => 'tcp',
  387. 'host' => REDIS_SERVER_HOST,
  388. 'port' => REDIS_SERVER_PORT,
  389. 'database' => REDIS_SERVER_DBNUM,
  390. ), $parameters);
  391. $options = array_merge(array(
  392. 'profile' => REDIS_SERVER_VERSION,
  393. ), $options);
  394. $client = new Client($parameters, $options);
  395. $client->connect();
  396. $client->flushdb();
  397. return $client;
  398. }
  399. /**
  400. * Helper method that returns a callback used to emulate a reply
  401. * to an ECHO command.
  402. *
  403. * @return \Closure
  404. */
  405. protected function getReadCallback()
  406. {
  407. return function ($command) {
  408. if (($id = $command->getId()) !== 'ECHO') {
  409. throw new \InvalidArgumentException("Expected ECHO, got {$id}");
  410. }
  411. list($echoed) = $command->getArguments();
  412. return $echoed;
  413. };
  414. }
  415. }