Skip to content

Commit

Permalink
Merge pull request #2308 from stimms/bug/async_await
Browse files Browse the repository at this point in the history
Fixing remaining async stuff I could find. Fixes #2250
  • Loading branch information
MisterJames authored Mar 6, 2018
2 parents 5c4537c + f963374 commit d72d19e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AllReady.Hangfire.Jobs;
using AllReady.Hangfire.Jobs;
using AllReady.Services;
using Microsoft.Extensions.Options;
using Moq;
Expand All @@ -7,6 +7,7 @@
using System.Net.Http;
using AllReady.Configuration;
using Xunit;
using System.Threading.Tasks;

namespace AllReady.UnitTest.Hangfire.Jobs
{
Expand All @@ -25,13 +26,13 @@ public SendRequestStatusToGetASmokeAlarmShould()
}

[Fact]
public void SendsValidHttpRequestMessage()
public async Task SendsValidHttpRequestMessage()
{
var mockedHttpClient = new Mock<IHttpClient>();
var sendRequestStatusToGetASmokeAlarm = new SendRequestStatusToGetASmokeAlarm(_getASmokeAlarmApiSettings, mockedHttpClient.Object);
try
{
sendRequestStatusToGetASmokeAlarm.Send("request1", "new", false);
await sendRequestStatusToGetASmokeAlarm.Send("request1", "new", false);
}
catch { }

Expand All @@ -45,15 +46,15 @@ public void SendsValidHttpRequestMessage()
}

[Fact]
public void ThrowsException_WhenOnNonSuccessHttpStatus()
public async Task ThrowsException_WhenOnNonSuccessHttpStatus()
{
var mockedHttpClient = new Mock<IHttpClient>();
mockedHttpClient.Setup(x => x.SendAsync(It.IsAny<HttpRequestMessage>()))
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.BadRequest));
var sendRequestStatusToGetASmokeAlarm = new SendRequestStatusToGetASmokeAlarm(_getASmokeAlarmApiSettings, mockedHttpClient.Object);

Assert.Throws<HttpRequestException>(
() => sendRequestStatusToGetASmokeAlarm.Send(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>())
await Assert.ThrowsAsync<HttpRequestException>(
async () => await sendRequestStatusToGetASmokeAlarm.Send(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>())
);
}
}
Expand Down
16 changes: 8 additions & 8 deletions AllReadyApp/Web-App/AllReady/DataAccess/SampleDataGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public SampleDataGenerator(AllReadyContext context, IOptions<SampleDataSettings>
_userManager = userManager;
}

public void InsertTestData()
public async Task InsertTestData()
{
// Avoid polluting the database if there's already something in there.
if (_context.Locations.Any() ||
Expand Down Expand Up @@ -440,7 +440,7 @@ public void InsertTestData()
TimeZoneId = _timeZone.Id,
PhoneNumber = "111-111-1111"
};
_userManager.CreateAsync(user1, _settings.DefaultAdminPassword).GetAwaiter().GetResult();
await _userManager.CreateAsync(user1, _settings.DefaultAdminPassword);
users.Add(user1);

var user2 = new ApplicationUser
Expand All @@ -453,7 +453,7 @@ public void InsertTestData()
TimeZoneId = _timeZone.Id,
PhoneNumber = "222-222-2222"
};
_userManager.CreateAsync(user2, _settings.DefaultAdminPassword).GetAwaiter().GetResult();
await _userManager.CreateAsync(user2, _settings.DefaultAdminPassword);
users.Add(user2);

var user3 = new ApplicationUser
Expand All @@ -466,7 +466,7 @@ public void InsertTestData()
TimeZoneId = _timeZone.Id,
PhoneNumber = "333-333-3333"
};
_userManager.CreateAsync(user3, _settings.DefaultAdminPassword).GetAwaiter().GetResult();
await _userManager.CreateAsync(user3, _settings.DefaultAdminPassword);
users.Add(user3);
#endregion

Expand All @@ -482,7 +482,7 @@ public void InsertTestData()
TimeZoneId = _timeZone.Id,
PhoneNumber = "333-333-3333"
};
_userManager.CreateAsync(eventManagerUser, _settings.DefaultAdminPassword).GetAwaiter().GetResult();
await _userManager.CreateAsync(eventManagerUser, _settings.DefaultAdminPassword);
users.Add(eventManagerUser);

var eventManager = new EventManager
Expand All @@ -508,7 +508,7 @@ public void InsertTestData()
TimeZoneId = _timeZone.Id,
PhoneNumber = "333-333-3333"
};
_userManager.CreateAsync(campaignManagerUser, _settings.DefaultAdminPassword).GetAwaiter().GetResult();
await _userManager.CreateAsync(campaignManagerUser, _settings.DefaultAdminPassword);
users.Add(campaignManagerUser);

var campaignManager = new CampaignManager
Expand Down Expand Up @@ -657,8 +657,8 @@ public async Task CreateAdminUser()
EmailConfirmed = true,
PhoneNumber = "444-444-4444"
};
_userManager.CreateAsync(user, _settings.DefaultAdminPassword).GetAwaiter().GetResult();
_userManager.AddClaimAsync(user, new Claim(Security.ClaimTypes.UserType, nameof(UserType.SiteAdmin))).GetAwaiter().GetResult();
await _userManager.CreateAsync(user, _settings.DefaultAdminPassword);
await _userManager.AddClaimAsync(user, new Claim(Security.ClaimTypes.UserType, nameof(UserType.SiteAdmin)));

var user2 = new ApplicationUser
{
Expand Down
11 changes: 6 additions & 5 deletions AllReadyApp/Web-App/AllReady/Hangfire/Jobs/ProcessApiRequests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System;
using System.Linq;
using System.Threading.Tasks;
using AllReady.Configuration;
using AllReady.Models;
using AllReady.Services.Mapping.GeoCoding;
Expand Down Expand Up @@ -27,7 +28,7 @@ public ProcessApiRequests(AllReadyContext context, IGeocodeService geocoder, IOp
this.approvedRegions = approvedRegions.Value;
}

public void Process(RequestApiViewModel viewModel)
public async Task Process(RequestApiViewModel viewModel)
{
//since this is Hangfire job code, it needs to be idempotent, this could be re-tried if there is a failure
if (context.Requests.Any(x => x.ProviderRequestId == viewModel.ProviderRequestId))
Expand Down Expand Up @@ -57,7 +58,7 @@ public void Process(RequestApiViewModel viewModel)
};

//this is a web service call
var coordinates = geocoder.GetCoordinatesFromAddress(request.Address, request.City, request.State, request.PostalCode, string.Empty).Result;
var coordinates = await geocoder.GetCoordinatesFromAddress(request.Address, request.City, request.State, request.PostalCode, string.Empty);

request.Latitude = coordinates?.Latitude ?? 0;
request.Longitude = coordinates?.Longitude ?? 0;
Expand All @@ -78,6 +79,6 @@ private bool RequestIsFromApprovedRegion(string providerData)

public interface IProcessApiRequests
{
void Process(RequestApiViewModel viewModel);
Task Process(RequestApiViewModel viewModel);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Net.Http;
using System.Net.Http;
using System.Net.Http.Headers;
using AllReady.Configuration;
using Microsoft.Extensions.Options;
using AllReady.Services;
using Newtonsoft.Json;
using System.Text;
using System.Threading.Tasks;

namespace AllReady.Hangfire.Jobs
{
Expand All @@ -19,14 +20,14 @@ public SendRequestStatusToGetASmokeAlarm(IOptions<GetASmokeAlarmApiSettings> get
_httpClient = httpClient;
}

public void Send(string serial, string status, bool acceptance)
public async Task Send(string serial, string status, bool acceptance)
{
var request = new HttpRequestMessage(HttpMethod.Post, $"{_getASmokeAlarmApiSettings.BaseAddress}admin/requests/status/{serial}")
{
Content = new StringContent(JsonConvert.SerializeObject(new { acceptance, status }), Encoding.UTF8, "application/json")
};
request.Headers.Authorization = AuthenticationHeaderValue.Parse(_getASmokeAlarmApiSettings.Token);
var response = _httpClient.SendAsync(request).Result;
var response = await _httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
}
}
Expand All @@ -42,6 +43,6 @@ public static class GasaStatus

public interface ISendRequestStatusToGetASmokeAlarm
{
void Send(string serial, string status, bool acceptance);
Task Send(string serial, string status, bool acceptance);
}
}
}
2 changes: 1 addition & 1 deletion AllReadyApp/Web-App/AllReady/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, AllReady
// for production applications, this should either be set to false or deleted.
if (purgeRefreshSampleData || Configuration["SampleData:InsertSampleData"] == "true")
{
sampleData.InsertTestData();
sampleData.InsertTestData().GetAwaiter().GetResult();
}

if (Configuration["SampleData:InsertTestUsers"] == "true")
Expand Down

0 comments on commit d72d19e

Please sign in to comment.