From 2df24fd02a241be886dc2c65dca76da926adb3b9 Mon Sep 17 00:00:00 2001 From: Praburaj Date: Tue, 13 Jan 2015 11:32:34 -0800 Subject: [PATCH] Creating a helper to retry a piece of code. --- test/E2ETests/Common/Helpers.cs | 25 +++++++++++++++++++++++ test/E2ETests/NtlmAuthentationTest.cs | 28 +++++++------------------- test/E2ETests/PublishAndRunTests.cs | 29 +++++++-------------------- test/E2ETests/SmokeTests.cs | 28 +++++++------------------- 4 files changed, 46 insertions(+), 64 deletions(-) diff --git a/test/E2ETests/Common/Helpers.cs b/test/E2ETests/Common/Helpers.cs index f2b971b4a6..c996cb81df 100644 --- a/test/E2ETests/Common/Helpers.cs +++ b/test/E2ETests/Common/Helpers.cs @@ -1,4 +1,8 @@ using System; +using System.Net; +using System.Net.Http; +using System.Threading; +using Microsoft.Framework.Logging; namespace E2ETests { @@ -11,5 +15,26 @@ namespace E2ETests return Type.GetType("Mono.Runtime") != null; } } + + public static void Retry(Action retryBlock, ILogger logger, int retryCount = 3) + { + for (int retry = 0; retry < retryCount; retry++) + { + try + { + retryBlock(); + break; //Went through successfully + } + catch (AggregateException exception) + { + if (exception.InnerException is HttpRequestException || exception.InnerException is WebException) + { + logger.WriteWarning("Failed to complete the request.", exception); + logger.WriteWarning("Retrying request.."); + Thread.Sleep(1 * 1000); //Wait for a second before retry + } + } + } + } } } \ No newline at end of file diff --git a/test/E2ETests/NtlmAuthentationTest.cs b/test/E2ETests/NtlmAuthentationTest.cs index 4d707c7b23..5c822ab66b 100644 --- a/test/E2ETests/NtlmAuthentationTest.cs +++ b/test/E2ETests/NtlmAuthentationTest.cs @@ -1,9 +1,7 @@ using System; using System.Diagnostics; using System.IO; -using System.Net; using System.Net.Http; -using System.Threading; using Microsoft.AspNet.Testing.xunit; using Microsoft.Framework.Logging; using Xunit; @@ -58,27 +56,15 @@ namespace E2ETests var initializationCompleteTime = DateTime.MinValue; //Request to base address and check if various parts of the body are rendered & measure the cold startup time. - for (int retryCount = 0; retryCount < 3; retryCount++) + Helpers.Retry(() => { - try - { - response = _httpClient.GetAsync(string.Empty).Result; - responseContent = response.Content.ReadAsStringAsync().Result; - initializationCompleteTime = DateTime.Now; - _logger.WriteInformation("[Time]: Approximate time taken for application initialization : '{0}' seconds", + response = _httpClient.GetAsync(string.Empty).Result; + responseContent = response.Content.ReadAsStringAsync().Result; + initializationCompleteTime = DateTime.Now; + }, logger: _logger); + + _logger.WriteInformation("[Time]: Approximate time taken for application initialization : '{0}' seconds", (initializationCompleteTime - testStartTime).TotalSeconds.ToString()); - break; //Went through successfully - } - catch (AggregateException exception) - { - if (exception.InnerException is HttpRequestException || exception.InnerException is WebException) - { - _logger.WriteWarning("Failed to complete the request.", exception); - _logger.WriteWarning("Retrying request.."); - Thread.Sleep(1 * 1000); //Wait for a second before retry - } - } - } VerifyHomePage(response, responseContent, true); diff --git a/test/E2ETests/PublishAndRunTests.cs b/test/E2ETests/PublishAndRunTests.cs index 9f7a8244dd..4c8b7db845 100644 --- a/test/E2ETests/PublishAndRunTests.cs +++ b/test/E2ETests/PublishAndRunTests.cs @@ -1,9 +1,7 @@ using System; using System.Diagnostics; using System.IO; -using System.Net; using System.Net.Http; -using System.Threading; using Microsoft.AspNet.Testing.xunit; using Microsoft.Framework.Logging; using Xunit; @@ -78,28 +76,15 @@ namespace E2ETests //Request to base address and check if various parts of the body are rendered & measure the cold startup time. //Add retry logic since tests are flaky on mono due to connection issues - for (int retryCount = 0; retryCount < 3; retryCount++) + Helpers.Retry(() => { - try - { - response = _httpClient.GetAsync(string.Empty).Result; - responseContent = response.Content.ReadAsStringAsync().Result; - initializationCompleteTime = DateTime.Now; - _logger.WriteInformation("[Time]: Approximate time taken for application initialization : '{0}' seconds", + response = _httpClient.GetAsync(string.Empty).Result; + responseContent = response.Content.ReadAsStringAsync().Result; + initializationCompleteTime = DateTime.Now; + }, logger: _logger); + + _logger.WriteInformation("[Time]: Approximate time taken for application initialization : '{0}' seconds", (initializationCompleteTime - testStartTime).TotalSeconds.ToString()); - break; //Went through successfully - } - catch (AggregateException exception) - { - // Both type exceptions thrown by Mono which are resolved by retry logic - if (exception.InnerException is HttpRequestException || exception.InnerException is WebException) - { - _logger.WriteWarning("Failed to complete the request.", exception); - _logger.WriteWarning("Retrying request.."); - Thread.Sleep(1 * 1000); //Wait for a second before retry - } - } - } VerifyHomePage(response, responseContent, true); diff --git a/test/E2ETests/SmokeTests.cs b/test/E2ETests/SmokeTests.cs index 1dda6cc277..5dded0511d 100644 --- a/test/E2ETests/SmokeTests.cs +++ b/test/E2ETests/SmokeTests.cs @@ -1,8 +1,6 @@ using System; using System.Diagnostics; -using System.Net; using System.Net.Http; -using System.Threading; using Microsoft.AspNet.Testing.xunit; using Microsoft.Framework.Logging; using Microsoft.Framework.Logging.Console; @@ -133,27 +131,15 @@ namespace E2ETests var initializationCompleteTime = DateTime.MinValue; //Request to base address and check if various parts of the body are rendered & measure the cold startup time. - for (int retryCount = 0; retryCount < 3; retryCount++) + Helpers.Retry(() => { - try - { - response = _httpClient.GetAsync(string.Empty).Result; - responseContent = response.Content.ReadAsStringAsync().Result; - initializationCompleteTime = DateTime.Now; - _logger.WriteInformation("[Time]: Approximate time taken for application initialization : '{0}' seconds", + response = _httpClient.GetAsync(string.Empty).Result; + responseContent = response.Content.ReadAsStringAsync().Result; + initializationCompleteTime = DateTime.Now; + }, logger: _logger); + + _logger.WriteInformation("[Time]: Approximate time taken for application initialization : '{0}' seconds", (initializationCompleteTime - testStartTime).TotalSeconds.ToString()); - break; //Went through successfully - } - catch (AggregateException exception) - { - if (exception.InnerException is HttpRequestException || exception.InnerException is WebException) - { - _logger.WriteWarning("Failed to complete the request.", exception); - _logger.WriteWarning("Retrying request.."); - Thread.Sleep(1 * 1000); //Wait for a second before retry - } - } - } VerifyHomePage(response, responseContent);