PipelineContextTest.php 13 KB

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