[tests] Add unhandled exception tests with AppDomain.UnhandledException managed handler
authorLudovic Henry <ludovic@xamarin.com>
Wed, 2 Mar 2016 14:37:01 +0000 (14:37 +0000)
committerLudovic Henry <ludovic@xamarin.com>
Tue, 8 Mar 2016 11:32:18 +0000 (11:32 +0000)
mono/tests/Makefile.am
mono/tests/unhandled-exception-1.cs
mono/tests/unhandled-exception-2.cs
mono/tests/unhandled-exception-3.cs
mono/tests/unhandled-exception-4.cs
mono/tests/unhandled-exception-5.cs
mono/tests/unhandled-exception-6.cs
mono/tests/unhandled-exception-7.cs
mono/tests/unhandled-exception-8.cs [deleted file]

index c193d9cd23e13dc77da1be17eaa09c105b791b19..3ca680c5d3cb083bb60a6fffe7b4a408db2d2854 100644 (file)
@@ -1376,25 +1376,35 @@ test-oom: $(OOM_TESTS)
 if HOST_WIN32
 test-unhandled-exception-2:
 else
-test-unhandled-exception-2: test-unhandled-exception-2-1 test-unhandled-exception-2-2 test-unhandled-exception-2-3 test-unhandled-exception-2-4        \
-                test-unhandled-exception-2-5 test-unhandled-exception-2-6 test-unhandled-exception-2-7 test-unhandled-exception-2-8
-
-test-unhandled-exception-2-1: unhandled-exception-1.exe test-runner.exe
-       @$(RUNTIME) ./test-runner.exe --testsuite-name $@ --expected-exit-code 1 $<
-test-unhandled-exception-2-2: unhandled-exception-2.exe test-runner.exe
-       @$(RUNTIME) ./test-runner.exe --testsuite-name $@ --expected-exit-code 0 $<
-test-unhandled-exception-2-3: unhandled-exception-3.exe test-runner.exe
-       @$(RUNTIME) ./test-runner.exe --testsuite-name $@ --expected-exit-code 255 $<
-test-unhandled-exception-2-4: unhandled-exception-4.exe test-runner.exe
-       @$(RUNTIME) ./test-runner.exe --testsuite-name $@ --expected-exit-code 0 $<
-test-unhandled-exception-2-5: unhandled-exception-5.exe test-runner.exe
-       @$(RUNTIME) ./test-runner.exe --testsuite-name $@ --expected-exit-code 255 $<
-test-unhandled-exception-2-6: unhandled-exception-6.exe test-runner.exe
-       @$(RUNTIME) ./test-runner.exe --testsuite-name $@ --expected-exit-code 255 $<
-test-unhandled-exception-2-7: unhandled-exception-7.exe test-runner.exe
-       @$(RUNTIME) ./test-runner.exe --testsuite-name $@ --expected-exit-code 255 $<
-test-unhandled-exception-2-8: unhandled-exception-8.exe test-runner.exe
-       @$(RUNTIME) ./test-runner.exe --testsuite-name $@ --expected-exit-code 3 $<
+
+# tests that expect a 1 exit code
+UNHANDLED_EXCEPTION_1_TESTS =  \
+       unhandled-exception-1.exe
+
+# tests that expect a 255 exit code
+UNHANDLED_EXCEPTION_255_TESTS =        \
+       unhandled-exception-2.exe       \
+       unhandled-exception-3.exe       \
+       unhandled-exception-4.exe       \
+       unhandled-exception-5.exe       \
+       unhandled-exception-6.exe       \
+       unhandled-exception-7.exe
+
+test-unhandled-exception-2: $(UNHANDLED_EXCEPTION_1_TESTS) $(UNHANDLED_EXCEPTION_255_TESTS)
+       $(MAKE) test-unhandled-exception-2-1-with-managed-handler
+       $(MAKE) test-unhandled-exception-2-1-without-managed-handler
+       $(MAKE) test-unhandled-exception-2-255-with-managed-handler
+       $(MAKE) test-unhandled-exception-2-255-without-managed-handler
+
+test-unhandled-exception-2-1-with-managed-handler: $(UNHANDLED_EXCEPTION_1_TESTS) test-runner.exe
+       $(RUNTIME) ./test-runner.exe -j a --testsuite-name $@ --expected-exit-code 1 $(UNHANDLED_EXCEPTION_1_TESTS)
+test-unhandled-exception-2-1-without-managed-handler: $(UNHANDLED_EXCEPTION_1_TESTS) test-runner.exe
+       TEST_UNHANDLED_EXCEPTION_HANDLER=1 $(RUNTIME) ./test-runner.exe -j a --testsuite-name $@ --expected-exit-code 1 $(UNHANDLED_EXCEPTION_1_TESTS)
+test-unhandled-exception-2-255-with-managed-handler: $(UNHANDLED_EXCEPTION_255_TESTS) test-runner.exe
+       $(RUNTIME) ./test-runner.exe -j a --testsuite-name $@ --expected-exit-code 255 $(UNHANDLED_EXCEPTION_255_TESTS)
+test-unhandled-exception-2-255-without-managed-handler: $(UNHANDLED_EXCEPTION_255_TESTS) test-runner.exe
+       TEST_UNHANDLED_EXCEPTION_HANDLER=1 $(RUNTIME) ./test-runner.exe -j a --testsuite-name $@ --expected-exit-code 255 $(UNHANDLED_EXCEPTION_255_TESTS)
+
 endif
 
 EXTRA_DIST += appdomain-loader.cs appdomain-tester.cs
index 2e89dcffdefd465ae4e120717f838a23a63e2402..90ff5e6162fe270766442bc7fbf64a63b1050d40 100644 (file)
@@ -12,6 +12,9 @@ class Driver
        /* Expected exit code: 1 */
        static void Main (string[] args)
        {
+               if (Environment.GetEnvironmentVariable ("TEST_UNHANDLED_EXCEPTION_HANDLER") != null)
+                       AppDomain.CurrentDomain.UnhandledException += (s, e) => {};
+
                ManualResetEvent mre = new ManualResetEvent (false);
 
                var t = new Thread (new ThreadStart (() => { try { throw new CustomException (); } finally { mre.Set (); } }));
@@ -20,7 +23,9 @@ class Driver
                if (!mre.WaitOne (5000))
                        Environment.Exit (2);
 
-               t.Join ();
+               /* Give a chance to the thread to finish executing the exception unwinding
+                * after the finally, before we exit with status 0 on the current thread */
+               Thread.Sleep (1000);
 
                Environment.Exit (0);
        }
index 7c9cc6999fa7ca5ea9611c442f376c49cd78b785..cb05c269fd82c0f31e220092d8744d70dc519868 100644 (file)
@@ -9,9 +9,12 @@ class CustomException : Exception
 
 class Driver
 {
-       /* expected exit code: 0 */
+       /* expected exit code: 255 */
        static void Main (string[] args)
        {
+               if (Environment.GetEnvironmentVariable ("TEST_UNHANDLED_EXCEPTION_HANDLER") != null)
+                       AppDomain.CurrentDomain.UnhandledException += (s, e) => {};
+
                ManualResetEvent mre = new ManualResetEvent (false);
 
                var a = new Action (() => { try { throw new CustomException (); } finally { mre.Set (); } });
@@ -24,11 +27,13 @@ class Driver
                        a.EndInvoke (ares);
                        Environment.Exit (4);
                } catch (CustomException) {
+                       /* expected behaviour */
+                       Environment.Exit (255);
                } catch (Exception ex) {
                        Console.WriteLine (ex);
                        Environment.Exit (3);
                }
 
-               Environment.Exit (0);
+               Environment.Exit (5);
        }
 }
index 2afa9ad3505d7c17fc72f37343b3455da946aa29..5805614c69131c08992c771af7b1247a932fc2f8 100644 (file)
@@ -9,9 +9,12 @@ class CustomException : Exception
 
 class Driver
 {
-       /* expected exit code: 0 */
+       /* expected exit code: 255 */
        static void Main (string[] args)
        {
+               if (Environment.GetEnvironmentVariable ("TEST_UNHANDLED_EXCEPTION_HANDLER") != null)
+                       AppDomain.CurrentDomain.UnhandledException += (s, e) => {};
+
                ManualResetEvent mre = new ManualResetEvent (false);
 
                ThreadPool.QueueUserWorkItem (_ => { try { throw new CustomException (); } finally { mre.Set (); } });
@@ -19,8 +22,8 @@ class Driver
                if (!mre.WaitOne (5000))
                        Environment.Exit (2);
 
-               /* Give a chance to the threadpool thread to finish executing the exception
-                * unwinding after the finally, before we exit with status 0 on the current thread */
+               /* Give a chance to the threadpool thread to finish executing the exception unwinding
+                * after the finally, before we exit with status 0 on the current thread */
                Thread.Sleep (1000);
 
                Environment.Exit (0);
index 9658316854b390467d7e4e297c77688167f4f1ff..4a85514e58bf8f9e7ee11817ef455cf4b16e471e 100644 (file)
@@ -9,9 +9,12 @@ class CustomException : Exception
 
 class Driver
 {
-       /* expected exit code: 0 */
+       /* expected exit code: 255 */
        static void Main (string[] args)
        {
+               if (Environment.GetEnvironmentVariable ("TEST_UNHANDLED_EXCEPTION_HANDLER") != null)
+                       AppDomain.CurrentDomain.UnhandledException += (s, e) => {};
+
                ManualResetEvent mre = new ManualResetEvent (false);
 
                var t = Task.Factory.StartNew (new Action (() => { try { throw new CustomException (); } finally { mre.Set (); } }));
@@ -23,13 +26,16 @@ class Driver
                        t.Wait ();
                        Environment.Exit (5);
                } catch (AggregateException ae) {
-                       if (!(ae.InnerExceptions [0] is CustomException))
-                               Environment.Exit (4);
+                       Console.WriteLine (ae);
+                       if (ae.InnerExceptions [0] is CustomException) {
+                               /* expected behaviour */
+                               Environment.Exit (255);
+                       }
                } catch (Exception ex) {
                        Console.WriteLine (ex);
                        Environment.Exit (3);
                }
 
-               Environment.Exit (0);
+               Environment.Exit (6);
        }
 }
index 33136b33590b44acb8f884f480caa70f997efde2..65cff47d992292b5be579de808e5f74902d20926 100644 (file)
@@ -26,6 +26,9 @@ class Driver
        /* expected exit code: 255 */
        static void Main (string[] args)
        {
+               if (Environment.GetEnvironmentVariable ("TEST_UNHANDLED_EXCEPTION_HANDLER") != null)
+                       AppDomain.CurrentDomain.UnhandledException += (s, e) => {};
+
                new FinalizedClass();
 
                GC.Collect ();
@@ -34,6 +37,10 @@ class Driver
                if (!mre.WaitOne (5000))
                        Environment.Exit (2);
 
+               /* Give a chance to the finalizer thread to finish executing the exception unwinding
+                * after the finally, before we exit with status 0 on the current thread */
+               Thread.Sleep (1000);
+
                Environment.Exit (0);
        }
 }
index adce560b819c33c5cecacea93042e40cf10bdbfa..7ed54d333006cfbf8d83185ffc7d5b9d720d9369 100644 (file)
@@ -10,9 +10,12 @@ class CustomException : Exception
 
 class Driver
 {
-       /* expected exit code: 0 */
+       /* expected exit code: 255 */
        static void Main (string[] args)
        {
+               if (Environment.GetEnvironmentVariable ("TEST_UNHANDLED_EXCEPTION_HANDLER") != null)
+                       AppDomain.CurrentDomain.UnhandledException += (s, e) => {};
+
                var action = new Action (Delegate);
                var ares = action.BeginInvoke (Callback, null);
 
index 97835b97c97dc0945bb8c8f900ea5f0e8b88c9be..87c5fbb8cb17137e5df5aebcfccd7edfe88d11c9 100644 (file)
@@ -28,12 +28,20 @@ class CrossDomain : MarshalByRefObject
 
 class Driver
 {
-       /* expected exit code: 0 */
+       /* expected exit code: 255 */
        static void Main (string[] args)
        {
+               if (Environment.GetEnvironmentVariable ("TEST_UNHANDLED_EXCEPTION_HANDLER") != null)
+                       AppDomain.CurrentDomain.UnhandledException += (s, e) => {};
+
                ManualResetEvent mre = new ManualResetEvent (false);
 
-               var cd = (CrossDomain) AppDomain.CreateDomain ("ad").CreateInstanceAndUnwrap (typeof(CrossDomain).Assembly.FullName, "CrossDomain");
+               var ad = AppDomain.CreateDomain ("ad");
+
+               if (Environment.GetEnvironmentVariable ("TEST_UNHANDLED_EXCEPTION_HANDLER") != null)
+                       ad.UnhandledException += (s, e) => {};
+
+               var cd = (CrossDomain) ad.CreateInstanceAndUnwrap (typeof(CrossDomain).Assembly.FullName, "CrossDomain");
 
                var action = cd.NewDelegateWithoutTarget ();
                var ares = action.BeginInvoke (Callback, null);
diff --git a/mono/tests/unhandled-exception-8.cs b/mono/tests/unhandled-exception-8.cs
deleted file mode 100644 (file)
index d02896b..0000000
+++ /dev/null
@@ -1,55 +0,0 @@
-using System;
-using System.Diagnostics;
-using System.Threading;
-using System.Threading.Tasks;
-
-class CustomException : Exception
-{
-}
-
-class CustomException2 : Exception
-{
-}
-
-class CrossDomain : MarshalByRefObject
-{
-       public Action NewDelegateWithTarget ()
-       {
-               return new Action (Bar);
-       }
-
-       public Action NewDelegateWithoutTarget ()
-       {
-               return () => { throw new CustomException (); };
-       }
-
-       public void Bar ()
-       {
-               throw new CustomException ();
-       }
-}
-
-class Driver
-{
-       /* expected exit code: 3 */
-       static void Main (string[] args)
-       {
-               ManualResetEvent mre = new ManualResetEvent (false);
-
-               var cd = (CrossDomain) AppDomain.CreateDomain ("ad").CreateInstanceAndUnwrap (typeof(CrossDomain).Assembly.FullName, "CrossDomain");
-
-               var a = cd.NewDelegateWithTarget ();
-               var ares = a.BeginInvoke (delegate { throw new CustomException2 (); }, null);
-
-               try {
-                       a.EndInvoke (ares);
-                       Environment.Exit (4);
-               } catch (CustomException) {
-               } catch (Exception ex) {
-                       Console.WriteLine (ex);
-                       Environment.Exit (3);
-               }
-
-               Environment.Exit (0);
-       }
-}