コード例 #1
0
        public void NoRetriesAtAllIfRetryQuotaIs0()
        {
            var config          = CreateConfig();
            var capacityManager = new CapacityManager(throttleRetryCount: 0, throttleRetryCost: 5, throttleCost: 1, timeoutRetryCost: 10);

            RunRetryTest((executionContext, retryPolicy) =>
            {
                Tester.Reset();
                Tester.Action = (int callCount) =>
                {
                    switch (callCount)
                    {
                    case 1:
                        throw new AmazonServiceException($"Mocked service error ({callCount})", new WebException(), HttpStatusCode.InternalServerError);

                    default:
                        throw new Exception($"Invalid number of calls ({callCount})");
                    }
                };

                var exception = Utils.AssertExceptionExpected <AmazonServiceException>(() =>
                {
                    RuntimePipeline.InvokeSync(executionContext);
                });

                Assert.AreEqual("Mocked service error (1)", exception.Message);

                var capacity = MockAdaptiveRetryPolicy.CurrentCapacityManagerInstance.GetRetryCapacity(config.ServiceURL);
                Assert.AreEqual(0, executionContext.RequestContext.Retries);
                Assert.AreEqual(1, Tester.CallCount);
                Assert.AreEqual(0, capacity.AvailableCapacity);
            }, config, capacityManager);
        }
コード例 #2
0
        public void AcquireCapacityInvalidUnitTest()
        {
            int             throttleRetryCount      = 0;
            int             throttleRetryCost       = 5;
            int             throttleCost            = 1;
            CapacityManager capacityManagerInstance = new CapacityManager(throttleRetryCount, throttleRetryCost, throttleCost);

            retryCapacity = capacityManagerInstance.GetRetryCapacity("AcquireCapacityInvalidUnitTest");
            Assert.IsNotNull(retryCapacity);
            Assert.IsFalse(capacityManagerInstance.TryAcquireCapacity(retryCapacity));
            Assert.AreEqual(0, retryCapacity.AvailableCapacity);
            var consumedCapacity = retryCapacity.MaxCapacity - retryCapacity.AvailableCapacity;

            Assert.AreEqual(0, consumedCapacity);
        }
コード例 #3
0
        public void ReleaseCapacityInvalidUnitTest()
        {
            int             throttleRetryCount      = 5;
            int             throttleRetryCost       = 5;
            int             throttleCost            = 1;
            CapacityManager capacityManagerInstance = new CapacityManager(throttleRetryCount, throttleRetryCost, throttleCost);

            retryCapacity = capacityManagerInstance.GetRetryCapacity("ReleaseCapacityInvalidUnitTest");
            Assert.IsNotNull(retryCapacity);

            capacityManagerInstance.TryReleaseCapacity(false, retryCapacity);
            Assert.AreEqual(throttleRetryCost * throttleRetryCount, retryCapacity.AvailableCapacity);
            var consumedCapacity = retryCapacity.MaxCapacity - retryCapacity.AvailableCapacity;

            Assert.AreEqual(0, consumedCapacity);
        }
コード例 #4
0
        public void ReleaseCapacityUnitTest()
        {
            int             throttleRetryCount      = 5;
            int             throttleRetryCost       = 5;
            int             throttleCost            = 1;
            CapacityManager capacityManagerInstance = new CapacityManager(throttleRetryCount, throttleRetryCost, throttleCost);

            retryCapacity = capacityManagerInstance.GetRetryCapacity("ReleaseCapacityUnitTest");
            Assert.IsNotNull(retryCapacity);
            Assert.IsTrue(capacityManagerInstance.TryAcquireCapacity(retryCapacity));

            capacityManagerInstance.ReleaseCapacity(CapacityManager.CapacityType.Retry, retryCapacity);
            Assert.AreEqual(throttleRetryCost * throttleRetryCount, retryCapacity.AvailableCapacity);
            var consumedCapacity = retryCapacity.MaxCapacity - retryCapacity.AvailableCapacity;

            Assert.AreEqual(0, consumedCapacity);

            capacityManagerInstance.ReleaseCapacity(CapacityManager.CapacityType.Increment, retryCapacity);
            Assert.AreEqual(throttleRetryCost * throttleRetryCount, retryCapacity.AvailableCapacity);
            consumedCapacity = retryCapacity.MaxCapacity - retryCapacity.AvailableCapacity;
            Assert.AreEqual(0, consumedCapacity);
        }
コード例 #5
0
        public void RunRetryTest(Action <IExecutionContext, MockStandardRetryPolicy> DoAction, AmazonS3Config config,
                                 CapacityManager capacityManager = null, double?exponentialPower = null, double?exponentialBase = null)
        {
            try
            {
                if (capacityManager != null)
                {
                    MockStandardRetryPolicy.SetCapacityManagerInstance(capacityManager);
                }

                var retryPolicy = new MockStandardRetryPolicy(config);
                retryPolicy.ExponentialBase  = exponentialBase ?? retryPolicy.ExponentialBase;
                retryPolicy.ExponentialPower = exponentialPower ?? retryPolicy.ExponentialPower;

                Handler = new RetryHandler(retryPolicy);
                if (RuntimePipeline.Handlers.Find(h => h is RetryHandler) != null)
                {
                    RuntimePipeline.ReplaceHandler <RetryHandler>(Handler);
                }
                else
                {
                    RuntimePipeline.AddHandler(Handler);
                }

                var executionContext = CreateTestContext(null, null, config);

                DoAction(executionContext, retryPolicy);
            }
            finally
            {
                if (capacityManager != null)
                {
                    MockStandardRetryPolicy.RestoreManagers();
                }
            }
        }
コード例 #6
0
 public static void SetCapacityManagerInstance(CapacityManager capacityManager)
 {
     _originalCapacityManager = CapacityManagerInstance;
     CapacityManagerInstance  = capacityManager;
 }