RetryPolicyTest.php 2.8 KB

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