Skip to content
This repository was archived by the owner on Mar 20, 2019. It is now read-only.

Commit fd85211

Browse files
committed
Unit test compile error fixes.
1 parent 2c9f305 commit fd85211

8 files changed

Lines changed: 93 additions & 116 deletions

src/DotNetOpenAuth.Test/CoordinatorBase.cs

Lines changed: 19 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@
66

77
namespace DotNetOpenAuth.Test {
88
using System;
9+
using System.Collections.Generic;
910
using System.Threading;
11+
using System.Threading.Tasks;
1012
using DotNetOpenAuth.Messaging;
1113
using DotNetOpenAuth.OpenId.RelyingParty;
1214
using DotNetOpenAuth.Test.Mocks;
1315
using NUnit.Framework;
1416
using Validation;
1517

1618
internal abstract class CoordinatorBase<T1, T2> {
17-
private Action<T1> party1Action;
18-
private Action<T2> party2Action;
19+
private Func<T1, CancellationToken, Task> party1Action;
20+
private Func<T2, CancellationToken, Task> party2Action;
1921

20-
protected CoordinatorBase(Action<T1> party1Action, Action<T2> party2Action) {
22+
protected CoordinatorBase(Func<T1, CancellationToken, Task> party1Action, Func<T2, CancellationToken, Task> party2Action) {
2123
Requires.NotNull(party1Action, "party1Action");
2224
Requires.NotNull(party2Action, "party2Action");
2325

@@ -29,62 +31,24 @@ protected CoordinatorBase(Action<T1> party1Action, Action<T2> party2Action) {
2931

3032
protected internal Action<IProtocolMessage> OutgoingMessageFilter { get; set; }
3133

32-
internal abstract void Run();
34+
internal abstract Task RunAsync();
3335

34-
protected void RunCore(T1 party1Object, T2 party2Object) {
35-
Thread party1Thread = null, party2Thread = null;
36-
Exception failingException = null;
36+
protected async Task RunCoreAsync(T1 party1Object, T2 party2Object) {
37+
var cts = new CancellationTokenSource();
3738

38-
// Each thread we create needs a surrounding exception catcher so that we can
39-
// terminate the other thread and inform the test host that the test failed.
40-
Action<Action> safeWrapper = (action) => {
41-
try {
42-
TestBase.SetMockHttpContext();
43-
action();
44-
} catch (Exception ex) {
45-
// We may be the second thread in an ThreadAbortException, so check the "flag"
46-
lock (this) {
47-
if (failingException == null || (failingException is ThreadAbortException && !(ex is ThreadAbortException))) {
48-
failingException = ex;
49-
if (Thread.CurrentThread == party1Thread) {
50-
party2Thread.Abort();
51-
} else {
52-
party1Thread.Abort();
53-
}
54-
}
55-
}
56-
}
57-
};
58-
59-
// Run the threads, and wait for them to complete.
60-
// If this main thread is aborted (test run aborted), go ahead and abort the other two threads.
61-
party1Thread = new Thread(() => { safeWrapper(() => { this.party1Action(party1Object); }); });
62-
party2Thread = new Thread(() => { safeWrapper(() => { this.party2Action(party2Object); }); });
63-
party1Thread.Name = "P1";
64-
party2Thread.Name = "P2";
6539
try {
66-
party1Thread.Start();
67-
party2Thread.Start();
68-
party1Thread.Join();
69-
party2Thread.Join();
70-
} catch (ThreadAbortException) {
71-
party1Thread.Abort();
72-
party2Thread.Abort();
40+
var parties = new List<Task> {
41+
Task.Run(() => this.party1Action(party1Object, cts.Token)),
42+
Task.Run(() => this.party2Action(party2Object, cts.Token)),
43+
};
44+
var completingTask = await Task.WhenAny(parties);
45+
await completingTask; // rethrow any exception from the first completing task.
46+
47+
// if no exception, then block for the second task now.
48+
await Task.WhenAll(parties);
49+
} catch {
50+
cts.Cancel(); // cause the second party to terminate, if necessary.
7351
throw;
74-
} catch (ThreadStartException ex) {
75-
if (ex.InnerException is ThreadAbortException) {
76-
// if party1Thread threw an exception
77-
// (which may even have been intentional for the test)
78-
// before party2Thread even started, then this exception
79-
// can be thrown, and should be ignored.
80-
} else {
81-
throw;
82-
}
83-
}
84-
85-
// Use the failing reason of a failing sub-thread as our reason, if anything failed.
86-
if (failingException != null) {
87-
throw new AssertionException("Coordinator thread threw unhandled exception: " + failingException, failingException);
8852
}
8953
}
9054
}

src/DotNetOpenAuth.Test/Messaging/Bindings/StandardExpirationBindingElementTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace DotNetOpenAuth.Test.Messaging.Bindings {
88
using System;
9-
9+
using System.Threading.Tasks;
1010
using DotNetOpenAuth.Configuration;
1111
using DotNetOpenAuth.Messaging;
1212
using DotNetOpenAuth.Messaging.Bindings;
@@ -16,13 +16,13 @@ namespace DotNetOpenAuth.Test.Messaging.Bindings {
1616
[TestFixture]
1717
public class StandardExpirationBindingElementTests : MessagingTestBase {
1818
[Test]
19-
public void SendSetsTimestamp() {
19+
public async Task SendSetsTimestamp() {
2020
TestExpiringMessage message = new TestExpiringMessage(MessageTransport.Indirect);
2121
message.Recipient = new Uri("http://localtest");
2222
((IExpiringProtocolMessage)message).UtcCreationDate = DateTime.Parse("1/1/1990");
2323

2424
Channel channel = CreateChannel(MessageProtections.Expiration);
25-
channel.PrepareResponse(message);
25+
await channel.PrepareResponseAsync(message);
2626
Assert.IsTrue(DateTime.UtcNow - ((IExpiringProtocolMessage)message).UtcCreationDate < TimeSpan.FromSeconds(3), "The timestamp on the message was not set on send.");
2727
}
2828

src/DotNetOpenAuth.Test/Messaging/Bindings/StandardReplayProtectionBindingElementTests.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace DotNetOpenAuth.Test.Messaging.Bindings {
99
using System.Collections.Generic;
1010
using System.Linq;
1111
using System.Text;
12+
using System.Threading;
13+
using System.Threading.Tasks;
1214
using DotNetOpenAuth.Messaging;
1315
using DotNetOpenAuth.Messaging.Bindings;
1416
using DotNetOpenAuth.OpenId;
@@ -40,14 +42,14 @@ public override void SetUp() {
4042
/// Verifies that the generated nonce includes random characters.
4143
/// </summary>
4244
[Test]
43-
public void RandomCharactersTest() {
44-
Assert.IsNotNull(this.nonceElement.ProcessOutgoingMessage(this.message));
45+
public async Task RandomCharactersTest() {
46+
Assert.IsNotNull(await this.nonceElement.ProcessOutgoingMessageAsync(this.message, CancellationToken.None));
4547
Assert.IsNotNull(this.message.Nonce, "No nonce was set on the message.");
4648
Assert.AreNotEqual(0, this.message.Nonce.Length, "The generated nonce was empty.");
4749
string firstNonce = this.message.Nonce;
4850

4951
// Apply another nonce and verify that they are different than the first ones.
50-
Assert.IsNotNull(this.nonceElement.ProcessOutgoingMessage(this.message));
52+
Assert.IsNotNull(await this.nonceElement.ProcessOutgoingMessageAsync(this.message, CancellationToken.None));
5153
Assert.IsNotNull(this.message.Nonce, "No nonce was set on the message.");
5254
Assert.AreNotEqual(0, this.message.Nonce.Length, "The generated nonce was empty.");
5355
Assert.AreNotEqual(firstNonce, this.message.Nonce, "The two generated nonces are identical.");
@@ -57,41 +59,41 @@ public void RandomCharactersTest() {
5759
/// Verifies that a message is received correctly.
5860
/// </summary>
5961
[Test]
60-
public void ValidMessageReceivedTest() {
62+
public async Task ValidMessageReceivedTest() {
6163
this.message.Nonce = "a";
62-
Assert.IsNotNull(this.nonceElement.ProcessIncomingMessage(this.message));
64+
Assert.IsNotNull(await this.nonceElement.ProcessIncomingMessageAsync(this.message, CancellationToken.None));
6365
}
6466

6567
/// <summary>
6668
/// Verifies that a message that doesn't have a string of random characters is received correctly.
6769
/// </summary>
6870
[Test]
69-
public void ValidMessageNoNonceReceivedTest() {
71+
public async Task ValidMessageNoNonceReceivedTest() {
7072
this.message.Nonce = string.Empty;
7173
this.nonceElement.AllowZeroLengthNonce = true;
72-
Assert.IsNotNull(this.nonceElement.ProcessIncomingMessage(this.message));
74+
Assert.IsNotNull(await this.nonceElement.ProcessIncomingMessageAsync(this.message, CancellationToken.None));
7375
}
7476

7577
/// <summary>
7678
/// Verifies that a message that doesn't have a string of random characters is received correctly.
7779
/// </summary>
7880
[Test, ExpectedException(typeof(ProtocolException))]
79-
public void InvalidMessageNoNonceReceivedTest() {
81+
public async Task InvalidMessageNoNonceReceivedTest() {
8082
this.message.Nonce = string.Empty;
8183
this.nonceElement.AllowZeroLengthNonce = false;
82-
Assert.IsNotNull(this.nonceElement.ProcessIncomingMessage(this.message));
84+
Assert.IsNotNull(await this.nonceElement.ProcessIncomingMessageAsync(this.message, CancellationToken.None));
8385
}
8486

8587
/// <summary>
8688
/// Verifies that a replayed message is rejected.
8789
/// </summary>
8890
[Test, ExpectedException(typeof(ReplayedMessageException))]
89-
public void ReplayDetectionTest() {
91+
public async Task ReplayDetectionTest() {
9092
this.message.Nonce = "a";
91-
Assert.IsNotNull(this.nonceElement.ProcessIncomingMessage(this.message));
93+
Assert.IsNotNull(await this.nonceElement.ProcessIncomingMessageAsync(this.message, CancellationToken.None));
9294

9395
// Now receive the same message again. This should throw because it's a message replay.
94-
this.nonceElement.ProcessIncomingMessage(this.message);
96+
await this.nonceElement.ProcessIncomingMessageAsync(this.message, CancellationToken.None);
9597
}
9698
}
9799
}

src/DotNetOpenAuth.Test/Messaging/ChannelTests.cs

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace DotNetOpenAuth.Test.Messaging {
99
using System.Collections.Generic;
1010
using System.IO;
1111
using System.Net;
12+
using System.Threading;
13+
using System.Threading.Tasks;
1214
using System.Web;
1315
using DotNetOpenAuth.Messaging;
1416
using DotNetOpenAuth.Messaging.Bindings;
@@ -39,44 +41,44 @@ public void ReadFromRequestForm() {
3941
/// will reject messages that come with an unexpected HTTP verb.
4042
/// </summary>
4143
[Test, ExpectedException(typeof(ProtocolException))]
42-
public void ReadFromRequestDisallowedHttpMethod() {
44+
public async Task ReadFromRequestDisallowedHttpMethod() {
4345
var fields = GetStandardTestFields(FieldFill.CompleteBeforeBindings);
4446
fields["GetOnly"] = "true";
45-
this.Channel.ReadFromRequest(CreateHttpRequestInfo("POST", fields));
47+
await this.Channel.ReadFromRequestAsync(CreateHttpRequestInfo("POST", fields), CancellationToken.None);
4648
}
4749

4850
[Test, ExpectedException(typeof(ArgumentNullException))]
49-
public void SendNull() {
50-
this.Channel.PrepareResponse(null);
51+
public async Task SendNull() {
52+
await this.Channel.PrepareResponseAsync(null);
5153
}
5254

5355
[Test, ExpectedException(typeof(ArgumentException))]
54-
public void SendIndirectedUndirectedMessage() {
56+
public async Task SendIndirectedUndirectedMessage() {
5557
IProtocolMessage message = new TestDirectedMessage(MessageTransport.Indirect);
56-
this.Channel.PrepareResponse(message);
58+
await this.Channel.PrepareResponseAsync(message);
5759
}
5860

5961
[Test, ExpectedException(typeof(ArgumentException))]
60-
public void SendDirectedNoRecipientMessage() {
62+
public async Task SendDirectedNoRecipientMessage() {
6163
IProtocolMessage message = new TestDirectedMessage(MessageTransport.Indirect);
62-
this.Channel.PrepareResponse(message);
64+
await this.Channel.PrepareResponseAsync(message);
6365
}
6466

6567
[Test, ExpectedException(typeof(ArgumentException))]
66-
public void SendInvalidMessageTransport() {
68+
public async Task SendInvalidMessageTransport() {
6769
IProtocolMessage message = new TestDirectedMessage((MessageTransport)100);
68-
this.Channel.PrepareResponse(message);
70+
await this.Channel.PrepareResponseAsync(message);
6971
}
7072

7173
[Test]
72-
public void SendIndirectMessage301Get() {
74+
public async Task SendIndirectMessage301Get() {
7375
TestDirectedMessage message = new TestDirectedMessage(MessageTransport.Indirect);
7476
GetStandardTestMessage(FieldFill.CompleteBeforeBindings, message);
7577
message.Recipient = new Uri("http://provider/path");
7678
var expected = GetStandardTestFields(FieldFill.CompleteBeforeBindings);
7779

78-
OutgoingWebResponse response = this.Channel.PrepareResponse(message);
79-
Assert.AreEqual(HttpStatusCode.Redirect, response.Status);
80+
var response = await this.Channel.PrepareResponseAsync(message);
81+
Assert.AreEqual(HttpStatusCode.Redirect, response.StatusCode);
8082
Assert.AreEqual("text/html; charset=utf-8", response.Headers[HttpResponseHeader.ContentType]);
8183
Assert.IsTrue(response.Body != null && response.Body.Length > 0); // a non-empty body helps get passed filters like WebSense
8284
StringAssert.StartsWith("http://provider/path", response.Headers[HttpResponseHeader.Location]);
@@ -110,7 +112,7 @@ public void SendIndirectMessage301GetNullFields() {
110112
}
111113

112114
[Test]
113-
public void SendIndirectMessageFormPost() {
115+
public async Task SendIndirectMessageFormPost() {
114116
// We craft a very large message to force fallback to form POST.
115117
// We'll also stick some HTML reserved characters in the string value
116118
// to test proper character escaping.
@@ -120,8 +122,8 @@ public void SendIndirectMessageFormPost() {
120122
Location = new Uri("http://host/path"),
121123
Recipient = new Uri("http://provider/path"),
122124
};
123-
OutgoingWebResponse response = this.Channel.PrepareResponse(message);
124-
Assert.AreEqual(HttpStatusCode.OK, response.Status, "A form redirect should be an HTTP successful response.");
125+
var response = await this.Channel.PrepareResponseAsync(message);
126+
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "A form redirect should be an HTTP successful response.");
125127
Assert.IsNull(response.Headers[HttpResponseHeader.Location], "There should not be a redirection header in the response.");
126128
string body = response.Body;
127129
StringAssert.Contains("<form ", body);
@@ -162,13 +164,13 @@ public void SendIndirectMessageFormPostNullFields() {
162164
/// we just check that the right method was called.
163165
/// </remarks>
164166
[Test, ExpectedException(typeof(NotImplementedException))]
165-
public void SendDirectMessageResponse() {
167+
public async Task SendDirectMessageResponse() {
166168
IProtocolMessage message = new TestDirectedMessage {
167169
Age = 15,
168170
Name = "Andrew",
169171
Location = new Uri("http://host/path"),
170172
};
171-
this.Channel.PrepareResponse(message);
173+
await this.Channel.PrepareResponseAsync(message);
172174
}
173175

174176
[Test, ExpectedException(typeof(ArgumentNullException))]
@@ -190,12 +192,12 @@ public void ReceiveUnrecognizedMessage() {
190192
}
191193

192194
[Test]
193-
public void ReadFromRequestWithContext() {
195+
public async Task ReadFromRequestWithContext() {
194196
var fields = GetStandardTestFields(FieldFill.AllRequired);
195197
TestMessage expectedMessage = GetStandardTestMessage(FieldFill.AllRequired);
196198
HttpRequest request = new HttpRequest("somefile", "http://someurl", MessagingUtilities.CreateQueryString(fields));
197199
HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter()));
198-
IProtocolMessage message = this.Channel.ReadFromRequest();
200+
IProtocolMessage message = await this.Channel.ReadFromRequestAsync(CancellationToken.None);
199201
Assert.IsNotNull(message);
200202
Assert.IsInstanceOf<TestMessage>(message);
201203
Assert.AreEqual(expectedMessage.Age, ((TestMessage)message).Age);
@@ -215,12 +217,12 @@ public void ReadFromRequestNull() {
215217
}
216218

217219
[Test]
218-
public void SendReplayProtectedMessageSetsNonce() {
220+
public async Task SendReplayProtectedMessageSetsNonce() {
219221
TestReplayProtectedMessage message = new TestReplayProtectedMessage(MessageTransport.Indirect);
220222
message.Recipient = new Uri("http://localtest");
221223

222224
this.Channel = CreateChannel(MessageProtections.ReplayProtection);
223-
this.Channel.PrepareResponse(message);
225+
await this.Channel.PrepareResponseAsync(message);
224226
Assert.IsNotNull(((IReplayProtectedProtocolMessage)message).Nonce);
225227
}
226228

@@ -251,12 +253,12 @@ public void MessageExpirationWithoutTamperResistance() {
251253
}
252254

253255
[Test, ExpectedException(typeof(ProtocolException))]
254-
public void TooManyBindingElementsProvidingSameProtection() {
256+
public async Task TooManyBindingElementsProvidingSameProtection() {
255257
Channel channel = new TestChannel(
256258
new TestMessageFactory(),
257259
new MockSigningBindingElement(),
258260
new MockSigningBindingElement());
259-
channel.ProcessOutgoingMessageTestHook(new TestSignedDirectedMessage());
261+
await channel.ProcessOutgoingMessageTestHookAsync(new TestSignedDirectedMessage());
260262
}
261263

262264
[Test]
@@ -284,10 +286,10 @@ public void BindingElementsOrdering() {
284286
}
285287

286288
[Test, ExpectedException(typeof(UnprotectedMessageException))]
287-
public void InsufficientlyProtectedMessageSent() {
289+
public async Task InsufficientlyProtectedMessageSent() {
288290
var message = new TestSignedDirectedMessage(MessageTransport.Direct);
289291
message.Recipient = new Uri("http://localtest");
290-
this.Channel.PrepareResponse(message);
292+
await this.Channel.PrepareResponseAsync(message);
291293
}
292294

293295
[Test, ExpectedException(typeof(UnprotectedMessageException))]
@@ -297,9 +299,9 @@ public void InsufficientlyProtectedMessageReceived() {
297299
}
298300

299301
[Test, ExpectedException(typeof(ProtocolException))]
300-
public void IncomingMessageMissingRequiredParameters() {
302+
public async Task IncomingMessageMissingRequiredParameters() {
301303
var fields = GetStandardTestFields(FieldFill.IdentifiableButNotAllRequired);
302-
this.Channel.ReadFromRequest(CreateHttpRequestInfo("GET", fields));
304+
await this.Channel.ReadFromRequestAsync(CreateHttpRequestInfo("GET", fields), CancellationToken.None);
303305
}
304306
}
305307
}

0 commit comments

Comments
 (0)