public async Task ConfiguredWith_2SecondsTimeBetweenRetries_2_Retries_ExponentialBackoff() { var settings = new WeatherClientSettings("apiKey"); settings.Retries = 2; settings.ThrowOnError = false; settings.TimeBetweenRetries = TimeSpan.FromSeconds(2); settings.ExponentialBackoffInRetries = true; int timesTried = 0; Stopwatch stopwatch = new Stopwatch(); SetUpWithSettings(settings, () => { timesTried++; var response = new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError); return(Task.FromResult(response)); }); stopwatch.Start(); CurrentWeather weather = await client.GetByName("Barcelona"); stopwatch.Stop(); Assert.IsNull(weather); //one of the actual request and 2 for the retries Assert.AreEqual(3, timesTried); Assert.Greater(stopwatch.Elapsed, TimeSpan.FromSeconds(5)); }
public void ConfiguredWithThrowOnError_ResponseHasError_Throws() { var settings = new WeatherClientSettings("apiKey"); settings.Retries = 0; settings.ThrowOnError = true; SetUpWithSettings(settings, () => { var response = new HttpResponseMessage(System.Net.HttpStatusCode.NotFound); return(Task.FromResult(response)); }); Assert.ThrowsAsync <HttpRequestException> (async() => await client.GetByName("Barcelona")); }
public void NotConfiguredWithThrowOnError_ResponseHasError_ReturnsNull() { var settings = new WeatherClientSettings("apiKey"); settings.Retries = 0; settings.ThrowOnError = false; SetUpWithSettings(settings, () => { var response = new HttpResponseMessage(System.Net.HttpStatusCode.NotFound); return(Task.FromResult(response)); }); CurrentWeather weather = new CurrentWeather(); Assert.DoesNotThrowAsync(async() => weather = await client.GetByName("Barcelona")); Assert.IsNull(weather); }
public void CreateWithCustomSettings() { var settings = new WeatherClientSettings(apiKey) { Version = "4", BaseURL = "api.test.org/data", ThrowOnError = true, Retries = 5, TimeBetweenRetries = TimeSpan.FromSeconds(2), ExponentialBackoffInRetries = true }; var weatherClient = new WeatherClient(settings); Assert.AreEqual(apiKey, weatherClient.Settings.AppId); Assert.AreEqual("4", weatherClient.Settings.Version); Assert.AreEqual("api.test.org/data", weatherClient.Settings.BaseURL); Assert.IsTrue(weatherClient.Settings.ThrowOnError); Assert.AreEqual(5, weatherClient.Settings.Retries); Assert.AreEqual(TimeSpan.FromSeconds(2), weatherClient.Settings.TimeBetweenRetries); Assert.IsTrue(weatherClient.Settings.ExponentialBackoffInRetries); }
public async Task ConfiguredWithRetries_NotFoundError_NotRetrying() { var settings = new WeatherClientSettings("apiKey"); settings.Retries = 6; settings.ThrowOnError = false; settings.TimeBetweenRetries = TimeSpan.FromSeconds(0); int timesTried = 0; SetUpWithSettings(settings, () => { timesTried++; var response = new HttpResponseMessage(System.Net.HttpStatusCode.NotFound); return(Task.FromResult(response)); }); CurrentWeather weather = await client.GetByName("Barcelona"); Assert.IsNull(weather); //one of the actual request Assert.AreEqual(1, timesTried); }
protected ClientBase(HttpClient client, WeatherClientSettings settings) { this.client = client; this.settings = settings; }
public void SetUpWithSettings(WeatherClientSettings settings, Func <Task <HttpResponseMessage> > mockResponseMessage) { var httpclient = MockHttpClient.GetMockClient(mockResponseMessage); client = new CurrentWeatherClient(httpclient, settings); }