PipelineTest.php 15 KB

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