예제 #1
0
        public void WriteVariablesToDiskCache(GitPreparer gitPreparer, GitVersionCacheKey cacheKey, VersionVariables variablesFromCache)
        {
            var cacheDir      = PrepareCacheDirectory(gitPreparer);
            var cacheFileName = GetCacheFileName(cacheKey, cacheDir);

            variablesFromCache.FileName = cacheFileName;

            Dictionary <string, string> dictionary;

            using (Logger.IndentLog("Creating dictionary"))
            {
                dictionary = variablesFromCache.ToDictionary(x => x.Key, x => x.Value);
            }

            Action writeCacheOperation = () =>
            {
                using (var stream = fileSystem.OpenWrite(cacheFileName))
                {
                    using (var sw = new StreamWriter(stream))
                    {
                        using (Logger.IndentLog("Storing version variables to cache file " + cacheFileName))
                        {
                            var serializer = new Serializer();
                            serializer.Serialize(sw, dictionary);
                        }
                    }
                }
            };

            var retryOperation = new OperationWithExponentialBackoff <IOException>(new ThreadSleep(), writeCacheOperation, maxRetries: 6);

            retryOperation.Execute();
        }
    public void OperationDelayDoublesBetweenRetries()
    {
        const int numberOfRetries   = 3;
        var       expectedSleepMSec = 500;
        var       sleepCount        = 0;

        Action operation = () =>
        {
            throw new IOException();
        };

        Action <int> validator = u =>
        {
            sleepCount++;
            u.ShouldBe(expectedSleepMSec);
            expectedSleepMSec *= 2;
        };

        var    retryOperation = new OperationWithExponentialBackoff <IOException>(new MockThreadSleep(validator), operation, numberOfRetries);
        Action action         = () => retryOperation.Execute();

        action.ShouldThrow <AggregateException>();

        sleepCount.ShouldBe(numberOfRetries);
    }
예제 #3
0
        public void WriteVariablesToDiskCache(GitPreparer gitPreparer, GitVersionCacheKey cacheKey, VersionVariables variablesFromCache)
        {
            var cacheDir = PrepareCacheDirectory(gitPreparer);
            var cacheFileName = GetCacheFileName(cacheKey, cacheDir);

            variablesFromCache.FileName = cacheFileName;

            Dictionary<string, string> dictionary;
            using (Logger.IndentLog("Creating dictionary"))
            {
                dictionary = variablesFromCache.ToDictionary(x => x.Key, x => x.Value);
            }

            Action writeCacheOperation = () =>
            {
                using (var stream = fileSystem.OpenWrite(cacheFileName))
                {
                    using (var sw = new StreamWriter(stream))
                    {
                        using (Logger.IndentLog("Storing version variables to cache file " + cacheFileName))
                        {
                            var serializer = new Serializer();
                            serializer.Serialize(sw, dictionary);
                        }
                    }
                }
            };

            var retryOperation = new OperationWithExponentialBackoff<IOException>(new ThreadSleep(), writeCacheOperation, maxRetries: 6);
            retryOperation.Execute();
        }
    public void OperationIsNotRetriedOnInvalidException()
    {
        Action operation = () =>
        {
            throw new Exception();
        };

        var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(), operation);
        Action action = () => retryOperation.Execute();
        action.ShouldThrow<Exception>();
    }
    public void OperationIsNotRetriedOnInvalidException()
    {
        Action operation = () =>
        {
            throw new Exception();
        };

        var    retryOperation = new OperationWithExponentialBackoff <IOException>(new MockThreadSleep(), operation);
        Action action         = () => retryOperation.Execute();

        action.ShouldThrow <Exception>();
    }
    public void OperationIsRetriedAMaximumNumberOfTimes()
    {
        const int numberOfRetries = 3;
        var operationCount = 0;

        Action operation = () =>
        {
            operationCount++;
            throw new IOException();
        };

        var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(), operation, numberOfRetries);
        Action action = () => retryOperation.Execute();
        action.ShouldThrow<AggregateException>();

        operationCount.ShouldBe(numberOfRetries + 1);
    }
    public void OperationIsRetriedOnIOException()
    {
        var operationCount = 0;

        Action operation = () =>
        {
            operationCount++;
            if (operationCount < 2)
            {
                throw new IOException();
            }
        };

        var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(), operation);
        retryOperation.Execute();

        operationCount.ShouldBe(2);
    }
    public void OperationIsRetriedAMaximumNumberOfTimes()
    {
        const int numberOfRetries = 3;
        var       operationCount  = 0;

        Action operation = () =>
        {
            operationCount++;
            throw new IOException();
        };

        var    retryOperation = new OperationWithExponentialBackoff <IOException>(new MockThreadSleep(), operation, numberOfRetries);
        Action action         = () => retryOperation.Execute();

        action.ShouldThrow <AggregateException>();

        operationCount.ShouldBe(numberOfRetries + 1);
    }
    public void OperationIsRetriedOnIOException()
    {
        var operationCount = 0;

        Action operation = () =>
        {
            operationCount++;
            if (operationCount < 2)
            {
                throw new IOException();
            }
        };

        var retryOperation = new OperationWithExponentialBackoff <IOException>(new MockThreadSleep(), operation);

        retryOperation.Execute();

        operationCount.ShouldBe(2);
    }
    public void TotalSleepTimeForSixRetriesIsAboutThirtySeconds()
    {
        const int numberOfRetries = 6;
        int       totalSleep      = 0;

        Action operation = () =>
        {
            throw new IOException();
        };

        Action <int> validator = u =>
        {
            totalSleep += u;
        };

        var    retryOperation = new OperationWithExponentialBackoff <IOException>(new MockThreadSleep(validator), operation, numberOfRetries);
        Action action         = () => retryOperation.Execute();

        action.ShouldThrow <AggregateException>();

        // Exact number is 31,5 seconds
        totalSleep.ShouldBe(31500);
    }
    public void OperationDelayDoublesBetweenRetries()
    {
        const int numberOfRetries = 3;
        var expectedSleepMSec = 500;
        var sleepCount = 0;

        Action operation = () =>
        {
            throw new IOException();
        };

        Action<int> validator = u =>
        {
            sleepCount++;
            u.ShouldBe(expectedSleepMSec);
            expectedSleepMSec *= 2;
        };

        var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(validator), operation, numberOfRetries);
        Action action = () => retryOperation.Execute();
        action.ShouldThrow<AggregateException>();

        sleepCount.ShouldBe(numberOfRetries);
    }
    public void TotalSleepTimeForSixRetriesIsAboutThirtySeconds()
    {
        const int numberOfRetries = 6;
        int totalSleep = 0;

        Action operation = () =>
        {
            throw new IOException();
        };

        Action<int> validator = u =>
        {
            totalSleep += u;
        };

        var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(validator), operation, numberOfRetries);
        Action action = () => retryOperation.Execute();
        action.ShouldThrow<AggregateException>();

        // Exact number is 31,5 seconds
        totalSleep.ShouldBe(31500);
    }