Build/Test Issues

Run issue with async method

Started by HakanL on 13,992 views

I'm getting a strange exception in nCrunch 2.5.0.12. I have a test method and if I change it to use async (it makes an async call) then it fails with this exception while running it:

System.NullReferenceException: Object reference not set to an instance of an object.
at nCrunch.Reflection.Clr.ClrMethod.Invoke(Object instance, Object[] parameters)



Here's the method: Test method

And this is the detailed log from nCrunch: Detailed log

The method actually executes, the exception happens after the method is completed.

If I remove the async/await (and just add .Result, as a test) then it runs fine. But I still need to call this with async/await on other (also failing) methods so I can properly catch for exceptions (instead of AggregateException).

Thanks,
/Hakan
Hi,

Thanks for sharing this issue.

NCrunch's MSTest runner doesn't support execution of 'async void' tests, because the behaviour is itself modeled off MSTest, which won't recognise 'async void' tests.

The error message is bad, and should be improved. I'll make an adjustment so that it is more meaningful. I suggest making the test 'async Task' instead. See http://stackoverflow.com/questions/19317135/why-cant-async-void-unit-tests-be-recognized for more details.


Cheers,

Remco
Ahh, mistake on my end, I didn't mean to have these as void, I just forgot to change them to Task (they were sync from the beginning) and I was thrown off by the error. Could we perhaps have a more meaningful error message, I spent several hours trying to figure this one out?

Thanks,
/Hakan
Absolutely. The new error is: Expected a return type of 'System.Threading.Tasks.Task' on an async method
Perfect! Thanks for the super-quick help!
Sorry to bump this old thread, semi related though.

Has there been a change / regression in this area?

Tests of form async Task now seem to be failing, eg.


[TestMethod]
public async Task AsyncTest()
{
await Task.CompletedTask;
}


Getting error message


Expected a return type of 'System.Threading.Tasks.Task' on an async method


I seem to remember having no problems with these in the past though I could be wrong.

This is in v3.15.0.6

Thanks in advance

Edited

Hi, thanks for sharing this.

The code above seems to work fine for me (can't reproduce the issue).

Is it possible that there is a mismatch between the System.Threading.Tasks.Task type being returned and the one expected by the runner? This could be caused by an assembly referencing issue. Do you have two different versions of this class present within the application domain? You can try the preload assembly references setting to see if this helps.

Edit: I've just reviewed the code involved here. NCrunch will throw this exception under the following conditions:

1. If the return value from the async test is null
2. If the return value does not have a full type name of "System.Threading.Tasks.Task" and does not inherit from a type with this name.

Maybe you can try hooking a debugger onto the test and examining its return result. I'm not sure why this is different in your environment compared with mine. Do you get this with a small sample project?

Edited

Remco wrote:Hi, thanks for sharing this.

The code above seems to work fine for me (can't reproduce the issue).

Is it possible that there is a mismatch between the System.Threading.Tasks.Task type being returned and the one expected by the runner? This could be caused by an assembly referencing issue. Do you have two different versions of this class present within the application domain? You can try the preload assembly references setting to see if this helps.

Edit: I've just reviewed the code involved here. NCrunch will throw this exception under the following conditions:

1. If the return value from the async test is null
2. If the return value does not have a full type name of "System.Threading.Tasks.Task" and does not inherit from a type with this name.

Maybe you can try hooking a debugger onto the test and examining its return result. I'm not sure why this is different in your environment compared with mine. Do you get this with a small sample project?


Never mind, I am a bit dopey and had a TestInitialize method which was async void, whereas all my tests are async Task.
Sorry about the confusion!
Our team has exactly this (most recent) issue - `Expected a return type of 'System.Threading.Tasks.Task' on an async method` using the latest NCrunch, VS 2017 and .NET Core 2.1.

These tests were running without issue previously.

The most basic test that's failing has no initialization and fails on an `await Task.Delay(50)` (we know this is bad practice to include the delay, so we'll leave that discussion for another day!)

      [TestMethod]
        async public Task CanEnqueue_GivenOperationsOlderThanSlidingWindowAndOverMaximum_ReturnsTrue()
        {
            // Arrange
            MaximumOperationsGate gate = new MaximumOperationsGate(TimeSpan.FromTicks(1), 1);
            gate.Enqueue();

            await Task.Delay(50);

            // Act
            bool result = gate.CanEnqueue();

            // Assert
            result
                .Should()
                .BeTrue();
        }


Here's a link to dummy project with that exact test in it that replicates the issue: https://1drv.ms/u/s!AmaPTHdCBAnDtp5-vBWzqpvterPb7g

Edited

Thanks for sharing this problem. Your sample solution was a huge help in quickly identifying the issue.

This has been caused by a small change in the way that the .NET Core compiler handles async methods.

Would you be interested in trying the build below? This includes a targeted fix:

NCrunch_Console_3.18.0.1.msi
NCrunch_Console_3.18.0.1.zip
NCrunch_GridNodeServer_3.18.0.1.msi
NCrunch_GridNodeServer_3.18.0.1.zip
NCrunch_LicenseServer_3.18.0.1.zip
NCrunch_VS2008_3.18.0.1.msi
NCrunch_VS2010_3.18.0.1.msi
NCrunch_VS2010_3.18.0.1.zip
NCrunch_VS2012_3.18.0.1.msi
NCrunch_VS2012_3.18.0.1.zip
NCrunch_VS2013_3.18.0.1.msi
NCrunch_VS2013_3.18.0.1.zip
NCrunch_VS2015_3.18.0.1.msi
NCrunch_VS2015_3.18.0.1.msi.7z
NCrunch_VS2015_3.18.0.1.zip
NCrunch_VS2017_3.18.0.1.msi
NCrunch_VS2017_3.18.0.1.msi.7z
NCrunch_VS2017_3.18.0.1.zip
That worked flawlessly.

Thank-you!

Remco wrote:Thanks for sharing this problem. Your sample solution was a huge help in quickly identifying the issue.

This has been caused by a small change in the way that the .NET Core compiler handles async methods.

Would you be interested in trying the build below? This includes a targeted fix:

NCrunch_Console_3.18.0.1.msi
NCrunch_Console_3.18.0.1.zip
NCrunch_GridNodeServer_3.18.0.1.msi
NCrunch_GridNodeServer_3.18.0.1.zip
NCrunch_LicenseServer_3.18.0.1.zip
NCrunch_VS2008_3.18.0.1.msi
NCrunch_VS2010_3.18.0.1.msi
NCrunch_VS2010_3.18.0.1.zip
NCrunch_VS2012_3.18.0.1.msi
NCrunch_VS2012_3.18.0.1.zip
NCrunch_VS2013_3.18.0.1.msi
NCrunch_VS2013_3.18.0.1.zip
NCrunch_VS2015_3.18.0.1.msi
NCrunch_VS2015_3.18.0.1.msi.7z
NCrunch_VS2015_3.18.0.1.zip
NCrunch_VS2017_3.18.0.1.msi
NCrunch_VS2017_3.18.0.1.msi.7z
NCrunch_VS2017_3.18.0.1.zip

Edited

Post a reply

Log in to reply.