f1f6c2f932069f57ae67e85f017d8f47b9f50fc6
[mono.git] / mono / tests / sgen-domain-unload-2.cs
1 using System;
2 using System.Threading;
3
4 /*
5 This test stresses what happens when root domain threads are allocating into the nursery
6 while a domain is cleaned up.
7
8 This is a regression test for a crash in the domain object cleaner code that did not
9 stop-the-world before walking the heap.
10 */
11 class Driver {
12         static void AllocStuff ()
13         {
14                 var x = new object ();
15                 for (int i = 0; i < 300; ++i)
16                         x = new byte [i];
17         }
18
19         static void BackgroundNoise ()
20         {
21                 while (true)
22                         AllocStuff ();
23         }
24
25         static void Main () {
26                 var testTimeout = new TestTimeout ();
27                 testTimeout.Start ();
28                 for (int i = 0; i < Math.Max (1, Environment.ProcessorCount / 2); ++i) {
29                 // for (int i = 0; i < 4; ++i) {
30                         var t = new Thread (BackgroundNoise);
31                         t.IsBackground = true;
32                         t.Start ();
33                 }
34                 
35                 const int TOTAL_ITERATIONS = 100;
36                 for (int i = 0; i < TOTAL_ITERATIONS; ++i) {
37                         var ad = AppDomain.CreateDomain ("domain_" + i);
38                         ad.DoCallBack (new CrossAppDomainDelegate (AllocStuff));
39                         AppDomain.Unload (ad);
40
41                         Console.Write (".");
42                         if (i > 0 && i % 20 == 0) Console.WriteLine ();
43
44                         if (!testTimeout.HaveTimeLeft ()) {
45                                 var finishTime = DateTime.UtcNow;
46                                 var ranFor = finishTime - testTimeout.StartTime;
47                                 Console.WriteLine ("Will run out of time soon. ran for {0}, finished {1}/{2} iterations", ranFor, i+1, TOTAL_ITERATIONS);
48                         }
49                 }
50                 Console.WriteLine ("\ndone");
51         }
52 }