AdapterTest.php 27 KB

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