Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Run issue with async method
HakanL
#1 Posted : Thursday, April 17, 2014 3:11:18 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 4/16/2014(UTC)
Posts: 3
Location: United States of America

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:

Quote:
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
Remco
#2 Posted : Thursday, April 17, 2014 4:25:53 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 5,234

Thanks: 691 times
Was thanked: 843 time(s) in 803 post(s)
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
HakanL
#3 Posted : Thursday, April 17, 2014 5:10:29 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 4/16/2014(UTC)
Posts: 3
Location: United States of America

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
Remco
#4 Posted : Thursday, April 17, 2014 5:12:08 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 5,234

Thanks: 691 times
Was thanked: 843 time(s) in 803 post(s)
Absolutely. The new error is: Expected a return type of 'System.Threading.Tasks.Task' on an async method
HakanL
#5 Posted : Thursday, April 17, 2014 5:18:54 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 4/16/2014(UTC)
Posts: 3
Location: United States of America

Perfect! Thanks for the super-quick help!
jschreuder
#6 Posted : Thursday, May 03, 2018 1:30:43 AM(UTC)
Rank: Member

Groups: Registered
Joined: 10/5/2015(UTC)
Posts: 25
Location: Australia

Thanks: 6 times
Was thanked: 5 time(s) in 5 post(s)
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.

Quote:

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


Getting error message

Quote:

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
Remco
#7 Posted : Thursday, May 03, 2018 2:15:14 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 5,234

Thanks: 691 times
Was thanked: 843 time(s) in 803 post(s)
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?
1 user thanked Remco for this useful post.
jschreuder on 5/3/2018(UTC)
jschreuder
#8 Posted : Thursday, May 03, 2018 2:43:04 AM(UTC)
Rank: Member

Groups: Registered
Joined: 10/5/2015(UTC)
Posts: 25
Location: Australia

Thanks: 6 times
Was thanked: 5 time(s) in 5 post(s)
Remco;12137 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!
b33rdy
#9 Posted : Tuesday, June 12, 2018 11:50:14 AM(UTC)
Rank: Member

Groups: Registered
Joined: 6/28/2016(UTC)
Posts: 10
Location: United Kingdom

Thanks: 5 times
Was thanked: 4 time(s) in 3 post(s)
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!)

Code:
      [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
Remco
#10 Posted : Wednesday, June 13, 2018 5:28:08 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 5,234

Thanks: 691 times
Was thanked: 843 time(s) in 803 post(s)
1 user thanked Remco for this useful post.
b33rdy on 6/13/2018(UTC)
b33rdy
#11 Posted : Wednesday, June 13, 2018 8:15:59 AM(UTC)
Rank: Member

Groups: Registered
Joined: 6/28/2016(UTC)
Posts: 10
Location: United Kingdom

Thanks: 5 times
Was thanked: 4 time(s) in 3 post(s)
1 user thanked b33rdy for this useful post.
Remco on 6/13/2018(UTC)
Users browsing this topic
Guest (4)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.082 seconds.