PipelineTest.php 15 KB

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