PipelineTest.php 15 KB

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