Creating a helper to retry a piece of code.
This commit is contained in:
parent
fdfe88f8e0
commit
2df24fd02a
|
|
@ -1,4 +1,8 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Threading;
|
||||||
|
using Microsoft.Framework.Logging;
|
||||||
|
|
||||||
namespace E2ETests
|
namespace E2ETests
|
||||||
{
|
{
|
||||||
|
|
@ -11,5 +15,26 @@ namespace E2ETests
|
||||||
return Type.GetType("Mono.Runtime") != null;
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,9 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net;
|
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading;
|
|
||||||
using Microsoft.AspNet.Testing.xunit;
|
using Microsoft.AspNet.Testing.xunit;
|
||||||
using Microsoft.Framework.Logging;
|
using Microsoft.Framework.Logging;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
@ -58,27 +56,15 @@ namespace E2ETests
|
||||||
var initializationCompleteTime = DateTime.MinValue;
|
var initializationCompleteTime = DateTime.MinValue;
|
||||||
|
|
||||||
//Request to base address and check if various parts of the body are rendered & measure the cold startup time.
|
//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;
|
||||||
response = _httpClient.GetAsync(string.Empty).Result;
|
initializationCompleteTime = DateTime.Now;
|
||||||
responseContent = response.Content.ReadAsStringAsync().Result;
|
}, logger: _logger);
|
||||||
initializationCompleteTime = DateTime.Now;
|
|
||||||
_logger.WriteInformation("[Time]: Approximate time taken for application initialization : '{0}' seconds",
|
_logger.WriteInformation("[Time]: Approximate time taken for application initialization : '{0}' seconds",
|
||||||
(initializationCompleteTime - testStartTime).TotalSeconds.ToString());
|
(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);
|
VerifyHomePage(response, responseContent, true);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net;
|
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading;
|
|
||||||
using Microsoft.AspNet.Testing.xunit;
|
using Microsoft.AspNet.Testing.xunit;
|
||||||
using Microsoft.Framework.Logging;
|
using Microsoft.Framework.Logging;
|
||||||
using Xunit;
|
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.
|
//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
|
//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;
|
||||||
response = _httpClient.GetAsync(string.Empty).Result;
|
initializationCompleteTime = DateTime.Now;
|
||||||
responseContent = response.Content.ReadAsStringAsync().Result;
|
}, logger: _logger);
|
||||||
initializationCompleteTime = DateTime.Now;
|
|
||||||
_logger.WriteInformation("[Time]: Approximate time taken for application initialization : '{0}' seconds",
|
_logger.WriteInformation("[Time]: Approximate time taken for application initialization : '{0}' seconds",
|
||||||
(initializationCompleteTime - testStartTime).TotalSeconds.ToString());
|
(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);
|
VerifyHomePage(response, responseContent, true);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Net;
|
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading;
|
|
||||||
using Microsoft.AspNet.Testing.xunit;
|
using Microsoft.AspNet.Testing.xunit;
|
||||||
using Microsoft.Framework.Logging;
|
using Microsoft.Framework.Logging;
|
||||||
using Microsoft.Framework.Logging.Console;
|
using Microsoft.Framework.Logging.Console;
|
||||||
|
|
@ -133,27 +131,15 @@ namespace E2ETests
|
||||||
var initializationCompleteTime = DateTime.MinValue;
|
var initializationCompleteTime = DateTime.MinValue;
|
||||||
|
|
||||||
//Request to base address and check if various parts of the body are rendered & measure the cold startup time.
|
//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;
|
||||||
response = _httpClient.GetAsync(string.Empty).Result;
|
initializationCompleteTime = DateTime.Now;
|
||||||
responseContent = response.Content.ReadAsStringAsync().Result;
|
}, logger: _logger);
|
||||||
initializationCompleteTime = DateTime.Now;
|
|
||||||
_logger.WriteInformation("[Time]: Approximate time taken for application initialization : '{0}' seconds",
|
_logger.WriteInformation("[Time]: Approximate time taken for application initialization : '{0}' seconds",
|
||||||
(initializationCompleteTime - testStartTime).TotalSeconds.ToString());
|
(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);
|
VerifyHomePage(response, responseContent);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue