RetryPolicyTest.php 3.5 KB

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