AdapterTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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-2015 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. /*
  23. * The adapter test class provides a universal test class for all of the
  24. * abstract methods.
  25. *
  26. * All methods marked not supported are explictly checked for for throwing
  27. * an exception.
  28. */
  29. /** Zend_Queue */
  30. require_once 'Zend/Queue.php';
  31. /** Zend_Queue */
  32. require_once 'Zend/Queue/Message.php';
  33. /** Zend_Queue_Message_Test */
  34. require_once 'MessageTestClass.php';
  35. /** Zend_Queue_Message_Iterator2 */
  36. require_once 'Iterator2.php';
  37. /**
  38. * @see Zend_Config
  39. */
  40. require_once 'Zend/Config.php';
  41. /**
  42. * @category Zend
  43. * @package Zend_Queue
  44. * @subpackage UnitTests
  45. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  46. * @license http://framework.zend.com/license/new-bsd New BSD License
  47. * @group Zend_Queue
  48. */
  49. abstract class Zend_Queue_Adapter_AdapterTest extends PHPUnit_Framework_TestCase
  50. {
  51. public function tearDown()
  52. {
  53. $this->error = false;
  54. }
  55. /**
  56. * getAdapterName() is an method to help make AdapterTest work with any
  57. * new adapters
  58. *
  59. * You must overload this method
  60. *
  61. * @return string
  62. */
  63. public function getAdapterName()
  64. {
  65. die('You must overload this function: getAdapterName()');
  66. // example for Zend_Queue_Adatper_Array
  67. return 'Array';
  68. }
  69. /**
  70. * getAdapterName() is an method to help make AdapterTest work with any
  71. * new adapters
  72. *
  73. * You may overload this method. The default return is
  74. * 'Zend_Queue_Adapter_' . $this->getAdapterName()
  75. *
  76. * @return string
  77. */
  78. public function getAdapterFullName()
  79. {
  80. return 'Zend_Queue_Adapter_' . $this->getAdapterName();
  81. }
  82. public function getTestConfig()
  83. {
  84. return array('driverOptions' => array());
  85. }
  86. /**
  87. * for ActiveMQ it uses /queue/ /temp-queue/ /topic/ /temp-topic/
  88. */
  89. public function createQueueName($name)
  90. {
  91. return $name;
  92. }
  93. /**
  94. * This is a generic function that creates a queue
  95. *
  96. * @param array $config, $config['name'] must be set.
  97. *
  98. * or
  99. *
  100. * @param string $name - name of the queue to create
  101. * @param array $config - a special config?
  102. * @return Zend_Queue
  103. */
  104. protected function createQueue($name, $config = null)
  105. {
  106. if (is_array($name)) {
  107. $config = $name;
  108. }
  109. if ($config === null) {
  110. $config = $this->getTestConfig();
  111. $config['name'] = $name;
  112. }
  113. if (is_string($name)) {
  114. $config['name'] = $name;
  115. }
  116. $config['name'] = $this->createQueueName($config['name']);
  117. $class = $this->getAdapterFullName();
  118. // create queue
  119. if (!class_exists($class)) {
  120. require_once 'Zend/Loader.php';
  121. Zend_Loader::loadClass($class);
  122. }
  123. set_error_handler(array($this, 'handleErrors'));
  124. try {
  125. $queue = new Zend_Queue($this->getAdapterName(), $config);
  126. } catch (Zend_Queue_Exception $e) {
  127. $this->markTestSkipped($e->getMessage());
  128. restore_error_handler();
  129. return false;
  130. }
  131. // a PHP level error occurred, mark test as failed with error as reason
  132. // (misconfigured test? undefined constant?)
  133. if ($this->error) {
  134. $err = error_get_last();
  135. $this->markTestFailed($err['message']);
  136. restore_error_handler();
  137. return false;
  138. }
  139. restore_error_handler();
  140. return $queue;
  141. }
  142. public function handleErrors($errno, $errstr)
  143. {
  144. $this->error = true;
  145. }
  146. // test the constants
  147. public function testConst()
  148. {
  149. $this->markTestSkipped('must be tested in each individual adapter');
  150. }
  151. public function testGetOptions()
  152. {
  153. $config = $this->getTestConfig();
  154. $config['setting'] = true;
  155. if (!$queue = $this->createQueue(__FUNCTION__, $config)) {
  156. return;
  157. }
  158. $adapter = $queue->getAdapter();
  159. $new = $adapter->getOptions();
  160. $this->assertTrue(is_array($new));
  161. $this->assertEquals($new['setting'], $config['setting']);
  162. // delete the queue we created
  163. $queue->deleteQueue();
  164. }
  165. // test the constructor
  166. public function testZendQueueAdapterConstructor()
  167. {
  168. $class = $this->getAdapterFullName();
  169. /**
  170. * @see Zend_Loader
  171. */
  172. require_once 'Zend/Loader.php';
  173. Zend_Loader::loadClass($class);
  174. try {
  175. $obj = new $class(true);
  176. $this->fail('__construct() $config must be an array');
  177. } catch (Exception $e) {
  178. $this->assertTrue(true);
  179. }
  180. try {
  181. $obj = new $class( array());
  182. $this->fail('__construct() cannot accept an empty array for a configuration');
  183. } catch (Exception $e) {
  184. $this->assertTrue(true);
  185. }
  186. try {
  187. $obj = new $class(array('name' => 'queue1', 'driverOptions'=>true));
  188. $this->fail('__construct() $config[\'options\'] must be an array');
  189. } catch (Exception $e) {
  190. $this->assertTrue(true);
  191. }
  192. try {
  193. $obj = new $class(array('name' => 'queue1', 'driverOptions'=>array('opt'=>'val')));
  194. $this->fail('__construct() humm I think this test is supposed to work @TODO');
  195. } catch (Exception $e) {
  196. $this->assertTrue(true);
  197. }
  198. try {
  199. $config = new Zend_Config(array('driverOptions' => array() ));
  200. $obj = new $class($config);
  201. $this->fail('__construct() \'name\' is a required configuration value');
  202. } catch (Exception $e) {
  203. $this->assertTrue(true);
  204. }
  205. try {
  206. $config = new Zend_Config(array('name' => 'queue1', 'driverOptions' => array(), 'options' => array('opt1' => 'val1')));
  207. $obj = new $class($config);
  208. $this->fail('__construct() is not supposed to accept a true value for a configuraiton');
  209. } catch (Exception $e) {
  210. $this->assertTrue(true);
  211. }
  212. // try passing the queue to the $adapter
  213. if (!$queue = $this->createQueue(__FUNCTION__)) {
  214. return;
  215. }
  216. $obj = new $class($queue->getOptions(), $queue);
  217. $this->assertTrue($obj instanceof Zend_Queue_Adapter_AdapterInterface);
  218. }
  219. // this tests the configuration option $config['messageClass']
  220. public function testZendQueueMessageTest()
  221. {
  222. $config = $this->getTestConfig();
  223. $config['messageClass'] = 'Zend_Queue_Message_Test';
  224. if (!$queue = $this->createQueue(__FUNCTION__, $config)) {
  225. return;
  226. }
  227. $adapter = $queue->getAdapter();
  228. // check to see if this function is supported
  229. if (! ($adapter->isSupported('send')
  230. && $adapter->isSupported('receive'))) {
  231. // delete the queue we created
  232. $queue->deleteQueue();
  233. $this->markTestSkipped('send() receive() are not supported');
  234. }
  235. $body = 'this is a test message';
  236. $message = $queue->send($body);
  237. $this->assertTrue($message instanceof Zend_Queue_Message);
  238. $list = $queue->receive();
  239. $this->assertTrue($list instanceof Zend_Queue_Message_Iterator);
  240. foreach ( $list as $i => $message ) {
  241. $this->assertTrue($message instanceof Zend_Queue_Message_Test);
  242. $queue->deleteMessage($message);
  243. }
  244. // delete the queue we created
  245. $queue->deleteQueue();
  246. }
  247. public function testFactory()
  248. {
  249. if (!$queue = $this->createQueue(__FUNCTION__)) {
  250. return;
  251. }
  252. $this->assertTrue($queue->getAdapter() instanceof Zend_Queue_Adapter_AdapterInterface);
  253. }
  254. public function testCreate()
  255. {
  256. if (!$queue = $this->createQueue(__FUNCTION__)) {
  257. return;
  258. }
  259. $adapter = $queue->getAdapter();
  260. // check to see if this function is supported
  261. $func = 'create';
  262. if (! $adapter->isSupported($func)) {
  263. $this->markTestSkipped($func . '() is not supported');
  264. return;
  265. }
  266. if ($adapter->isSupported('getQueues')) {
  267. $this->assertTrue(in_array($queue->getName(), $adapter->getQueues()));
  268. }
  269. // cannot recreate a queue.
  270. $this->assertFalse($adapter->create($queue->getName()));
  271. // delete the queue we created
  272. $queue->deleteQueue();
  273. }
  274. public function testDelete()
  275. {
  276. if (!$queue = $this->createQueue(__FUNCTION__)) {
  277. return;
  278. }
  279. $adapter = $queue->getAdapter();
  280. // check to see if this function is supported
  281. $func = 'delete';
  282. if (! $adapter->isSupported($func)) {
  283. $this->markTestSkipped($func . '() is not supported');
  284. return;
  285. }
  286. $new = $this->createQueueName(__FUNCTION__ . '_2');
  287. $this->assertTrue($adapter->create($new));
  288. $this->assertTrue($adapter->delete($new));
  289. if ($adapter->isSupported('getQueues')) {
  290. if (in_array($new, $adapter->getQueues())) {
  291. $this->fail('delete() failed to delete it\'s queue, but returned true: '. $new);
  292. }
  293. }
  294. // delete the queue we created
  295. $queue->deleteQueue();
  296. }
  297. public function testIsExists()
  298. {
  299. if (!$queue = $this->createQueue(__FUNCTION__)) {
  300. return;
  301. }
  302. $adapter = $queue->getAdapter();
  303. // check to see if this function is supported
  304. $func = 'isExists';
  305. if (! $adapter->isSupported($func)) {
  306. $this->markTestSkipped($func . '() is not supported');
  307. return;
  308. }
  309. $this->assertFalse($adapter->isExists('perl'));
  310. $new = $this->createQueueName(__FUNCTION__ . '_2');
  311. $this->assertTrue($adapter->create($new));
  312. $this->assertTrue($adapter->isExists($new));
  313. $this->assertTrue($adapter->delete($new));
  314. if ($adapter->isSupported('getQueues')) {
  315. if (in_array($new, $adapter->getQueues())) {
  316. $this->fail('delete() failed to delete it\'s queue, but returned true: '. $new);
  317. }
  318. }
  319. // delete the queue we created
  320. $queue->deleteQueue();
  321. }
  322. public function testSend()
  323. {
  324. if (!$queue = $this->createQueue(__FUNCTION__)) {
  325. return;
  326. }
  327. $adapter = $queue->getAdapter();
  328. // check to see if this function is supported
  329. $func = 'send';
  330. if (! $adapter->isSupported($func)) {
  331. $this->markTestSkipped($func . '() is not supported');
  332. return;
  333. }
  334. $body = 'this is a test message';
  335. $message = $adapter->send($body);
  336. $this->assertTrue($message instanceof Zend_Queue_Message);
  337. // receive the record we created.
  338. if (! $adapter->isSupported('receive')) {
  339. $messages = $adapter->receive();
  340. foreach ( $list as $i => $message ) {
  341. $this->assertTrue($message instanceof Zend_Queue_Message_Test);
  342. $queue->deleteMessage($message);
  343. }
  344. }
  345. // delete the queue we created
  346. $queue->deleteQueue();
  347. }
  348. public function testReceive()
  349. {
  350. if (!$queue = $this->createQueue(__FUNCTION__)) {
  351. return;
  352. }
  353. $adapter = $queue->getAdapter();
  354. // check to see if this function is supported
  355. $func = 'receive';
  356. if (! $adapter->isSupported($func)) {
  357. $this->markTestSkipped($func . '() is not supported');
  358. return;
  359. }
  360. // send the message
  361. $body = 'this is a test message 2';
  362. $message = $adapter->send($body);
  363. $this->assertTrue($message instanceof Zend_Queue_Message);
  364. // get it back
  365. $list = $adapter->receive(1);
  366. $this->assertEquals(1, count($list));
  367. $this->assertTrue($list instanceof Zend_Queue_Message_Iterator);
  368. $this->assertTrue($list->valid());
  369. $message = $list->current();
  370. if ($adapter->isSupported('deleteMessage')) {
  371. $adapter->deleteMessage($list->current());
  372. }
  373. $this->assertTrue($message instanceof Zend_Queue_Message);
  374. $this->assertEquals($message->body, $body);
  375. // delete the queue we created
  376. $queue->deleteQueue();
  377. }
  378. public function testDeleteMessage()
  379. {
  380. if (!$queue = $this->createQueue(__FUNCTION__)) {
  381. return;
  382. }
  383. $adapter = $queue->getAdapter();
  384. // check to see if this function is supported
  385. $func = 'deleteMessage';
  386. if (! $adapter->isSupported($func)) {
  387. $this->markTestSkipped($func . '() is not supported');
  388. return;
  389. }
  390. // in order to test this we need to send and receive so that the
  391. // test code can send a sample message.
  392. if (! ($adapter->isSupported('send') && $adapter->isSupported('receive'))) {
  393. $this->markTestSkipped('send() and receive() are not supported');
  394. }
  395. $body = 'this is a test message';
  396. $message = $adapter->send($body);
  397. $this->assertTrue($message instanceof Zend_Queue_Message);
  398. $list = $adapter->receive();
  399. $this->assertTrue($list instanceof Zend_Queue_Message_Iterator);
  400. $this->assertTrue($list->valid());
  401. $message = $list->current();
  402. $this->assertTrue($message instanceof Zend_Queue_Message);
  403. $this->assertTrue($adapter->deleteMessage($message));
  404. // no more messages, should return false
  405. // stomp and amazon always return true.
  406. $falsePositive = array('Activemq', 'Amazon');
  407. if (! in_array($this->getAdapterName(), $falsePositive)) {
  408. $this->assertFalse($adapter->deleteMessage($message));
  409. }
  410. // delete the queue we created
  411. $queue->deleteQueue();
  412. }
  413. public function testGetQueues()
  414. {
  415. if (!$queue = $this->createQueue(__FUNCTION__)) {
  416. return;
  417. }
  418. $adapter = $queue->getAdapter();
  419. // check to see if this function is supported
  420. $func = 'getQueues';
  421. if (! $adapter->isSupported($func)) {
  422. $this->markTestSkipped($func . '() is not supported');
  423. return;
  424. }
  425. // get a listing of queues
  426. $queues = $adapter->getQueues();
  427. // this is an array right?
  428. $this->assertTrue(is_array($queues));
  429. // make sure our current queue is in this list.
  430. $this->assertTrue(in_array($queue->getName(), $queues));
  431. // delete the queue we created
  432. $queue->deleteQueue();
  433. }
  434. public function testCount()
  435. {
  436. if (!$queue = $this->createQueue(__FUNCTION__)) {
  437. return;
  438. }
  439. $adapter = $queue->getAdapter();
  440. // check to see if this function is supported
  441. $func = 'count';
  442. if (! $adapter->isSupported($func)) {
  443. $this->markTestSkipped($func . '() is not supported');
  444. return;
  445. }
  446. // for a test case, the count should be zero at first.
  447. $this->assertEquals($adapter->count(), 0);
  448. if (! $adapter->isSupported('send') && $adapter->isSupported('receive') ) {
  449. $this->markTestSkipped('send() and receive() are not supported');
  450. }
  451. $body = 'this is a test message';
  452. // send a message
  453. $message = $adapter->send($body);
  454. // test queue count for being 1
  455. $this->assertEquals($adapter->count(), 1);
  456. // receive the message
  457. $message = $adapter->receive();
  458. /* we need to delete the messages we put in the queue before
  459. * counting.
  460. *
  461. * not all adapters support deleteMessage, but we should remove
  462. * the messages that we created if we can.
  463. */
  464. if ( $adapter->isSupported('deleteMessage') ) {
  465. foreach ( $message as $msg ) {
  466. $adapter->deleteMessage($msg);
  467. }
  468. }
  469. // test the count for being 0
  470. $this->assertEquals($adapter->count(), 0);
  471. // delete the queue we created
  472. $queue->deleteQueue();
  473. }
  474. public function testCapabilities()
  475. {
  476. if (!$queue = $this->createQueue(__FUNCTION__)) {
  477. return;
  478. }
  479. $adapter = $queue->getAdapter();
  480. $list = $adapter->getCapabilities();
  481. $this->assertTrue(is_array($list));
  482. // these functions must have an boolean answer
  483. $func = array(
  484. 'create', 'delete', 'send', 'receive',
  485. 'deleteMessage', 'getQueues', 'count',
  486. 'isExists'
  487. );
  488. foreach ( array_values($func) as $f ) {
  489. $this->assertTrue(isset($list[$f]));
  490. $this->assertTrue(is_bool($list[$f]));
  491. }
  492. // delete the queue we created
  493. $queue->deleteQueue();
  494. }
  495. public function testIsSupported()
  496. {
  497. if (!$queue = $this->createQueue(__FUNCTION__)) {
  498. return;
  499. }
  500. $adapter = $queue->getAdapter();
  501. $list = $adapter->getCapabilities();
  502. foreach ( $list as $function => $result ) {
  503. $this->assertTrue(is_bool($result));
  504. if ( $result ) {
  505. $this->assertTrue($adapter->isSupported($function));
  506. } else {
  507. $this->assertFalse($adapter->isSupported($function));
  508. }
  509. }
  510. // delete the queue we created
  511. $queue->deleteQueue();
  512. }
  513. public function testGetQueue()
  514. {
  515. if (!$queue = $this->createQueue(__FUNCTION__)) {
  516. return;
  517. }
  518. $adapter = $queue->getAdapter();
  519. $this->assertTrue($queue === $queue->getAdapter()->getQueue());
  520. // delete the queue we created
  521. $queue->deleteQueue();
  522. }
  523. /*
  524. * Send about 10 messages, read 5 back, then read 5 back 1 at a time.
  525. * delete all messages and created queue
  526. */
  527. public function testSampleBehavior()
  528. {
  529. if (!$queue = $this->createQueue(__FUNCTION__)) {
  530. return;
  531. }
  532. $this->assertTrue($queue instanceof Zend_Queue);
  533. if ($queue->isSupported('send')) {
  534. $msg = 1;
  535. for($i = 0; $i < 10; $i++) {
  536. $queue->send("$msg");
  537. $msg ++;
  538. }
  539. }
  540. if ($queue->isSupported('receive')) {
  541. $msg = 1;
  542. $messages = $queue->receive(5);
  543. foreach($messages as $i => $message) {
  544. $this->assertEquals($msg, $message->body);
  545. $queue->deleteMessage($message);
  546. $msg++;
  547. }
  548. for($i = 0; $i < 5; $i++) {
  549. $messages = $queue->receive();
  550. $message = $messages->current();
  551. $this->assertEquals($msg, $message->body);
  552. $queue->deleteMessage($message);
  553. $msg++;
  554. }
  555. }
  556. $this->assertEquals(0, count($queue));
  557. $this->assertTrue($queue->deleteQueue());
  558. // delete the queue we created
  559. $queue->deleteQueue();
  560. }
  561. /**
  562. * This tests to see if a message is in-visibile for the proper amount of time
  563. *
  564. * adapters that support deleteMessage() by nature will support visibility
  565. */
  566. public function testVisibility()
  567. {
  568. $debug = false;
  569. $default_timeout = 3; // how long we tell the queue to keep the message invisible
  570. $extra_delay = 2; // how long we are willing to wait for the test to finish before failing
  571. // keep in mind that some queue services are on forigen machines and need network time.
  572. if (false) { // easy comment/uncomment, set to true or false
  573. $this->markTestSkipped('Visibility testing takes ' . $default_timeout+$extra_delay . ' seconds per adapter, if you wish to test this, uncomment the test case in ' . __FILE__ . ' line ' . __LINE__);
  574. return;
  575. }
  576. $config = $this->getTestConfig();
  577. $config['timeout'] = 2;
  578. if (!$queue = $this->createQueue(__FUNCTION__, $config)) {
  579. return;
  580. }
  581. $adapter = $queue->getAdapter();
  582. $not_supported = array('Activemq');
  583. if ((! $queue->isSupported('deleteMessage')) || in_array($this->getAdapterName(), $not_supported)) {
  584. $queue->deleteQueue();
  585. $this->markTestSkipped($this->getAdapterName() . ' does not support visibility of messages');
  586. return;
  587. }
  588. $body = 'hello world';
  589. $queue->send($body);
  590. $messages = $queue->receive(1); // messages are deleted at the bottom.
  591. if ($queue->isSupported('count')) {
  592. $this->assertEquals(1, count($queue));
  593. }
  594. $start = microtime(true);
  595. $end = 0;
  596. $this->assertTrue($messages instanceof Zend_Queue_Message_Iterator);
  597. $timeout = $config['timeout'] + $start + $extra_delay;
  598. $found = false;
  599. $check = microtime(true);
  600. $end = false;
  601. do {
  602. $search = $queue->receive(1);
  603. if ((microtime(true) - $check) > 0.1) {
  604. $check = microtime(true);
  605. if ($debug) echo "Checking - found ", count($search), " messages at : ", $check, "\n";
  606. }
  607. if ( count($search) > 0 ) {
  608. if ($search->current()->body == $body) {
  609. $found = true;
  610. $end = microtime(true);
  611. } else {
  612. $this->fail('sent message is not the message received');
  613. }
  614. }
  615. } while ($found === false && microtime(true) < $timeout);
  616. // record end time
  617. if ($end === false) {
  618. $end = microtime(true);
  619. }
  620. $duration = sprintf("%5.2f seconds", $end-$start);
  621. /*
  622. There has to be some fuzzyness regarding comparisons because while
  623. the timeout may be honored, the actual code time, database querying
  624. and so on, may take more than the timeout time.
  625. */
  626. if ($found) {
  627. if (abs(($end-$start) - $config['timeout']) < $extra_delay) { // stupid Db Adapter responds in a fraction less than a second.
  628. $this->assertTrue(true, 'message was invisible for the required amount of time');
  629. } else {
  630. if ($debug) echo 'required duration of invisibility: ', $config['timeout'], ' seconds; actual duration: ', $duration, "\n";
  631. $this->fail('message was NOT invisible for the required amount of time');
  632. }
  633. } else {
  634. $this->fail('message never became visibile duration:' . $duration);
  635. }
  636. if ($debug) echo "duration $duration\n";
  637. // now we delete the messages
  638. if ( $adapter->isSupported('deleteMessage') ) {
  639. foreach ( $messages as $msg ) {
  640. $adapter->deleteMessage($msg);
  641. }
  642. }
  643. // delete the queue we created
  644. $queue->deleteQueue();
  645. }
  646. /**
  647. * tests a function for an exception
  648. *
  649. * @param string $func function name
  650. * @param array $args function arguments
  651. * @return boolean - true if exception, false if not
  652. */
  653. protected function try_exception($func, $args)
  654. {
  655. $return = false;
  656. }
  657. public function testIsSupportException()
  658. {
  659. if (!$queue = $this->createQueue(__FUNCTION__)) {
  660. return;
  661. }
  662. $adapter = $queue->getAdapter();
  663. $functions = $adapter->getCapabilities();
  664. if (! $functions['create']) {
  665. try {
  666. $adapter->create(__FUNCTION__ . '_2');
  667. $this->fail('unsupported create() failed to throw an exception');
  668. } catch (Exception $e) {
  669. $this->assertTrue(true, 'exception thrown');
  670. }
  671. }
  672. if (! $functions['delete']) {
  673. try {
  674. $adapter->delete(__FUNCTION__ . '_2');
  675. $this->fail('unsupported delete() failed to throw an exception');
  676. } catch (Exception $e) {
  677. $this->assertTrue(true, 'exception thrown');
  678. }
  679. }
  680. if (! $functions['send']) {
  681. try {
  682. $adapter->send(__FUNCTION__);
  683. $this->fail('unsupported send() failed to throw an exception');
  684. } catch (Exception $e) {
  685. $this->assertTrue(true, 'exception thrown');
  686. }
  687. }
  688. if (! $functions['receive']) {
  689. try {
  690. $adapter->send(__FUNCTION__);
  691. $this->fail('unsupported receive() failed to throw an exception');
  692. } catch (Exception $e) {
  693. $this->assertTrue(true, 'exception thrown');
  694. }
  695. }
  696. if (! $functions['receive']) {
  697. try {
  698. $adapter->receive();
  699. $this->fail('unsupported receive() failed to throw an exception');
  700. } catch (Exception $e) {
  701. $this->assertTrue(true, 'exception thrown');
  702. }
  703. }
  704. if (! $functions['deleteMessage']) {
  705. try {
  706. $message = new Zend_Queue_Message();
  707. $adapter->deleteMessage($message);
  708. $this->fail('unsupported deleteMessage() failed to throw an exception');
  709. } catch (Exception $e) {
  710. $this->assertTrue(true, 'exception thrown');
  711. }
  712. }
  713. if (! $functions['getQueues']) {
  714. try {
  715. $adapter->getQueues();
  716. $this->fail('unsupported getQueues() failed to throw an exception');
  717. } catch (Exception $e) {
  718. $this->assertTrue(true, 'exception thrown');
  719. }
  720. }
  721. if (! $functions['count']) {
  722. try {
  723. $a = $adapter->count();
  724. $this->fail('unsupported count() failed to throw an exception');
  725. } catch (Exception $e) {
  726. $this->assertTrue(true, 'exception thrown');
  727. }
  728. }
  729. if (! $functions['isExists']) {
  730. try {
  731. $a = $adapter->isExists(__FUNCTION__ . '_3');
  732. $this->fail('unsupported isExists() failed to throw an exception');
  733. } catch (Exception $e) {
  734. $this->assertTrue(true, 'exception thrown');
  735. }
  736. }
  737. // delete the queue we created
  738. $queue->deleteQueue();
  739. }
  740. }