RetryPolicyTest.php 3.4 KB

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