question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for ValueTask

See original GitHub issue

Currently FakeItEasy has special support for Task and Task<T>:

  • Dummy creation strategy: return a completed Task, or Task<T> whose result is a dummy T
  • Special overload for configuring the return value of a method that returns Task<T>, where the user can just pass the task’s value instead of the task itself.

We should consider supporting ValueTask and ValueTask<T> as well. But it might not be as simple as for Task and Task<T>

  • ValueTask<T> is only available in .NET Core (all versions) and .NET Standard 2.1
  • ValueTask is only available in .NET Core 2.1+ and .NET Standard 2.1
  • BUT there’s a NuGet package supported on .NET Standard 1.0+ that exposes both types

So, if we add support for ValueTask only in a new .NET Standard 2.1 target, projects that target TFMs not compatible with .NET Standard 2.1 and use the package won’t benefit from it. A possible approach would be to offer the Returns overloads for ValueTask in a separate package, and support dummy creation dynamically (like we did for ValueTuple)

Also, ValueTask[<T>] doesn’t work exactly the same as Task[<T>], there are a few subtleties. For instance, you can’t await the same ValueTask[<T>] more than once, which will have to be taken into account for dummy creation (we’ll have to return a new one for each call).

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
thomaslevesquecommented, Oct 15, 2019

Would they not also all of a sudden have conflicting ValueTask types?

No, because the netcoreapp3.0/netstandard2.1 TFMs of this package don’t contain the ValueTask type.

But yes, the problem is fairly easy to fix. And we could even avoid the error entirely, by multitargeting the package. The netcoreapp3.0/netstandard2.1 TFMs would contain nothing, since FakeItEasy would already define the extension methods for these TFMs. We could consider including a target that causes a warning (like we did for FakeItEasy.Analyzer) to indicate the package is no longer necessary.

1reaction
thomaslevesquecommented, Oct 15, 2019

The same task is returned for every call.

Ah, it looks like my assumption was wrong… in fact we lazily create a new task every time. Just ignore me, we can do exactly the same for ValueTask

Also, synchronously completed ValueTasks (that just contain a value) can be awaited multiple times, apparently. So there’s no problem here after all.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding the Whys, Whats, and Whens of ValueTask
In this post, I'll cover the newer ValueTask / ValueTask<TResult> types, which were introduced to help improve asynchronous performance in ...
Read more >
ValueTask Struct (System.Threading.Tasks)
A ValueTask is a structure that can wrap either a Task or a IValueTaskSource instance. Returning a ValueTask that wraps a IValueTaskSource instance...
Read more >
How to use ValueTask in C# | InfoWorld
Take advantage of ValueTask in C# to avoid allocation when returning task objects from an asynchronous method.
Read more >
Support for ValueTask · Issue #845 · App-vNext/Polly
Hi Any plans for adding support for ValueTask? Adding: ValueTask ExecuteAsync(Func\ action); and so on to IAsyncPolicy.
Read more >
Understanding Task and ValueTask in C# - Code Maze
The Task class resides under the System.Threading.Tasks namespace. Task help us execute a section of our code in the thread pool outside of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found