PipelineTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. $exception = null;
  270. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  271. $connection->expects($this->never())->method('writeRequest');
  272. $connection->expects($this->never())->method('readResponse');
  273. $pipeline = new Pipeline(new Client($connection));
  274. $exception = null;
  275. $responses = null;
  276. try {
  277. $responses = $pipeline->execute(function ($pipe) {
  278. $pipe->echo('one');
  279. throw new ClientException('TEST');
  280. $pipe->echo('two');
  281. });
  282. } catch (Exception $exception) {
  283. // NOOP
  284. }
  285. $this->assertInstanceOf('Predis\ClientException', $exception);
  286. $this->assertSame('TEST', $exception->getMessage());
  287. $this->assertNull($responses);
  288. }
  289. // ******************************************************************** //
  290. // ---- INTEGRATION TESTS --------------------------------------------- //
  291. // ******************************************************************** //
  292. /**
  293. * @group connected
  294. */
  295. public function testIntegrationWithFluentInterface()
  296. {
  297. $pipeline = $this->getClient()->pipeline();
  298. $results = $pipeline->echo('one')
  299. ->echo('two')
  300. ->echo('three')
  301. ->execute();
  302. $this->assertSame(array('one', 'two', 'three'), $results);
  303. }
  304. /**
  305. * @group connected
  306. */
  307. public function testIntegrationWithCallableBlock()
  308. {
  309. $client = $this->getClient();
  310. $results = $client->pipeline(function ($pipe) {
  311. $pipe->set('foo', 'bar');
  312. $pipe->get('foo');
  313. });
  314. $this->assertEquals(array('OK', 'bar'), $results);
  315. $this->assertTrue($client->exists('foo'));
  316. }
  317. /**
  318. * @group connected
  319. */
  320. public function testOutOfBandMessagesInsidePipeline()
  321. {
  322. $oob = null;
  323. $client = $this->getClient();
  324. $results = $client->pipeline(function ($pipe) use (&$oob) {
  325. $pipe->set('foo', 'bar');
  326. $oob = $pipe->getClient()->echo('oob message');
  327. $pipe->get('foo');
  328. });
  329. $this->assertEquals(array('OK', 'bar'), $results);
  330. $this->assertSame('oob message', $oob);
  331. $this->assertTrue($client->exists('foo'));
  332. }
  333. /**
  334. * @group connected
  335. */
  336. public function testIntegrationWithClientExceptionInCallableBlock()
  337. {
  338. $exception = null;
  339. $client = $this->getClient();
  340. try {
  341. $client->pipeline(function ($pipe) {
  342. $pipe->set('foo', 'bar');
  343. throw new ClientException('TEST');
  344. });
  345. } catch (Exception $exception) {
  346. // NOOP
  347. }
  348. $this->assertInstanceOf('Predis\ClientException', $exception);
  349. $this->assertSame('TEST', $exception->getMessage());
  350. $this->assertFalse($client->exists('foo'));
  351. }
  352. /**
  353. * @group connected
  354. */
  355. public function testIntegrationWithServerExceptionInCallableBlock()
  356. {
  357. $exception = null;
  358. $client = $this->getClient();
  359. try {
  360. $client->pipeline(function ($pipe) {
  361. $pipe->set('foo', 'bar');
  362. // LPUSH on a string key fails, but won't stop
  363. // the pipeline to send the commands.
  364. $pipe->lpush('foo', 'bar');
  365. $pipe->set('hoge', 'piyo');
  366. });
  367. } catch (Exception $exception) {
  368. // NOOP
  369. }
  370. $this->assertInstanceOf('Predis\Response\ServerException', $exception);
  371. $this->assertTrue($client->exists('foo'));
  372. $this->assertTrue($client->exists('hoge'));
  373. }
  374. /**
  375. * @group connected
  376. */
  377. public function testIntegrationWithServerErrorInCallableBlock()
  378. {
  379. $client = $this->getClient(array(), array('exceptions' => false));
  380. $results = $client->pipeline(function ($pipe) {
  381. $pipe->set('foo', 'bar');
  382. $pipe->lpush('foo', 'bar'); // LPUSH on a string key fails.
  383. $pipe->get('foo');
  384. });
  385. $this->assertEquals('OK', $results[0]);
  386. $this->assertInstanceOf('Predis\Response\Error', $results[1]);
  387. $this->assertSame('bar', $results[2]);
  388. }
  389. // ******************************************************************** //
  390. // ---- HELPER METHODS ------------------------------------------------ //
  391. // ******************************************************************** //
  392. /**
  393. * Returns a client instance connected to the specified Redis
  394. * server instance to perform integration tests.
  395. *
  396. * @param array $parameters Additional connection parameters.
  397. * @param array $options Additional client options.
  398. * @return Client
  399. */
  400. protected function getClient(array $parameters = array(), array $options = array())
  401. {
  402. return $this->createClient($parameters, $options);
  403. }
  404. /**
  405. * Helper method that returns a callback used to emulate the response to an
  406. * ECHO command.
  407. *
  408. * @return \Closure
  409. */
  410. protected function getReadCallback()
  411. {
  412. return function ($command) {
  413. if (($id = $command->getId()) !== 'ECHO') {
  414. throw new InvalidArgumentException("Expected ECHO, got {$id}");
  415. }
  416. list($echoed) = $command->getArguments();
  417. return $echoed;
  418. };
  419. }
  420. }