AdapterTest.php 27 KB

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