From b56613b99b3dd36e4f5282bfd46308ef3da7a61e Mon Sep 17 00:00:00 2001 From: Haacked Date: Fri, 21 Mar 2014 11:14:22 -0700 Subject: [PATCH] Await calls with try/catch In 451eddc647045bc6309bc0c31ca653a042d19fee we were a little overzealous in removing async/await calls. Anywhere we do a try/catch around a method that returns a Task, we need to use await. --- .../Clients/AuthorizationsClientTests.cs | 14 ++++++-------- .../Helpers/NSubstituteExtensions.cs | 19 +++++++++++++++++++ Octokit.Tests/OctoKit.Tests-NetCore45.csproj | 1 + Octokit.Tests/Octokit.Tests-Portable.csproj | 1 + Octokit.Tests/Octokit.Tests.csproj | 1 + Octokit/Clients/AuthorizationsClient.cs | 4 ++-- 6 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 Octokit.Tests/Helpers/NSubstituteExtensions.cs diff --git a/Octokit.Tests/Clients/AuthorizationsClientTests.cs b/Octokit.Tests/Clients/AuthorizationsClientTests.cs index 955dbfdb..6e190a81 100644 --- a/Octokit.Tests/Clients/AuthorizationsClientTests.cs +++ b/Octokit.Tests/Clients/AuthorizationsClientTests.cs @@ -127,20 +127,18 @@ namespace Octokit.Tests.Clients } [Fact] - public void WrapsTwoFactorFailureWithTwoFactorException() + public async Task WrapsTwoFactorFailureWithTwoFactorException() { var data = new NewAuthorization(); var client = Substitute.For(); client.Put(Args.Uri, Args.Object, Args.String) - .Returns(_ => - { - throw new AuthorizationException( - new ApiResponse { StatusCode = HttpStatusCode.Unauthorized}); - }); + .ThrowsAsync( + new AuthorizationException( + new ApiResponse { StatusCode = HttpStatusCode.Unauthorized })); var authEndpoint = new AuthorizationsClient(client); - Assert.Throws(() => - authEndpoint.GetOrCreateApplicationAuthentication("clientId", "secret", data, "authenticationCode")); + await AssertEx.Throws(async () => + await authEndpoint.GetOrCreateApplicationAuthentication("clientId", "secret", data, "authenticationCode")); } [Fact] diff --git a/Octokit.Tests/Helpers/NSubstituteExtensions.cs b/Octokit.Tests/Helpers/NSubstituteExtensions.cs new file mode 100644 index 00000000..97c1e053 --- /dev/null +++ b/Octokit.Tests/Helpers/NSubstituteExtensions.cs @@ -0,0 +1,19 @@ +using System; +using System.Threading.Tasks; +using NSubstitute; +using NSubstitute.Core; + +public static class NSubstituteExtensions +{ + public static ConfiguredCall ReturnsAsync( + this TTask value, Func returnThis, + params Func[] returnThese) where TTask : Task + { + return value.Returns(callInfo => Task.Factory.StartNew(() => returnThis(callInfo))); + } + + public static ConfiguredCall ThrowsAsync(this Task value, Exception exception) + { + return value.ReturnsAsync, T>(callInfo => { throw exception; }); + } +} diff --git a/Octokit.Tests/OctoKit.Tests-NetCore45.csproj b/Octokit.Tests/OctoKit.Tests-NetCore45.csproj index 1b4ba7b3..bfd3c35d 100644 --- a/Octokit.Tests/OctoKit.Tests-NetCore45.csproj +++ b/Octokit.Tests/OctoKit.Tests-NetCore45.csproj @@ -79,6 +79,7 @@ + diff --git a/Octokit.Tests/Octokit.Tests-Portable.csproj b/Octokit.Tests/Octokit.Tests-Portable.csproj index 281a0d1e..d5eb3473 100644 --- a/Octokit.Tests/Octokit.Tests-Portable.csproj +++ b/Octokit.Tests/Octokit.Tests-Portable.csproj @@ -79,6 +79,7 @@ + diff --git a/Octokit.Tests/Octokit.Tests.csproj b/Octokit.Tests/Octokit.Tests.csproj index 4a9dd735..64da5f2f 100644 --- a/Octokit.Tests/Octokit.Tests.csproj +++ b/Octokit.Tests/Octokit.Tests.csproj @@ -102,6 +102,7 @@ + diff --git a/Octokit/Clients/AuthorizationsClient.cs b/Octokit/Clients/AuthorizationsClient.cs index 29cb8957..df4a3014 100644 --- a/Octokit/Clients/AuthorizationsClient.cs +++ b/Octokit/Clients/AuthorizationsClient.cs @@ -117,7 +117,7 @@ namespace Octokit /// /// Thrown when a general API error occurs. /// The created . - public Task GetOrCreateApplicationAuthentication( + public async Task GetOrCreateApplicationAuthentication( string clientId, string clientSecret, NewAuthorization newAuthorization, @@ -139,7 +139,7 @@ namespace Octokit try { - return ApiConnection.Put( + return await ApiConnection.Put( endpoint, requestData, twoFactorAuthenticationCode);