Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6225775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:54:18+00:00 2026-05-24T08:54:18+00:00

My team makes extensive use of NUnit unit tests in our C# project. Recently

  • 0

My team makes extensive use of NUnit unit tests in our C# project. Recently we have started using the Task Parallel Library (TPL) in .NET 4, which has introduced a wrinkle for us.

In the TPL, if a Task is faulted (that is, an exception was thrown while executing the task), that task’s Exception property must be retrieved at least once. If it is not, when the Task object is finalized by the garbage collector an exception will be thrown which terminates the process.

It is possible to detect and prevent this by registering a handler for the TaskScheduler.UnobservedTaskException. We have done that for a few of our test cases to reproduce unobserved task exception bugs, but I would rather have some way to modify the way NUnit runs the tests so that for each test, an UnobservedTaskException handler is registered, then after that test a garbage collection is forced to flush out any tasks with unobserved exceptions. I would then like this to cause the test to fail.

How are other teams solving this problem? How are you detecting test cases that pass (that is, complete without any exceptions) but leave one or more Task objects with unobserved exceptions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:54:20+00:00Added an answer on May 24, 2026 at 8:54 am

    Here is how I do it:

    [Test]
    public void ObservesTaskException()
    {
        bool wasUnobservedException = false;
    
        TaskScheduler.UnobservedTaskException += 
            (s, args) => wasUnobservedException = true;
    
        CauseATaskToThrowInTheSystemUnderTest();
    
        GC.Collect();
        GC.WaitForPendingFinalizers();
    
        Assert.That(wasUnobservedException, Is.False);
    }
    

    The call to CauseATaskToThrowInTheSystemUnderTest() is a placeholder for whatever you need to do. I recommend wrapping your code in a function like this because it aids in making sure the Task object that throws the exception is unreachable when the GC runs. If the Task object is reachable, the finalizer won’t run and this test will not test anything.

    Obviously, it is also important to make sure the task has completed before garbage collection occurs. How you do this (if you even can) is dependent on your particular code. Perhaps the flow of your program ensures this is the case. If not, and if your test can access to the Task object, you can do the following:

    var continuation = GetTheTaskFromTheSystemUnderTest();
        .ContinueWith(t => {});
    
    CauseATaskToThrowInTheSystemUnderTest();
    
    bool isTaskCompleted = continuation.Wait(SomeSuitableTimeout);
    

    You should add isTaskCompleted to your assertion.

    The timeout is there just in case the code is broken in the future–you don’t want your test to hang. The value should be very small. If you find you actually need to wait very long for your task, your test suite will likely be too slow for frequent use.

    Having access to the tasks you create (including continuation tasks you create on other tasks) is one of the considerations to keep in mind while designing for testability. When doing so, the usual tradeoffs apply. I try to include tests like this when my code creates tasks–this is an important behavior of my code and it needs to be tested.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our team is currently working on a large project which makes heavy use of
Our team is willing to unit-test a new code written under a running project
Our project has about 20 developers, but our application makes relatively light use of
Each of our team members runs all kind of tests and makes sure all
Our team has just started unittesting and mocking, and we have run into some
In my current project I am using boost::shared_ptr quite extensively. Recently my fellow team
Our team have built a web application using Ruby on Rails. It currently doesn't
Our team is just ramping up to use source control through SVN. We are
Our team recently upgraded to TeamCity 4.5.4 but we're having trouble with TeamCity running
My team (4 people) have just reached a major milestone in our development, putting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.