RetryPolicyTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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_Service_WindowsAzure
  17. * @subpackage UnitTests
  18. * @version $Id: RetryPolicyTest.php 35709 2009-12-14 14:14:14Z unknown $
  19. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/TestHelper.php';
  23. if (!defined('PHPUnit_MAIN_METHOD')) {
  24. define('PHPUnit_MAIN_METHOD', 'Zend_Service_WindowsAzure_RetryPolicyTest::main');
  25. }
  26. require_once 'Zend/Service/WindowsAzure/RetryPolicy/RetryPolicyAbstract.php';
  27. /**
  28. * @category Zend
  29. * @package Zend_Service_WindowsAzure
  30. * @subpackage UnitTests
  31. * @group Zend_Service
  32. * @group Zend_Service_WindowsAzure
  33. * @version $Id: RetryPolicyTest.php 35709 2009-12-14 14:14:14Z unknown $
  34. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Service_WindowsAzure_RetryPolicyTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Helper variable for counting retries
  41. *
  42. * @var int
  43. */
  44. protected $_executedRetries = 0;
  45. /**
  46. * Helper variable for setting Exception count
  47. *
  48. * @var int
  49. */
  50. protected $_exceptionCount = 0;
  51. public static function main()
  52. {
  53. $suite = new PHPUnit_Framework_TestSuite("Zend_Service_WindowsAzure_RetryPolicyTest");
  54. $result = PHPUnit_TextUI_TestRunner::run($suite);
  55. }
  56. /**
  57. * Test retry policy - noRetry
  58. */
  59. public function testNoRetry()
  60. {
  61. $this->_executedRetries = 0;
  62. $policy = Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::noRetry();
  63. $retries = $policy->execute(
  64. array($this, '_countRetries')
  65. );
  66. $this->assertEquals(1, $retries);
  67. }
  68. /**
  69. * Test retry policy - retryN
  70. */
  71. public function testRetryN()
  72. {
  73. $this->_executedRetries = 0;
  74. $this->_exceptionCount = 9;
  75. $policy = Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::retryN(10, 100);
  76. $retries = $policy->execute(
  77. array($this, '_countRetriesAndThrowExceptions')
  78. );
  79. $this->assertEquals(10, $retries);
  80. }
  81. /**
  82. * Helper function, counting retries
  83. */
  84. public function _countRetries()
  85. {
  86. return ++$this->_executedRetries;
  87. }
  88. /**
  89. * Helper function, counting retries and generating number of exceptions
  90. */
  91. public function _countRetriesAndThrowExceptions()
  92. {
  93. ++$this->_executedRetries;
  94. if ($this->_exceptionCount-- > 0) {
  95. throw new Exception("Exception thrown.");
  96. }
  97. return $this->_executedRetries;
  98. }
  99. }
  100. // Call Zend_Service_WindowsAzure_RetryPolicyTest::main() if this source file is executed directly.
  101. if (PHPUnit_MAIN_METHOD == "Zend_Service_WindowsAzure_RetryPolicyTest::main") {
  102. Zend_Service_WindowsAzure_RetryPolicyTest::main();
  103. }