PlatformJobQueueTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Queue
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** PHPUnit Test Case */
  23. require_once 'PHPUnit/Framework/TestCase.php';
  24. /** TestHelp.php */
  25. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  26. /** Zend_Queue */
  27. require_once 'Zend/Queue.php';
  28. /** Zend_Queue */
  29. require_once 'Zend/Queue/Message.php';
  30. /** Zend_Queue_Message_Test */
  31. require_once 'MessageTestClass.php';
  32. /** Base Adapter test class */
  33. require_once dirname(__FILE__) . '/AdapterTest.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Queue
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_Queue
  41. */
  42. class Zend_Queue_Adapter_PlatformJobQueueTest extends Zend_Queue_Adapter_AdapterTest
  43. {
  44. public function setUp()
  45. {
  46. if (!defined('TESTS_ZEND_QUEUE_PLATFORMJQ_HOST')
  47. || !constant('TESTS_ZEND_QUEUE_PLATFORMJQ_HOST')
  48. ) {
  49. $this->markTestSkipped();
  50. }
  51. }
  52. /**
  53. * getAdapterName() is a method to help make AdapterTest work with any
  54. * new adapters
  55. *
  56. * You must overload this method
  57. *
  58. * @return string
  59. */
  60. public function getAdapterName()
  61. {
  62. return 'PlatformJobQueue';
  63. }
  64. public function getTestConfig()
  65. {
  66. return array('daemonOptions' => array(
  67. 'host' => constant('TESTS_ZEND_QUEUE_PLATFORMJQ_HOST'),
  68. 'password' => constant('TESTS_ZEND_QUEUE_PLATFORMJQ_PASS'),
  69. ));
  70. }
  71. /**
  72. * getAdapterFullName() is a method to help make AdapterTest work with any
  73. * new adapters
  74. *
  75. * You may overload this method. The default return is
  76. * 'Zend_Queue_Adapter_' . $this->getAdapterName()
  77. *
  78. * @return string
  79. */
  80. public function getAdapterFullName()
  81. {
  82. return 'Zend_Queue_Adapter_' . $this->getAdapterName();
  83. }
  84. public function testFailedConstructor()
  85. {
  86. try {
  87. $queue = $this->createQueue(__FUNCTION__, array());
  88. $this->fail('The test should fail if no host and password are passed');
  89. } catch (Exception $e) {
  90. $this->assertTrue( true, 'Job Queue host and password should be provided');
  91. }
  92. try {
  93. $queue = $this->createQueue(__FUNCTION__, array('daemonOptions' => array()));
  94. $this->fail('The test should fail if no host is passed');
  95. } catch (Exception $e) {
  96. $this->assertTrue(true, 'Platform Job Queue host should be provided');
  97. }
  98. try {
  99. $queue = $this->createQueue(__FUNCTION__, array('daemonOptions' => array('host' => 'localhost')));
  100. $this->fail('The test should fail if no password is passed');
  101. } catch (Exception $e) {
  102. $this->assertTrue(true, 'Platform Job Queue password should be provided');
  103. }
  104. }
  105. // this tests the configuration option $config['messageClass']
  106. public function testZendQueueMessageTest()
  107. {
  108. $config = $this->getTestConfig();
  109. if (!$queue = $this->createQueue(__FUNCTION__, $config)) {
  110. return;
  111. }
  112. $message = $queue->send(array('script' => 'info.php'));
  113. $this->assertTrue($message instanceof Zend_Queue_Message);
  114. $list = $queue->receive();
  115. $this->assertTrue($list instanceof Zend_Queue_Message_Iterator);
  116. foreach ( $list as $message ) {
  117. $this->assertTrue($message instanceof Zend_Queue_Message_PlatformJob);
  118. $queue->deleteMessage($message);
  119. }
  120. // delete the queue we created
  121. $queue->deleteQueue();
  122. }
  123. public function testSend()
  124. {
  125. if (!$queue = $this->createQueue(__FUNCTION__)) {
  126. return;
  127. }
  128. $adapter = $queue->getAdapter();
  129. $message = $adapter->send(array('script' => 'info.php'));
  130. $this->assertTrue($message instanceof Zend_Queue_Message);
  131. $list = $queue->receive();
  132. $this->assertTrue($list instanceof Zend_Queue_Message_Iterator);
  133. foreach ($list as $message) {
  134. $this->assertTrue($message instanceof Zend_Queue_Message_PlatformJob);
  135. $queue->deleteMessage($message);
  136. }
  137. // delete the queue we created
  138. $queue->deleteQueue();
  139. }
  140. public function testReceive()
  141. {
  142. if (!$queue = $this->createQueue(__FUNCTION__)) {
  143. return;
  144. }
  145. $adapter = $queue->getAdapter();
  146. // check to see if this function is supported
  147. $func = 'receive';
  148. if (!$adapter->isSupported($func)) {
  149. $this->markTestSkipped($func . '() is not supported');
  150. return;
  151. }
  152. $scriptName = 'info.php';
  153. // send the message
  154. $message = $adapter->send((array('script' => $scriptName)));
  155. $this->assertTrue($message instanceof Zend_Queue_Message);
  156. // get it back
  157. $list = $adapter->receive(1);
  158. $this->assertEquals(1, count($list));
  159. $this->assertTrue($list instanceof Zend_Queue_Message_Iterator);
  160. $this->assertTrue($list->valid());
  161. $message = $list->current();
  162. if ($adapter->isSupported('deleteMessage')) {
  163. $adapter->deleteMessage($list->current());
  164. }
  165. $this->assertTrue($message instanceof Zend_Queue_Message);
  166. $this->assertEquals($message->getJob()->getScript(), $scriptName);
  167. // delete the queue we created
  168. $queue->deleteQueue();
  169. }
  170. public function testDeleteMessage()
  171. {
  172. if (!$queue = $this->createQueue(__FUNCTION__)) {
  173. return;
  174. }
  175. $adapter = $queue->getAdapter();
  176. // check to see if this function is supported
  177. $func = 'receive';
  178. if (!$adapter->isSupported($func)) {
  179. $this->markTestSkipped($func . '() is not supported');
  180. return;
  181. }
  182. $scriptName = 'info.php';
  183. // send the message
  184. $message = $adapter->send((array('script' => $scriptName)));
  185. $this->assertTrue($message instanceof Zend_Queue_Message);
  186. // get it back
  187. $list = $adapter->receive(1);
  188. $this->assertEquals(1, count($list));
  189. $this->assertTrue($list instanceof Zend_Queue_Message_Iterator);
  190. $this->assertTrue($list->valid());
  191. $message = $list->current();
  192. if ($adapter->isSupported('deleteMessage')) {
  193. $adapter->deleteMessage($list->current());
  194. }
  195. $this->assertTrue($message instanceof Zend_Queue_Message);
  196. $this->assertEquals($message->getJob()->getScript(), $scriptName);
  197. $id = $message->getJob()->getID();
  198. $this->assertFalse($adapter->isJobIdExist($id));
  199. // delete the queue we created
  200. $queue->deleteQueue();
  201. }
  202. public function testCount()
  203. {
  204. if (!$queue = $this->createQueue(__FUNCTION__)) {
  205. return;
  206. }
  207. $adapter = $queue->getAdapter();
  208. // check to see if this function is supported
  209. $func = 'count';
  210. if (!$adapter->isSupported($func)) {
  211. $this->markTestSkipped($func . '() is not supported');
  212. return;
  213. }
  214. $initCount = $adapter->count();
  215. // send a message
  216. $message = $adapter->send(array('script' => 'info.php'));
  217. // test queue count for being 1
  218. $this->assertEquals($adapter->count(), ($initCount + 1));
  219. // receive the message
  220. $message = $adapter->receive();
  221. /* we need to delete the messages we put in the queue before
  222. * counting.
  223. *
  224. * not all adapters support deleteMessage, but we should remove
  225. * the messages that we created if we can.
  226. */
  227. if ($adapter->isSupported('deleteMessage')) {
  228. foreach ($message as $msg) {
  229. $adapter->deleteMessage($msg);
  230. }
  231. }
  232. // test the count for being 0
  233. $this->assertEquals($adapter->count(), $initCount);
  234. // delete the queue we created
  235. $queue->deleteQueue();
  236. }
  237. public function testSampleBehavior()
  238. {
  239. if (!$queue = $this->createQueue(__FUNCTION__)) {
  240. return;
  241. }
  242. $this->assertTrue($queue instanceof Zend_Queue);
  243. $initCount = $queue->count();
  244. $scriptName = 'info.php';
  245. for ($i = 0; $i < 10; $i++) {
  246. $queue->send(array('script' => $scriptName));
  247. }
  248. $messages = $queue->receive(5);
  249. foreach($messages as $i => $message) {
  250. $this->assertEquals($message->getJob()->getScript(), $scriptName);
  251. $queue->deleteMessage($message);
  252. }
  253. for ($i = 0; $i < 5; $i++) {
  254. $messages = $queue->receive();
  255. $message = $messages->current();
  256. $queue->deleteMessage($message);
  257. }
  258. $this->assertEquals($initCount, count($queue));
  259. $this->assertTrue($queue->deleteQueue());
  260. // delete the queue we created
  261. $queue->deleteQueue();
  262. }
  263. public function testVisibility()
  264. {
  265. $this->markTestSkipped('testVisibility unsupported');
  266. }
  267. // test the constants
  268. public function testConst()
  269. {
  270. $this->markTestSkipped('no constants to test');
  271. }
  272. }