ValueTask and ValueTask<T> are often allocation-free alternatives to Task and Task<T> respectively. They are used in many of the modern performance-oriented APIs in .NET Core 2.1+.
See https://github.com/dotnet/corefx/issues/27445 for more detail.
Test1 and Test2 in the example below (correctly) take ~1s to finish, Test3 and Test4 both complete after a few milliseconds.
Example:
public class UnitTest1
{
[Fact]
public Task Test1()
{
return Task.Delay(1000);
}
[Fact]
public async Task Test2()
{
await new ValueTask(Task.Delay(1000));
}
[Fact]
public ValueTask Test3()
{
return new ValueTask(Task.Delay(1000));
}
[Fact]
public async ValueTask Test4()
{
await new ValueTask(Task.Delay(1000));
}
}
This problem is actually a bit worse than mentioned, in that async tests that return ValueTask actually mask test failures. Both of these tests should fail, but only one does:
[Fact]
public async Task TaskTest () {
await new ValueTask(Task.Delay(10));
Assert.True(false);
}
[Fact]
public async ValueTask ValueTaskTest () {
await new ValueTask(Task.Delay(10));
Assert.True(false);
}
// <PackageReference Include="xunit" Version="2.4.1" />
// <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
We don't support test methods that return values, so ValueTask<T> shouldn't be an issue here.
Most helpful comment
This problem is actually a bit worse than mentioned, in that async tests that return ValueTask actually mask test failures. Both of these tests should fail, but only one does: