public void MediaRetryPolicyTestExecuteActionBackoff()
        {
            MediaRetryPolicy target = new TestMediaServicesClassFactory(null).GetSaveChangesRetryPolicy();

            int exceptionCount = 5;
            int expected = 10;
            var fakeException = new WebException("test", WebExceptionStatus.ConnectionClosed);

            TimeSpan lastInterval = TimeSpan.Zero;
            DateTime lastInvoked = DateTime.UtcNow;

            Func<int> func = () =>
            {
                TimeSpan newInterval = DateTime.UtcNow - lastInvoked;
                TimeSpan delta = newInterval - lastInterval;
                Assert.IsTrue(exceptionCount > 3 || delta.TotalMilliseconds > 1, "Iterations left:{0} interval increase too small from {1} to {2}", exceptionCount, lastInterval, newInterval, delta);
                lastInvoked = DateTime.UtcNow;
                lastInterval = newInterval;
                if (--exceptionCount > 0) throw fakeException;
                return expected;
            };

            int actual = target.ExecuteAction(func);
            Assert.AreEqual(expected, actual);
            Assert.AreEqual(0, exceptionCount);
        }
        public void MediaRetryPolicyTestExecuteActionNonTransient()
        {
            MediaRetryPolicy target = new TestMediaServicesClassFactory(null).GetSaveChangesRetryPolicy();

            int exceptionCount = 2;
            int expected = 10;
            var fakeException = new WebException("test", WebExceptionStatus.RequestCanceled);

            Func<int> func = () =>
            {
                if (--exceptionCount > 0) throw fakeException;
                return expected;
            };

            try
            {
                target.ExecuteAction(func);
            }
            catch (WebException x)
            {
                Assert.AreEqual(1, exceptionCount);
                Assert.AreEqual(fakeException, x);
                throw;
            }

            Assert.Fail("Expected exception");
        }
 public void MediaRetryPolicyTestExecuteActionTrivial()
 {
     MediaRetryPolicy target = new TestMediaServicesClassFactory(null).GetSaveChangesRetryPolicy();
     int expected = 10;
     Func<int> func = () => expected;
     int actual = target.ExecuteAction(func);
     Assert.AreEqual(expected, actual);
 }
        public void MediaRetryPolicyTestExecuteActionRetry()
        {
            MediaRetryPolicy target = new TestMediaServicesClassFactory(null).GetSaveChangesRetryPolicy();

            int exceptionCount = 2;
            int expected = 10;
            var fakeException = new WebException("test", WebExceptionStatus.ConnectionClosed);

            Func<int> func = () => {
                    if (--exceptionCount > 0) throw fakeException;
                    return expected;
                };

            int actual = target.ExecuteAction(func);
            Assert.AreEqual(expected, actual);
            Assert.AreEqual(0, exceptionCount);
        }