Creating a helper to retry a piece of code.

This commit is contained in:
Praburaj 2015-01-13 11:32:34 -08:00
parent fdfe88f8e0
commit 2df24fd02a
4 changed files with 46 additions and 64 deletions

View File

@ -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
}
}
}
}
}
}

View File

@ -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);

View File

@ -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);

View File

@ -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);