AdapterTest.php 27 KB

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