-
-
Notifications
You must be signed in to change notification settings - Fork 794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Await
anything & custom awaitable types
#1123
Conversation
Some other parts of Moq (e.g. the `ReturnBase` behavior or default value providers) already produce awaitables that needn't be lifted.
* `IAwaitableHandler` needs to become public so that it can be implemented for user-defined awaitable types. * `Await` methods for user-defined awaitable types may be defined any- where. They are required to be static and have exactly one parameter. That parameter gets used to look up a suitable `IAwaitableHandler`. * Handlers are registered with `AwaitableHandler.Register`. (For this reason `AwaitableHandler` is also made public.)
... by combining the existing `IAwaitableHandler` & `AwaitableHandler`.
This makes both default value providers and "inner mock" discovery work seamlessly for all known awaitable types. Also reduces code duplication a little.
case ExpressionType.Extension when (e is AwaitExpression awaitExpression): | ||
{ | ||
Split(awaitExpression.Operand, out r, out p); | ||
p.AwaitableHandler = awaitExpression.AwaitableHandler; | ||
return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An AwaitExpression
appears to get thrown away here. This might cause the "await" to disappear from the resulting setup's Expression
.
// NOTE: Depending on the exact scenario, the `Await` method name may get converted to | ||
// a faux `await` keyword (e.g. with `SetupSet`). Right now, we don't care too much about | ||
// that slight loss of accuracy, as long as a clue for the occurred await remains. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps, for overall consistency, all setup expressions should be post-processed to rewrite Await
calls as (await …)
. That might not be worth the trouble if we later decide to go in the oppsosite direction (i.e. if we want to try and preserve the original Await
method identity in expression trees reconstructed from delegates).
This is an improved redo of my previous PR #1008, and closes #1007.
What is this?
This PR achieves "await anything, anywhere" for Moq. It allows you to await tasks and other custom awaitable types right inside setup expressions:
If the
await
keyword were supported in LINQ expression trees, we wouldn't need a substitute method; but this is probably as close as we can get to C# syntax today.See #1007 for a few more examples how to use this.
Benefits over the existing async API (
.ReturnsAsync
,.ThrowsAsync
, etc.)Using
Await
, you can chain across async calls inside a single setup expressions; something that the existing APIs don't cover at all:It works in places where there currently aren't any dedicated async methods, e.g. in
Mock.Of
:The existing dedicated async methods aren't as feature-complete as the main setup API, and bringing them up to feature parity is a constant effort. Because
Await
is designed in a way that does not add any new setup methods, any feature added to the main API should automatically work for async methods, too.Writing a custom await operator:
task.Result()
instead of.Await(task)
Would you rather have your setup look like this?
This is fairly easy to do. Simply add this to your code:
Note that custom await-like operators need to return an actual value in order to work properly with
.SetupSet
,.SetupAdd
, and.SetupRemove
. In all other cases, the representation of your await method in the setup expression will only get inspected, but never actually called. (And even when it does get called by.SetupSet
and friends, the passed-in tasks / awaitable should already have completed successfully, so.Result
will be non-blocking.)Custom awaitable types
If you have any custom awaitable type that you want to use with Moq, you'll have to provide a little guidance and register a dedicated handler. Say this is your custom awaitable:
You will need to instruct Moq...
Cup<>
,Cup<>
, andTContents
value out of an existingCup<>
.This is done by registering an implementation of
Moq.Async.AwaitableHandler
:A third method
TryGetResult
for extracting theTContents
value is provided by the base class. (It will attempt to fetch the result via the awaiter.) Consider providing your own implementation for better performance.Once you register your handler with the Moq infrastructure, it will recognize your type and be able to provide default values etc.:
If you want to await your cups in setup expressions, you'll need a custom
Await
method for static type safety:(As mentioned above, the return value is only a dummy value to help along
.SetupSet
and friends.)You're done: