PipelineContextTest.php 13 KB

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