PipelineTest.php 15 KB

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