-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using DNS.Protocol.Utils; | ||
|
||
namespace DNS.Tests.Utils { | ||
|
||
public class TaskExtensionsTest { | ||
[Fact] | ||
public async Task WithoutCancellation() { | ||
object obj = new object(); | ||
CancellationTokenSource cts = new CancellationTokenSource(); | ||
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>(); | ||
Task<object> resultTask = tcs.Task.WithCancellation(cts.Token); | ||
|
||
tcs.SetResult(obj); | ||
object result = await resultTask; | ||
|
||
Assert.Same(obj, result); | ||
} | ||
|
||
[Fact] | ||
public async Task WithCancellation() { | ||
CancellationTokenSource cts = new CancellationTokenSource(); | ||
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>(); | ||
Task resultTask = tcs.Task.WithCancellation(cts.Token); | ||
|
||
cts.Cancel(); | ||
|
||
OperationCanceledException e = | ||
await Assert.ThrowsAsync<OperationCanceledException>(() => resultTask); | ||
Assert.Equal(cts.Token, e.CancellationToken); | ||
} | ||
|
||
[Fact] | ||
public async Task WithoutCancellationTimeout() { | ||
object obj = new object(); | ||
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>(); | ||
Task<object> resultTask = tcs.Task.WithCancellationTimeout(TimeSpan.FromMilliseconds(60000)); | ||
|
||
tcs.SetResult(obj); | ||
object result = await resultTask; | ||
|
||
Assert.Same(obj, result); | ||
} | ||
|
||
[Fact(Timeout = 30000)] | ||
public async Task WithoutCancellationTimeoutAndCancellationToken() { | ||
CancellationTokenSource cts = new CancellationTokenSource(); | ||
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>(); | ||
Task<object> resultTask = tcs.Task.WithCancellationTimeout(TimeSpan.FromMilliseconds(60000), cts.Token); | ||
|
||
cts.Cancel(); | ||
|
||
await Assert.ThrowsAsync<OperationCanceledException>(() => resultTask); | ||
} | ||
|
||
[Fact] | ||
public async Task WithCancellationTimeout() { | ||
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>(); | ||
Task resultTask = tcs.Task.WithCancellationTimeout(TimeSpan.FromMilliseconds(100)); | ||
|
||
await Assert.ThrowsAsync<OperationCanceledException>(() => resultTask); | ||
} | ||
} | ||
} |