-
Notifications
You must be signed in to change notification settings - Fork 5
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
3 changed files
with
128 additions
and
1 deletion.
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,66 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace System | ||
{ | ||
|
||
public static partial class OperationExtensions | ||
{ | ||
public static OperationAwaiter<T> GetAwaiter<T>(this Operation<T> operation) | ||
{ | ||
return new OperationAwaiter<T>(operation); | ||
} | ||
|
||
public static OperationAwaiter GetAwaiter(this Operation operation) | ||
{ | ||
return new OperationAwaiter(operation); | ||
} | ||
|
||
} | ||
|
||
public class OperationAwaiter : INotifyCompletion | ||
{ | ||
private Operation _operation; | ||
|
||
public OperationAwaiter(Operation operation) | ||
{ | ||
_operation = operation; | ||
} | ||
public bool IsCompleted => true; | ||
|
||
public void OnCompleted(Action continuation) | ||
{ | ||
continuation(); | ||
} | ||
|
||
public void GetResult() | ||
{ | ||
_operation.Unwrap(); | ||
} | ||
} | ||
|
||
|
||
public class OperationAwaiter<T> : INotifyCompletion | ||
{ | ||
private Operation<T> _operation; | ||
|
||
public OperationAwaiter(Operation<T> operation) | ||
{ | ||
_operation = operation; | ||
} | ||
public bool IsCompleted => true; | ||
|
||
public void OnCompleted(Action continuation) | ||
{ | ||
continuation(); | ||
} | ||
|
||
public T GetResult() | ||
{ | ||
return _operation.Unwrap(); | ||
} | ||
} | ||
} |
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,61 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Tests | ||
{ | ||
[TestClass] | ||
public class AsyncTests | ||
{ | ||
[TestMethod] | ||
public async Task AwaitSuccessfulOperation() | ||
{ | ||
var operation = Operation.Success(10); | ||
|
||
var result = await operation; | ||
|
||
Assert.AreEqual(10, result); | ||
} | ||
|
||
[TestMethod] | ||
public async Task AwaitFailedOperation() | ||
{ | ||
var operation = Operation.Fail<int>("An Error Occured"); | ||
|
||
bool failed = false; | ||
try | ||
{ | ||
var result = await operation; | ||
} | ||
catch | ||
{ | ||
failed = true; | ||
} | ||
|
||
Assert.IsTrue(failed, "Operation Not Throwing Exception"); | ||
} | ||
|
||
[TestMethod] | ||
public void TestErrorCapturedInTask() | ||
{ | ||
var result = AsyncOpMethod(); | ||
|
||
Assert.IsTrue(result.IsFaulted); | ||
|
||
async Task<int> AsyncOpMethod() | ||
{ | ||
await Operation.Fail<int>("An Error Occured"); | ||
return 1; | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public async Task AwaitNonGenericOperation() | ||
{ | ||
var operation = Operation.Success(); | ||
await operation; | ||
} | ||
} | ||
} |
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