RetryPolicyTest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /** Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract */
  23. require_once 'Zend/Service/WindowsAzure/RetryPolicy/RetryPolicyAbstract.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Service_WindowsAzure
  27. * @subpackage UnitTests
  28. * @version $Id$
  29. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Service_WindowsAzure_RetryPolicyTest extends PHPUnit_Framework_TestCase
  33. {
  34. /**
  35. * Helper variable for counting retries
  36. *
  37. * @var int
  38. */
  39. protected $_executedRetries = 0;
  40. /**
  41. * Helper variable for setting Exception count
  42. *
  43. * @var int
  44. */
  45. protected $_exceptionCount = 0;
  46. /**
  47. * Test retry policy - noRetry
  48. */
  49. public function testNoRetry()
  50. {
  51. $this->_executedRetries = 0;
  52. $policy = Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::noRetry();
  53. $retries = $policy->execute(
  54. array($this, '_countRetries')
  55. );
  56. $this->assertEquals(1, $retries);
  57. }
  58. /**
  59. * Test retry policy - retryN
  60. */
  61. public function testRetryN()
  62. {
  63. $this->_executedRetries = 0;
  64. $this->_exceptionCount = 9;
  65. $policy = Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::retryN(10, 100);
  66. $retries = $policy->execute(
  67. array($this, '_countRetriesAndThrowExceptions')
  68. );
  69. $this->assertEquals(10, $retries);
  70. }
  71. /**
  72. * Helper function, counting retries
  73. */
  74. public function _countRetries()
  75. {
  76. return ++$this->_executedRetries;
  77. }
  78. /**
  79. * Helper function, counting retries and generating number of exceptions
  80. */
  81. public function _countRetriesAndThrowExceptions()
  82. {
  83. ++$this->_executedRetries;
  84. if ($this->_exceptionCount-- > 0) {
  85. throw new Exception("Exception thrown.");
  86. }
  87. return $this->_executedRetries;
  88. }
  89. }