PipelineContextTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  208. $connection->expects($this->never())->method('writeCommand');
  209. $connection->expects($this->never())->method('readResponse');
  210. $pipeline = new PipelineContext(new Client($connection));
  211. $exception = null;
  212. $replies = null;
  213. try {
  214. $replies = $pipeline->execute(function ($pipe) {
  215. $pipe->echo('one');
  216. throw new ClientException('TEST');
  217. $pipe->echo('two');
  218. });
  219. } catch (\Exception $ex) {
  220. $exception = $ex;
  221. }
  222. $this->assertInstanceOf('Predis\ClientException', $exception);
  223. $this->assertSame('TEST', $exception->getMessage());
  224. $this->assertNull($replies);
  225. }
  226. // ******************************************************************** //
  227. // ---- INTEGRATION TESTS --------------------------------------------- //
  228. // ******************************************************************** //
  229. /**
  230. * @group connected
  231. */
  232. public function testIntegrationWithFluentInterface()
  233. {
  234. $pipeline = $this->getClient()->pipeline();
  235. $results = $pipeline->echo('one')
  236. ->echo('two')
  237. ->echo('three')
  238. ->execute();
  239. $this->assertSame(array('one', 'two', 'three'), $results);
  240. }
  241. /**
  242. * @group connected
  243. */
  244. public function testIntegrationWithCallableBlock()
  245. {
  246. $client = $this->getClient();
  247. $results = $client->pipeline(function ($pipe) {
  248. $pipe->set('foo', 'bar');
  249. $pipe->get('foo');
  250. });
  251. $this->assertSame(array(true, 'bar'), $results);
  252. $this->assertTrue($client->exists('foo'));
  253. }
  254. /**
  255. * @group connected
  256. */
  257. public function testOutOfBandMessagesInsidePipeline()
  258. {
  259. $oob = null;
  260. $client = $this->getClient();
  261. $results = $client->pipeline(function ($pipe) use (&$oob) {
  262. $pipe->set('foo', 'bar');
  263. $oob = $pipe->getClient()->echo('oob message');
  264. $pipe->get('foo');
  265. });
  266. $this->assertSame(array(true, 'bar'), $results);
  267. $this->assertSame('oob message', $oob);
  268. $this->assertTrue($client->exists('foo'));
  269. }
  270. /**
  271. * @group connected
  272. */
  273. public function testIntegrationWithClientExceptionInCallableBlock()
  274. {
  275. $client = $this->getClient();
  276. try {
  277. $client->pipeline(function ($pipe) {
  278. $pipe->set('foo', 'bar');
  279. throw new ClientException('TEST');
  280. });
  281. } catch (\Exception $ex) {
  282. $exception = $ex;
  283. }
  284. $this->assertInstanceOf('Predis\ClientException', $exception);
  285. $this->assertSame('TEST', $exception->getMessage());
  286. $this->assertFalse($client->exists('foo'));
  287. }
  288. /**
  289. * @group connected
  290. */
  291. public function testIntegrationWithServerExceptionInCallableBlock()
  292. {
  293. $client = $this->getClient();
  294. try {
  295. $client->pipeline(function ($pipe) {
  296. $pipe->set('foo', 'bar');
  297. // LPUSH on a string key fails, but won't stop
  298. // the pipeline to send the commands.
  299. $pipe->lpush('foo', 'bar');
  300. $pipe->set('hoge', 'piyo');
  301. });
  302. } catch (\Exception $ex) {
  303. $exception = $ex;
  304. }
  305. $this->assertInstanceOf('Predis\ServerException', $exception);
  306. $this->assertTrue($client->exists('foo'));
  307. $this->assertTrue($client->exists('hoge'));
  308. }
  309. /**
  310. * @group connected
  311. */
  312. public function testIntegrationWithServerErrorInCallableBlock()
  313. {
  314. $client = $this->getClient(array(), array('exceptions' => false));
  315. $results = $client->pipeline(function ($pipe) {
  316. $pipe->set('foo', 'bar');
  317. $pipe->lpush('foo', 'bar'); // LPUSH on a string key fails.
  318. $pipe->get('foo');
  319. });
  320. $this->assertTrue($results[0]);
  321. $this->assertInstanceOf('Predis\ResponseError', $results[1]);
  322. $this->assertSame('bar', $results[2]);
  323. }
  324. // ******************************************************************** //
  325. // ---- HELPER METHODS ------------------------------------------------ //
  326. // ******************************************************************** //
  327. /**
  328. * Returns a client instance connected to the specified Redis
  329. * server instance to perform integration tests.
  330. *
  331. * @return array Additional connection parameters.
  332. * @return array Additional client options.
  333. * @return Client New client instance.
  334. */
  335. protected function getClient(array $parameters = array(), array $options = array())
  336. {
  337. return $this->createClient($parameters, $options);
  338. }
  339. /**
  340. * Helper method that returns a callback used to emulate a reply
  341. * to an ECHO command.
  342. *
  343. * @return \Closure
  344. */
  345. protected function getReadCallback()
  346. {
  347. return function ($command) {
  348. if (($id = $command->getId()) !== 'ECHO') {
  349. throw new \InvalidArgumentException("Expected ECHO, got {$id}");
  350. }
  351. list($echoed) = $command->getArguments();
  352. return $echoed;
  353. };
  354. }
  355. }