PipelineTest.php 15 KB

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