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 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('writeRequest');
  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('writeRequest');
  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\ResponseInterface');
  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 testExecuteReturnsPipelineForFluentInterface()
  108. {
  109. $profile = Profile\Factory::getDefault();
  110. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  111. $pipeline = new Pipeline(new Client($connection));
  112. $command = $profile->createCommand('echo', array('one'));
  113. $this->assertSame($pipeline, $pipeline->executeCommand($command));
  114. }
  115. /**
  116. * @group disconnected
  117. */
  118. public function testExecuteCommandDoesNotSendCommandsWithoutExecute()
  119. {
  120. $profile = Profile\Factory::getDefault();
  121. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  122. $connection->expects($this->never())->method('writeRequest');
  123. $connection->expects($this->never())->method('readResponse');
  124. $pipeline = new Pipeline(new Client($connection));
  125. $pipeline->executeCommand($profile->createCommand('echo', array('one')));
  126. $pipeline->executeCommand($profile->createCommand('echo', array('two')));
  127. $pipeline->executeCommand($profile->createCommand('echo', array('three')));
  128. }
  129. /**
  130. * @group disconnected
  131. */
  132. public function testExecuteWithEmptyBuffer()
  133. {
  134. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  135. $connection->expects($this->never())->method('writeRequest');
  136. $connection->expects($this->never())->method('readResponse');
  137. $pipeline = new Pipeline(new Client($connection));
  138. $this->assertSame(array(), $pipeline->execute());
  139. }
  140. /**
  141. * @group disconnected
  142. */
  143. public function testExecuteWithFilledBuffer()
  144. {
  145. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  146. $connection->expects($this->exactly(3))
  147. ->method('writeRequest');
  148. $connection->expects($this->exactly(3))
  149. ->method('readResponse')
  150. ->will($this->returnCallback($this->getReadCallback()));
  151. $pipeline = new Pipeline(new Client($connection));
  152. $pipeline->echo('one');
  153. $pipeline->echo('two');
  154. $pipeline->echo('three');
  155. $pipeline->flushPipeline();
  156. $this->assertSame(array('one', 'two', 'three'), $pipeline->execute());
  157. }
  158. /**
  159. * @group disconnected
  160. */
  161. public function testFlushWithFalseArgumentDiscardsBuffer()
  162. {
  163. $pipeline = new Pipeline(new Client());
  164. $pipeline->echo('one');
  165. $pipeline->echo('two');
  166. $pipeline->echo('three');
  167. $pipeline->flushPipeline(false);
  168. $this->assertSame(array(), $pipeline->execute());
  169. }
  170. /**
  171. * @group disconnected
  172. */
  173. public function testFlushHandlesPartialBuffers()
  174. {
  175. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  176. $connection->expects($this->exactly(4))
  177. ->method('writeRequest');
  178. $connection->expects($this->exactly(4))
  179. ->method('readResponse')
  180. ->will($this->returnCallback($this->getReadCallback()));
  181. $pipeline = new Pipeline(new Client($connection));
  182. $pipeline->echo('one');
  183. $pipeline->echo('two');
  184. $pipeline->flushPipeline();
  185. $pipeline->echo('three');
  186. $pipeline->echo('four');
  187. $this->assertSame(array('one', 'two', 'three', 'four'), $pipeline->execute());
  188. }
  189. /**
  190. * @group disconnected
  191. */
  192. public function testSwitchesToMasterWithReplicationConnection()
  193. {
  194. $pong = new Response\Status('PONG');
  195. $connection = $this->getMock('Predis\Connection\ReplicationConnectionInterface');
  196. $connection->expects($this->once())
  197. ->method('switchTo')
  198. ->with('master');
  199. $connection->expects($this->exactly(3))
  200. ->method('writeRequest');
  201. $connection->expects($this->exactly(3))
  202. ->method('readResponse')
  203. ->will($this->returnValue($pong));
  204. $pipeline = new Pipeline(new Client($connection));
  205. $pipeline->ping();
  206. $pipeline->ping();
  207. $pipeline->ping();
  208. $this->assertSame(array($pong, $pong, $pong), $pipeline->execute());
  209. }
  210. /**
  211. * @group disconnected
  212. */
  213. public function testExecuteAcceptsCallableArgument()
  214. {
  215. $test = $this;
  216. $pipeline = new Pipeline(new Client());
  217. $callable = function ($pipe) use ($test, $pipeline) {
  218. $test->assertSame($pipeline, $pipe);
  219. $pipe->flushPipeline(false);
  220. };
  221. $pipeline->execute($callable);
  222. }
  223. /**
  224. * @group disconnected
  225. * @expectedException InvalidArgumentException
  226. */
  227. public function testExecuteDoesNotAcceptNonCallableArgument()
  228. {
  229. $noncallable = new \stdClass();
  230. $pipeline = new Pipeline(new Client());
  231. $pipeline->execute($noncallable);
  232. }
  233. /**
  234. * @group disconnected
  235. * @expectedException Predis\ClientException
  236. */
  237. public function testExecuteInsideCallableArgumentThrowsException()
  238. {
  239. $pipeline = new Pipeline(new Client());
  240. $pipeline->execute(function ($pipe) {
  241. $pipe->execute();
  242. });
  243. }
  244. /**
  245. * @group disconnected
  246. */
  247. public function testExecuteWithCallableArgumentRunsPipelineInCallable()
  248. {
  249. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  250. $connection->expects($this->exactly(4))
  251. ->method('writeRequest');
  252. $connection->expects($this->exactly(4))
  253. ->method('readResponse')
  254. ->will($this->returnCallback($this->getReadCallback()));
  255. $pipeline = new Pipeline(new Client($connection));
  256. $responses = $pipeline->execute(function ($pipe) {
  257. $pipe->echo('one');
  258. $pipe->echo('two');
  259. $pipe->echo('three');
  260. $pipe->echo('four');
  261. });
  262. $this->assertSame(array('one', 'two', 'three', 'four'), $responses);
  263. }
  264. /**
  265. * @group disconnected
  266. */
  267. public function testExecuteWithCallableArgumentHandlesExceptions()
  268. {
  269. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  270. $connection->expects($this->never())->method('writeRequest');
  271. $connection->expects($this->never())->method('readResponse');
  272. $pipeline = new Pipeline(new Client($connection));
  273. $exception = null;
  274. $responses = null;
  275. try {
  276. $responses = $pipeline->execute(function ($pipe) {
  277. $pipe->echo('one');
  278. throw new ClientException('TEST');
  279. $pipe->echo('two');
  280. });
  281. } catch (Exception $ex) {
  282. $exception = $ex;
  283. }
  284. $this->assertInstanceOf('Predis\ClientException', $exception);
  285. $this->assertSame('TEST', $exception->getMessage());
  286. $this->assertNull($responses);
  287. }
  288. // ******************************************************************** //
  289. // ---- INTEGRATION TESTS --------------------------------------------- //
  290. // ******************************************************************** //
  291. /**
  292. * @group connected
  293. */
  294. public function testIntegrationWithFluentInterface()
  295. {
  296. $pipeline = $this->getClient()->pipeline();
  297. $results = $pipeline->echo('one')
  298. ->echo('two')
  299. ->echo('three')
  300. ->execute();
  301. $this->assertSame(array('one', 'two', 'three'), $results);
  302. }
  303. /**
  304. * @group connected
  305. */
  306. public function testIntegrationWithCallableBlock()
  307. {
  308. $client = $this->getClient();
  309. $results = $client->pipeline(function ($pipe) {
  310. $pipe->set('foo', 'bar');
  311. $pipe->get('foo');
  312. });
  313. $this->assertEquals(array('OK', 'bar'), $results);
  314. $this->assertTrue($client->exists('foo'));
  315. }
  316. /**
  317. * @group connected
  318. */
  319. public function testOutOfBandMessagesInsidePipeline()
  320. {
  321. $oob = null;
  322. $client = $this->getClient();
  323. $results = $client->pipeline(function ($pipe) use (&$oob) {
  324. $pipe->set('foo', 'bar');
  325. $oob = $pipe->getClient()->echo('oob message');
  326. $pipe->get('foo');
  327. });
  328. $this->assertEquals(array('OK', 'bar'), $results);
  329. $this->assertSame('oob message', $oob);
  330. $this->assertTrue($client->exists('foo'));
  331. }
  332. /**
  333. * @group connected
  334. */
  335. public function testIntegrationWithClientExceptionInCallableBlock()
  336. {
  337. $client = $this->getClient();
  338. try {
  339. $client->pipeline(function ($pipe) {
  340. $pipe->set('foo', 'bar');
  341. throw new ClientException('TEST');
  342. });
  343. } catch (Exception $ex) {
  344. $exception = $ex;
  345. }
  346. $this->assertInstanceOf('Predis\ClientException', $exception);
  347. $this->assertSame('TEST', $exception->getMessage());
  348. $this->assertFalse($client->exists('foo'));
  349. }
  350. /**
  351. * @group connected
  352. */
  353. public function testIntegrationWithServerExceptionInCallableBlock()
  354. {
  355. $client = $this->getClient();
  356. try {
  357. $client->pipeline(function ($pipe) {
  358. $pipe->set('foo', 'bar');
  359. // LPUSH on a string key fails, but won't stop
  360. // the pipeline to send the commands.
  361. $pipe->lpush('foo', 'bar');
  362. $pipe->set('hoge', 'piyo');
  363. });
  364. } catch (Exception $ex) {
  365. $exception = $ex;
  366. }
  367. $this->assertInstanceOf('Predis\Response\ServerException', $exception);
  368. $this->assertTrue($client->exists('foo'));
  369. $this->assertTrue($client->exists('hoge'));
  370. }
  371. /**
  372. * @group connected
  373. */
  374. public function testIntegrationWithServerErrorInCallableBlock()
  375. {
  376. $client = $this->getClient(array(), array('exceptions' => false));
  377. $results = $client->pipeline(function ($pipe) {
  378. $pipe->set('foo', 'bar');
  379. $pipe->lpush('foo', 'bar'); // LPUSH on a string key fails.
  380. $pipe->get('foo');
  381. });
  382. $this->assertEquals('OK', $results[0]);
  383. $this->assertInstanceOf('Predis\Response\Error', $results[1]);
  384. $this->assertSame('bar', $results[2]);
  385. }
  386. // ******************************************************************** //
  387. // ---- HELPER METHODS ------------------------------------------------ //
  388. // ******************************************************************** //
  389. /**
  390. * Returns a client instance connected to the specified Redis
  391. * server instance to perform integration tests.
  392. *
  393. * @return array Additional connection parameters.
  394. * @return array Additional client options.
  395. * @return Client New client instance.
  396. */
  397. protected function getClient(array $parameters = array(), array $options = array())
  398. {
  399. return $this->createClient($parameters, $options);
  400. }
  401. /**
  402. * Helper method that returns a callback used to emulate the response to an
  403. * ECHO command.
  404. *
  405. * @return \Closure
  406. */
  407. protected function getReadCallback()
  408. {
  409. return function ($command) {
  410. if (($id = $command->getId()) !== 'ECHO') {
  411. throw new InvalidArgumentException("Expected ECHO, got {$id}");
  412. }
  413. list($echoed) = $command->getArguments();
  414. return $echoed;
  415. };
  416. }
  417. }