Exceptions in unit tests in secondary threads kills vstesthost

Hi,

I was told at the PDC to log this issue here. I have some asynchronous APIs like BeginWork(). I am writing some unit tests for these APIs. These APIs create threads to do the work and fire events to signify that work is complete. If there are uncaught exceptions in these secondary threads, VsTestHost dies without performing any further unit tests in the list which have not been executed yet.

I wanted to know if this is a bug. If it is not, is there a workaround

Thanks
Pawan


Answer this question

Exceptions in unit tests in secondary threads kills vstesthost

  • Steele

    Here is a small sample for my class which throws exception.

        public class WorkPerformer {
            public event EventHandler<EventArgs> WorkComplete;
            public void BeginWork() {
                Thread t = new Thread(new ThreadStart(WorkProc));
                t.Start();
            }
            private void WorkProc() {
                Thread.Sleep(5000);
                string a = null;
                int b = a.Length;
                if (WorkComplete != null) {
                    EventArgs e = new EventArgs();
                    WorkComplete(this, e);
                }
            }
        }

    // create a new unit test project for this class and add this method in it
    // you will need using System.Threading;
            ManualResetEvent workCompleteEvent = new ManualResetEvent(false);
            /// <summary>
            ///A test case for BeginWork ()
            ///</summary>
            [TestMethod()]
            public void BeginWorkTest() {
                WorkPerformer target = new WorkPerformer();
                target.WorkComplete += new System.EventHandler<System.EventArgs>(target_WorkComplete);
                target.BeginWork();
                bool timeout = workCompleteEvent.WaitOne(30000, true);
                Assert.IsFalse(timeout);
            }

            void target_WorkComplete(object sender, System.EventArgs e) {
                workCompleteEvent.Set();
            }




  • PaulDrda

    Pawan,

    Yes, as a workaround you can enable legacy unhandled exception exception handling by doing the following in vstesthost.exe.config:
    <configuration>
        <runtime>
            <legacyUnhandledExceptionPolicy enabled="1"/>
        </runtime>
    </configuration>
    You can find more information about this here: http://msdn2.microsoft.com/en-us/library/ms228965.aspx

    When exception is thrown on background thread we cannot reliably say which test caused the exception as when exception is thrown another test could be in progress.

    In Beta 2 build if you enable legacy exception handling the exception will be ignored and if you don't do synchronization between test thread and backgruond threads yourself you may see that test run has passed without any errors. In RC/RTM build Test run will still be error (and you will see the exception and the call stack in Test Run details window) even if you may see that all tests passed.

    Thank you,
    Michael

  • Ricky A Jones


    Where is your callback method written I assume your callback method that is called by the asynchronous method is also written in the test project.

    Please give a little more details about your setup. I tried a basic scenario and it looks like what you are saying is true. But it will be nice to get a small example from you to decide the exact solution.

    -Munjal

  • Steve Hittle

    There is not much we can do when unhandled exception is thrown on secondary thread. In post-Beta2 builds we log the exception but CLR still terminates the program. This is by design. The workaround is to catch exceptions that your threads may throw.

    Thanks,
    Michael 

  • Devo64

    I understand that - but when I have a unit test suit for 1000 tests and 200th one stops my unit testing because some designer checked in a bug, I get 800 not executed tests. This is not acceptable - you cannot say that it is by design because it is "INCORRECT" design.

    In 2.0, app.config allows one to configure process so that CLR continues the process if secondary thread dies. If you expose some app.config for VSTESTHOST.exe, we can achieve our goal without any software support from Microsoft.

    -Pawan

  • Exceptions in unit tests in secondary threads kills vstesthost