PipelineTest.php 15 KB

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