Skip to content

Commit 31bfa83

Browse files
committed
await all the things
- aspnet#11 (1 of 4 or so) - remove almost all mention of `.Result`, `.Wait()`, ... in test code - rename (mostly newly-) `Task`-returning helpers `...Async` - remove newly-unused `Microsoft.TestCommon.TaskExtensions` - remove status checks of `Task`s that have been `await`ed - remove some (not all) `Assert.NotNull(task)` checks - start Task`s that `RazorParser.CreateParseTask()` returns (see aspnet#22) - fix `async void` tests Add Moq `Setup()`s for new NREs - appears the `Task.WaitUntilCompleted()` extension method didn't execute all inner tasks - more importantly, `null` inner tasks were ignored nit: `Assert.Equal(null, xyz)` -> `Assert.Null(xyz)`
1 parent 8b9b851 commit 31bfa83

File tree

161 files changed

+2950
-3597
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

161 files changed

+2950
-3597
lines changed

test/Common/TaskHelpersExtensionsTest.cs

Lines changed: 42 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using Microsoft.TestCommon;
5-
using Moq;
65

76
namespace System.Threading.Tasks
87
{
@@ -12,123 +11,99 @@ public class TaskHelpersExtensionsTest
1211
// Task<object> Task<T>.CastToObject()
1312

1413
[Fact, ForceGC]
15-
public Task ConvertFromTaskOfStringShouldSucceed()
14+
public async Task ConvertFromTaskOfStringShouldSucceed()
1615
{
1716
// Arrange
18-
return Task.FromResult("StringResult")
17+
var task = Task.FromResult("StringResult")
18+
.CastToObject();
1919

2020
// Act
21-
.CastToObject()
21+
var result = await task;
2222

2323
// Assert
24-
.ContinueWith((task) =>
25-
{
26-
Assert.Equal(TaskStatus.RanToCompletion, task.Status);
27-
Assert.Equal("StringResult", (string)task.Result);
28-
});
24+
Assert.Equal(TaskStatus.RanToCompletion, task.Status);
25+
Assert.Equal("StringResult", (string)result);
2926
}
3027

3128
[Fact, ForceGC]
32-
public Task ConvertFromTaskOfIntShouldSucceed()
29+
public async Task ConvertFromTaskOfIntShouldSucceed()
3330
{
3431
// Arrange
35-
return Task.FromResult(123)
32+
var task = Task.FromResult(123)
33+
.CastToObject();
3634

3735
// Act
38-
.CastToObject()
36+
var result = await task;
3937

4038
// Assert
41-
.ContinueWith((task) =>
42-
{
43-
Assert.Equal(TaskStatus.RanToCompletion, task.Status);
44-
Assert.Equal(123, (int)task.Result);
45-
});
39+
Assert.Equal(TaskStatus.RanToCompletion, task.Status);
40+
Assert.Equal(123, (int)result);
4641
}
4742

4843
[Fact, ForceGC]
49-
public Task ConvertFromFaultedTaskOfObjectShouldBeHandled()
44+
public async Task ConvertFromFaultedTaskOfObjectShouldBeHandled()
5045
{
5146
// Arrange
52-
return TaskHelpers.FromError<object>(new InvalidOperationException())
47+
var task = TaskHelpers.FromError<object>(new InvalidOperationException())
48+
.CastToObject();
5349

54-
// Act
55-
.CastToObject()
56-
57-
// Assert
58-
.ContinueWith((task) =>
59-
{
60-
Assert.Equal(TaskStatus.Faulted, task.Status);
61-
Assert.IsType<InvalidOperationException>(task.Exception.GetBaseException());
62-
});
50+
// Act & Assert
51+
await Assert.ThrowsAsync<InvalidOperationException>(() => task);
52+
Assert.Equal(TaskStatus.Faulted, task.Status);
6353
}
6454

6555
[Fact, ForceGC]
66-
public Task ConvertFromCancelledTaskOfStringShouldBeHandled()
56+
public async Task ConvertFromCancelledTaskOfStringShouldBeHandled()
6757
{
6858
// Arrange
69-
return TaskHelpers.Canceled<string>()
59+
var task = TaskHelpers.Canceled<string>()
60+
.CastToObject();
7061

71-
// Act
72-
.CastToObject()
73-
74-
// Assert
75-
.ContinueWith((task) =>
76-
{
77-
Assert.Equal(TaskStatus.Canceled, task.Status);
78-
});
62+
// Act & Assert
63+
await Assert.ThrowsAsync<TaskCanceledException>(() => task);
64+
Assert.Equal(TaskStatus.Canceled, task.Status);
7965
}
8066

8167
// ----------------------------------------------------------------
8268
// Task<object> Task.CastToObject()
8369

8470
[Fact, ForceGC]
85-
public Task ConvertFromTaskShouldSucceed()
71+
public async Task ConvertFromTaskShouldSucceed()
8672
{
8773
// Arrange
88-
return TaskHelpers.Completed()
74+
var task = TaskHelpers.Completed()
75+
.CastToObject();
8976

9077
// Act
91-
.CastToObject()
78+
var result = await task;
9279

9380
// Assert
94-
.ContinueWith((task) =>
95-
{
96-
Assert.Equal(TaskStatus.RanToCompletion, task.Status);
97-
Assert.Equal(null, task.Result);
98-
});
81+
Assert.Equal(TaskStatus.RanToCompletion, task.Status);
82+
Assert.Null(result);
9983
}
10084

10185
[Fact, ForceGC]
102-
public Task ConvertFromFaultedTaskShouldBeHandled()
86+
public async Task ConvertFromFaultedTaskShouldBeHandled()
10387
{
10488
// Arrange
105-
return TaskHelpers.FromError(new InvalidOperationException())
89+
var task = TaskHelpers.FromError(new InvalidOperationException())
90+
.CastToObject();
10691

107-
// Act
108-
.CastToObject()
109-
110-
// Assert
111-
.ContinueWith((task) =>
112-
{
113-
Assert.Equal(TaskStatus.Faulted, task.Status);
114-
Assert.IsType<InvalidOperationException>(task.Exception.GetBaseException());
115-
});
92+
// Act & Assert
93+
await Assert.ThrowsAsync<InvalidOperationException>(() => task);
94+
Assert.Equal(TaskStatus.Faulted, task.Status);
11695
}
11796

11897
[Fact, ForceGC]
119-
public Task ConvertFromCancelledTaskShouldBeHandled()
98+
public async Task ConvertFromCancelledTaskShouldBeHandled()
12099
{
121100
// Arrange
122-
return TaskHelpers.Canceled()
101+
var task = TaskHelpers.Canceled()
102+
.CastToObject();
123103

124-
// Act
125-
.CastToObject()
126-
127-
// Assert
128-
.ContinueWith((task) =>
129-
{
130-
Assert.Equal(TaskStatus.Canceled, task.Status);
131-
});
104+
// Act & Assert
105+
await Assert.ThrowsAsync<TaskCanceledException>(() => task);
106+
Assert.Equal(TaskStatus.Canceled, task.Status);
132107
}
133108

134109
// -----------------------------------------------------------------

test/Microsoft.AspNet.Facebook.Test/FacebookAuthorizeFilterHookTest.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections;
65
using System.Collections.Generic;
76
using System.Collections.Specialized;
87
using System.Web;
@@ -34,7 +33,7 @@ public void OnAuthorization_CannotCreateCookiesHookRedirectsToConfigValueOrDefau
3433
// Assert
3534
Assert.Equal(result.RedirectUrl.AbsoluteUri, new Uri(expectedRedirectPath).AbsoluteUri);
3635
}
37-
36+
3837
[Fact]
3938
public void OnAuthorization_OnlyTriggersCannotCreateCookiesHook()
4039
{
Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System.Threading.Tasks;
45
using Microsoft.AspNet.Facebook.Client;
56
using Microsoft.AspNet.Facebook.Test.Helpers;
67
using Microsoft.AspNet.Facebook.Test.Types;
@@ -11,112 +12,104 @@ namespace Microsoft.AspNet.Facebook.Test
1112
public class FacebookClientExtensionsTest
1213
{
1314
[Fact]
14-
public void GetCurrentUserAsyncOfT_CallsGetTaskAsyncWithTheExpectedPath()
15+
public async Task GetCurrentUserAsyncOfT_CallsGetTaskAsyncWithTheExpectedPath()
1516
{
1617
LocalFacebookClient client = new LocalFacebookClient();
17-
client.GetCurrentUserAsync<SimpleUser>().Wait();
18+
await client.GetCurrentUserAsync<SimpleUser>();
1819

1920
Assert.Equal("me?fields=id,name,picture.fields(url)", client.Path);
2021
}
2122

2223
[Fact]
23-
public void GetCurrentUserAsync_CallsGetTaskAsyncWithTheExpectedPath()
24+
public async Task GetCurrentUserAsync_CallsGetTaskAsyncWithTheExpectedPath()
2425
{
2526
LocalFacebookClient client = new LocalFacebookClient();
26-
client.GetCurrentUserAsync().Wait();
27+
await client.GetCurrentUserAsync();
2728

2829
Assert.Equal("me", client.Path);
2930
}
3031

3132
[Fact]
32-
public void GetCurrentUserFriendsAsyncOfT_CallsGetTaskAsyncWithTheExpectedPath()
33+
public async Task GetCurrentUserFriendsAsyncOfT_CallsGetTaskAsyncWithTheExpectedPath()
3334
{
3435
LocalFacebookClient client = new LocalFacebookClient();
35-
client.GetCurrentUserFriendsAsync<SimpleUser>().Wait();
36+
await client.GetCurrentUserFriendsAsync<SimpleUser>();
3637

3738
Assert.Equal("me/friends?fields=id,name,picture.fields(url)", client.Path);
3839
}
3940

4041
[Fact]
41-
public void GetCurrentUserFriendsAsync_CallsGetTaskAsyncWithTheExpectedPath()
42+
public async Task GetCurrentUserFriendsAsync_CallsGetTaskAsyncWithTheExpectedPath()
4243
{
4344
LocalFacebookClient client = new LocalFacebookClient();
44-
client.GetCurrentUserFriendsAsync().Wait();
45+
await client.GetCurrentUserFriendsAsync();
4546

4647
Assert.Equal("me/friends", client.Path);
4748
}
4849

4950
[Fact]
50-
public void GetCurrentUserPermissionsAsync_CallsGetTaskAsyncWithTheExpectedPath()
51+
public async Task GetCurrentUserPermissionsAsync_CallsGetTaskAsyncWithTheExpectedPath()
5152
{
5253
LocalFacebookClient client = new LocalFacebookClient();
53-
client.GetCurrentUserPermissionsAsync().Wait();
54+
await client.GetCurrentUserPermissionsAsync();
5455

5556
Assert.Equal("me/permissions", client.Path);
5657
}
5758

5859
[Fact]
59-
public void GetCurrentUserPhotosAsyncOfT_CallsGetTaskAsyncWithTheExpectedPath()
60+
public async Task GetCurrentUserPhotosAsyncOfT_CallsGetTaskAsyncWithTheExpectedPath()
6061
{
6162
LocalFacebookClient client = new LocalFacebookClient();
62-
client.GetCurrentUserPhotosAsync<UserPhoto>().Wait();
63+
await client.GetCurrentUserPhotosAsync<UserPhoto>();
6364

6465
Assert.Equal("me/photos?fields=name,picture,source", client.Path);
6566
}
6667

6768
[Fact]
68-
public void GetCurrentUserStatusesAsyncOfT_CallsGetTaskAsyncWithTheExpectedPath()
69+
public async Task GetCurrentUserStatusesAsyncOfT_CallsGetTaskAsyncWithTheExpectedPath()
6970
{
7071
LocalFacebookClient client = new LocalFacebookClient();
71-
client.GetCurrentUserStatusesAsync<UserStatus>().Wait();
72+
await client.GetCurrentUserStatusesAsync<UserStatus>();
7273

7374
Assert.Equal("me/statuses?fields=message,time", client.Path);
7475
}
7576

7677
[Fact]
77-
public void GetFacebookObjectAsyncOfT_CallsGetTaskAsyncWithTheExpectedPath()
78+
public async Task GetFacebookObjectAsyncOfT_CallsGetTaskAsyncWithTheExpectedPath()
7879
{
7980
LocalFacebookClient client = new LocalFacebookClient();
80-
client.GetFacebookObjectAsync<FacebookConnection<FacebookPicture>>("me/picture").Wait();
81+
await client.GetFacebookObjectAsync<FacebookConnection<FacebookPicture>>("me/picture");
8182

8283
Assert.Equal("me/picture?fields=url", client.Path);
8384
}
8485

8586
[Fact]
86-
public void GetFacebookObjectAsync_CallsGetTaskAsyncWithTheExpectedPath()
87+
public async Task GetFacebookObjectAsync_CallsGetTaskAsyncWithTheExpectedPath()
8788
{
8889
LocalFacebookClient client = new LocalFacebookClient();
89-
client.GetFacebookObjectAsync("me/notes").Wait();
90+
await client.GetFacebookObjectAsync("me/notes");
9091

9192
Assert.Equal("me/notes", client.Path);
9293
}
9394

9495
[Fact]
95-
public void GetFacebookObjectAsyncOfT_ThrowArgumentNullExceptions()
96+
public async Task GetFacebookObjectAsyncOfT_ThrowArgumentNullExceptions()
9697
{
9798
LocalFacebookClient client = null;
98-
Assert.ThrowsArgumentNull(
99-
() => client.GetFacebookObjectAsync<SimpleUser>("me").Wait(),
100-
"client");
99+
await Assert.ThrowsArgumentNullAsync(() => client.GetFacebookObjectAsync<SimpleUser>("me"), "client");
101100

102101
client = new LocalFacebookClient();
103-
Assert.ThrowsArgumentNull(
104-
() => client.GetFacebookObjectAsync<SimpleUser>(null).Wait(),
105-
"objectPath");
102+
await Assert.ThrowsArgumentNullAsync(() => client.GetFacebookObjectAsync<SimpleUser>(null), "objectPath");
106103
}
107104

108105
[Fact]
109-
public void GetFacebookObjectAsync_ThrowArgumentNullExceptions()
106+
public async Task GetFacebookObjectAsync_ThrowArgumentNullExceptions()
110107
{
111108
LocalFacebookClient client = null;
112-
Assert.ThrowsArgumentNull(
113-
() => client.GetFacebookObjectAsync("me").Wait(),
114-
"client");
109+
await Assert.ThrowsArgumentNullAsync(() => client.GetFacebookObjectAsync("me"), "client");
115110

116111
client = new LocalFacebookClient();
117-
Assert.ThrowsArgumentNull(
118-
() => client.GetFacebookObjectAsync(null).Wait(),
119-
"objectPath");
112+
await Assert.ThrowsArgumentNullAsync(() => client.GetFacebookObjectAsync(null), "objectPath");
120113
}
121114
}
122115
}

test/Microsoft.AspNet.Facebook.Test/FacebookRealtimeControllerTest.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class FacebookRealtimeControllerTest
2424
public void Overriding_VerificationToken()
2525
{
2626
var userRealTimeController = new UserRealtimeCallbackController();
27-
Assert.Equal(null, userRealTimeController.VerifyToken);
27+
Assert.Null(userRealTimeController.VerifyToken);
2828

2929
userRealTimeController = new UserRealtimeCallbackController(null, "foo");
3030
Assert.Equal("foo", userRealTimeController.VerifyToken);
@@ -36,7 +36,7 @@ public void Overriding_VerificationToken()
3636
[Theory]
3737
[InlineData("123456", "foo")]
3838
[InlineData("654321", "bar")]
39-
public void Get_ReturnsOk_WithValidParameters(string challenge, string verifyToken)
39+
public async Task Get_ReturnsOk_WithValidParameters(string challenge, string verifyToken)
4040
{
4141
var userRealTimeController = new UserRealtimeCallbackController(null, verifyToken);
4242
userRealTimeController.Request = new HttpRequestMessage();
@@ -46,7 +46,7 @@ public void Get_ReturnsOk_WithValidParameters(string challenge, string verifyTok
4646
Mode = "subscribe",
4747
Verify_Token = verifyToken
4848
};
49-
Assert.Equal(challenge, userRealTimeController.Get(subscriptionVerification).Content.ReadAsStringAsync().Result);
49+
Assert.Equal(challenge, await userRealTimeController.Get(subscriptionVerification).Content.ReadAsStringAsync());
5050
}
5151

5252
[Theory]
@@ -71,7 +71,7 @@ public void Get_ReturnsExpectedStatusCode(string challenge, string verifyToken,
7171
[Theory]
7272
[InlineData(ContentString, AppSignatureHeader1, AppSecret1)]
7373
[InlineData(ContentString, AppSignatureHeader2, AppSecret2)]
74-
public void Post_ReturnsOk_WithValidParameters(string contentString, string headerValue, string appSecret)
74+
public async Task Post_ReturnsOk_WithValidParameters(string contentString, string headerValue, string appSecret)
7575
{
7676
var userRealTimeController = new UserRealtimeCallbackController(appSecret, null);
7777
userRealTimeController.Request = new HttpRequestMessage
@@ -80,15 +80,15 @@ public void Post_ReturnsOk_WithValidParameters(string contentString, string head
8080
};
8181
var request = userRealTimeController.Request;
8282
request.Headers.Add("X-Hub-Signature", headerValue);
83-
Assert.Equal(HttpStatusCode.OK, userRealTimeController.Post().Result.StatusCode);
83+
Assert.Equal(HttpStatusCode.OK, (await userRealTimeController.Post()).StatusCode);
8484
}
8585

8686
[Theory]
8787
[InlineData(ContentString, AppSignatureHeader1, AppSecret2)]
8888
[InlineData(ContentString, AppSignatureHeader2, AppSecret1)]
8989
[InlineData(ContentString, null, AppSecret2)]
9090
[InlineData(ContentString, AppSignatureHeader1, null)]
91-
public void Post_ReturnsBadRequest_WithInValidParameters(string contentString, string headerValue, string AppSecret)
91+
public async Task Post_ReturnsBadRequest_WithInValidParameters(string contentString, string headerValue, string AppSecret)
9292
{
9393
var userRealTimeController = new UserRealtimeCallbackController(AppSecret, null);
9494
userRealTimeController.Request = new HttpRequestMessage
@@ -100,7 +100,7 @@ public void Post_ReturnsBadRequest_WithInValidParameters(string contentString, s
100100
{
101101
request.Headers.Add("X-Hub-Signature", headerValue);
102102
}
103-
Assert.Equal(HttpStatusCode.BadRequest, userRealTimeController.Post().Result.StatusCode);
103+
Assert.Equal(HttpStatusCode.BadRequest, (await userRealTimeController.Post()).StatusCode);
104104
}
105105

106106
private sealed class UserRealtimeCallbackController : FacebookRealtimeUpdateController

0 commit comments

Comments
 (0)