Merge pull request #5382 from kumpera/pedump_fix
[mono.git] / mono / tests / static-constructor.cs
index e520de65796f4bdd8921f9acdae0a352bdbc257c..89df82d896672aee64d806b09379011edd315430 100644 (file)
@@ -1,6 +1,7 @@
 
 using System;
 using System.Reflection;
+using System.Threading;
 
 struct A {
        public A (int i) {
@@ -12,6 +13,21 @@ struct B {
        }
 }
 
+public class StaticInitFails {
+
+       public static string s;
+
+       static StaticInitFails () {
+               Thread.Sleep (1000);
+               throw new Exception ();
+               s = "FOO";
+       }
+
+       public static void foo () {
+               Console.WriteLine (s);
+       }
+}
+
 public class Tests {
        static int last = 42;
        static int burp;
@@ -36,9 +52,46 @@ public class Tests {
                ConstructorInfo con1 = typeof (A).GetConstructor (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type [1] { typeof (int) }, null);
                ConstructorInfo con2 = typeof (B).GetConstructor (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type [1] { typeof (int) }, null);
 
-               con1.Invoke (null, new Object [] { 0 });
-               con2.Invoke (null, new Object [] { 0 });
+               con1.Invoke (new Object [] { 0 });
+               con2.Invoke (new Object [] { 0 });
+
+               // Test what happens when static initialization throws an exception
+               // First start a thread which will trigger the initialization
+               Thread t = new Thread (new ThreadStart (Run));
+               t.Start ();
+
+               Thread.Sleep (500);
+
+               // While the thread waits, trigger initialization on this thread too so this
+               // thread will wait for the other thread to finish initialization
+               try {
+                       Run2 ();
+                       return 1;
+               }
+               catch (TypeInitializationException ex) {
+               }
+
+               // Try again synchronously
+               try {
+                       Run2 ();
+                       return 1;
+               }
+               catch (TypeInitializationException ex) {
+               }
 
                return 0;
        }
+
+       private static void Run () {
+               try {
+                       StaticInitFails.foo ();
+               }
+               catch (Exception) {
+               }
+       }
+
+       private static void Run2 () {
+               StaticInitFails.foo ();
+       }
+
 }