PipelineContextTest.php 13 KB

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