public void When_a_not_supported_exception_is_thrown_It_does_not_retry()
        {
            var throwsNotSupported = new AlwaysThrowsQuery<object, NotSupportedException>(TimeSpan.FromMilliseconds(200),retryInterval);

            Assert.Throws<NotSupportedException>(() => retryUntilTimeoutRobustWrapper.Robustly(throwsNotSupported));
            Assert.That(throwsNotSupported.Tries, Is.EqualTo(1));
        }
        public void When_a_query_always_throws_an_exception_It_rethrows_eventually()
        {
            var query = new AlwaysThrowsQuery<object,TestException>(TimeSpan.FromMilliseconds(1000), TimeSpan.FromMilliseconds(20));

            Assert.Throws<TestException>(() => new RetryUntilTimeoutRobustWrapper().Robustly(query));
            Assert.That(query.Tries, Is.GreaterThan(2));
        }
        public void When_a_not_supported_exception_is_thrown_It_does_not_retry()
        {
            var throwsNotSupported = new AlwaysThrowsQuery<object, NotSupportedException>(new Options{Timeout = TimeSpan.FromMilliseconds(200), RetryInterval = retryInterval});

            Assert.Throws<NotSupportedException>(() => _retryUntilTimeoutTimingStrategy.Synchronise(throwsNotSupported));
            Assert.That(throwsNotSupported.Tries, Is.EqualTo(1));
        }
        public void When_a_query_always_throws_an_exception_It_rethrows_eventually()
        {
            var query = new AlwaysThrowsQuery<object, TestException>(new Options { Timeout = TimeSpan.FromMilliseconds(1000), RetryInterval = TimeSpan.FromMilliseconds(20) });

            Assert.Throws<TestException>(() => new RetryUntilTimeoutTimingStrategy().Synchronise(query));
            Assert.That(query.Tries, Is.GreaterThan(2));
        }
        public void When_an_action_throws_a_not_supported_exception_It_does_not_retry()
        {
            var retryInterval = TimeSpan.FromMilliseconds(10);

            var robustness = new RetryUntilTimeoutRobustWrapper();

            var query = new AlwaysThrowsQuery<object, NotSupportedException>(TimeSpan.FromMilliseconds(100), retryInterval);
            Assert.Throws<NotSupportedException>(() => robustness.Robustly(query));
            Assert.That(query.Tries, Is.EqualTo(1));
        }
        public void When_a_query_throws_a_not_supported_exception_It_does_not_retry()
        {
            var retryInterval = TimeSpan.FromMilliseconds(10);
            
            var robustness = new RetryUntilTimeoutTimingStrategy();

            var query = new AlwaysThrowsQuery<object, NotSupportedException>(new Options{Timeout = TimeSpan.FromMilliseconds(100), RetryInterval = retryInterval});
            Assert.Throws<NotSupportedException>(() => robustness.Synchronise(query));
            Assert.That(query.Tries, Is.EqualTo(1));
        }
        private void When_a_query_always_throws_an_exception_It_retries_until_the_timeout_is_reached(int timeoutMilliseconds, int intervalMilliseconds)
        {
            var expectedTimeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
            var retryInterval = TimeSpan.FromMilliseconds(intervalMilliseconds);

            var query = new AlwaysThrowsQuery<object, TestException>(new Options { Timeout = expectedTimeout, RetryInterval = retryInterval });

            var retryUntilTimeoutRobustWrapper = new RetryUntilTimeoutTimingStrategy();

            try
            {
                retryUntilTimeoutRobustWrapper.Synchronise(query);
                Assert.Fail("Expecting test exception");
            }
            catch (TestException){}

            Assert.That(query.LastCall,
                        Is.GreaterThan(expectedTimeout.TotalMilliseconds -
                                       (retryInterval.Milliseconds + When_waiting.AccuracyMilliseconds)));
            Assert.That(query.LastCall, Is.LessThan(
                expectedTimeout.TotalMilliseconds + retryInterval.Milliseconds + When_waiting.AccuracyMilliseconds));

        }
        private void When_a_query_always_throws_an_exception_It_retries_until_the_timeout_is_reached(int timeoutMilliseconds, int intervalMilliseconds)
        {
            var expectedTimeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
            var retryInterval = TimeSpan.FromMilliseconds(intervalMilliseconds);

            var query = new AlwaysThrowsQuery<object, TestException>(expectedTimeout, retryInterval);

            var retryUntilTimeoutRobustWrapper = new RetryUntilTimeoutRobustWrapper();

            try
            {
                retryUntilTimeoutRobustWrapper.Robustly(query);
                Assert.Fail("Expecting test exception");
            }
            catch (TestException){}

            Assert.That(query.LastCall, Is.InRange(expectedTimeout.TotalMilliseconds - (retryInterval.Milliseconds + When_waiting.AccuracyMilliseconds),
                                                   expectedTimeout.TotalMilliseconds + retryInterval.Milliseconds + When_waiting.AccuracyMilliseconds));
        }