/
unhandled-exception-test-case.2.cs
226 lines (210 loc) · 7.68 KB
/
unhandled-exception-test-case.2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using System;
using System.Reflection;
using System.Threading;
namespace UnhandledExceptionTest
{
class TestConfiguration {
private bool useDifferentThread = false;
public bool DT {
get {
return useDifferentThread;
}
}
private bool useDifferentApplicationDomain = false;
public bool DA {
get {
return useDifferentApplicationDomain;
}
}
private bool differentConfigurationIsLegacy;
public bool DCIL {
get {
return differentConfigurationIsLegacy;
}
}
private bool useDifferentThreadInDifferentApplicationDomain = false;
public bool DTDA {
get {
return useDifferentThreadInDifferentApplicationDomain;
}
}
private bool addHandlerToRootApplicationDomain = false;
public bool HRA {
get {
return addHandlerToRootApplicationDomain;
}
}
private bool addHandlerToDifferentApplicationDomain = false;
public bool HDA {
get {
return addHandlerToDifferentApplicationDomain;
}
}
private static bool ParseArgumentValue (string value) {
if ((value.Length == 1)) {
switch (value [0]) {
case 'T':
return true;
case 'F':
return false;
default:
Console.WriteLine ("Invalid argument value {0}", value);
throw new ApplicationException ("Invalid argument value " + value);
}
} else {
Console.WriteLine ("Invalid argument value {0}", value);
throw new ApplicationException ("Invalid argument value " + value);
}
}
public TestConfiguration (string configuration) {
string [] arguments = configuration.Split (',');
foreach (string argument in arguments) {
string [] components = argument.Split (':');
if (components.Length == 2) {
switch (components [0]) {
case "DT":
useDifferentThread = ParseArgumentValue (components [1]);
break;
case "DA":
useDifferentApplicationDomain = ParseArgumentValue (components [1]);
break;
case "DCIL":
differentConfigurationIsLegacy = ParseArgumentValue (components [1]);
break;
case "DTDA":
useDifferentThreadInDifferentApplicationDomain = ParseArgumentValue (components [1]);
break;
case "HRA":
addHandlerToRootApplicationDomain = ParseArgumentValue (components [1]);
break;
case "HDA":
addHandlerToDifferentApplicationDomain = ParseArgumentValue (components [1]);
break;
default:
Console.WriteLine ("Invalid argument {0}", argument);
throw new ApplicationException ("Invalid argument " + argument);
}
} else {
Console.WriteLine ("Invalid argument {0}", argument);
throw new ApplicationException ("Invalid argument " + argument);
}
}
}
private static string BoolToString (bool value) {
return value ? "T" : "F";
}
public void Print () {
Console.WriteLine ("Configuration: DT={0},DA={1},DCIL={2},DTDA={3},HRA={4},HDA={5}",
BoolToString (useDifferentThread),
BoolToString (useDifferentApplicationDomain),
BoolToString (differentConfigurationIsLegacy),
BoolToString (useDifferentThreadInDifferentApplicationDomain),
BoolToString (addHandlerToRootApplicationDomain),
BoolToString (addHandlerToDifferentApplicationDomain));
}
}
class Test {
private string configurationDescription;
private TestConfiguration configuration;
public Test (string configurationDescription) {
this.configurationDescription = configurationDescription;
this.configuration = new TestConfiguration (configurationDescription);
}
private AppDomain CreateDiffrentAppDomain () {
AppDomainSetup ads = new AppDomainSetup();
ads.ApplicationBase = System.Environment.CurrentDirectory;
ads.DisallowBindingRedirects = false;
ads.DisallowCodeDownload = true;
ads.ConfigurationFile = System.Environment.CurrentDirectory + System.IO.Path.DirectorySeparatorChar +
(configuration.DCIL ? "unhandled-exception-legacy-configuration.config" : "unhandled-exception-base-configuration.config");
return AppDomain.CreateDomain("DifferentAppDomain", null, ads);
}
public void RunTest () {
if (configuration.DA) {
AppDomain differentAppDomain = CreateDiffrentAppDomain ();
if (configuration.HDA) {
differentAppDomain.UnhandledException += new UnhandledExceptionEventHandler (DifferentDomainUnhandledExceptionHandler);
}
DifferentDomainActor dda = (DifferentDomainActor) differentAppDomain.CreateInstanceAndUnwrap (
Assembly.GetEntryAssembly().FullName, typeof (DifferentDomainActor).FullName);
dda.Act (configurationDescription);
} else {
if (configuration.DT) {
Console.WriteLine ("Throwing ApplicationException in different thread");
} else {
Console.WriteLine ("Throwing ApplicationException in main thread");
}
throw new ApplicationException ("This exception is unhandled");
}
if (configuration.DT) {
Console.WriteLine ("Continuing in different thread after the exception was thrown");
}
}
static void Main (string [] args) {
if (args.Length != 1) {
Console.WriteLine ("Invalid arguments (number of) {0}", args.Length);
throw new ApplicationException ("Invalid arguments (number of) " + args.Length);
}
Test test = new Test (args [0]);
test.Act ();
}
public void Act () {
configuration.Print ();
Console.WriteLine ("Running under version {0}", Environment.Version);
if (configuration.HRA) {
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler (RootDomainUnhandledExceptionHandler);
}
if (configuration.DT) {
Thread thread = new Thread (new ThreadStart (this.RunTest));
thread.Start ();
thread.Join ();
} else {
RunTest ();
}
Console.WriteLine ("Continuing in main thread after the exception was thrown");
Console.WriteLine ("Continuing in root AppDomain after the exception was thrown");
Console.WriteLine ("MARKER-CONT");
}
public static void PrintUnhandledException (string caller, object sender, UnhandledExceptionEventArgs e) {
Exception ex = (Exception)e.ExceptionObject;
Console.WriteLine ("Running {0}", caller);
Console.WriteLine ("Handling exception type {0}", ex.GetType().Name);
Console.WriteLine ("Message is {0}", ex.Message);
Console.WriteLine ("IsTerminating is set to {0}", e.IsTerminating);
}
public static void RootDomainUnhandledExceptionHandler (object sender, UnhandledExceptionEventArgs e) {
Console.WriteLine ("MARKER-RDUE");
PrintUnhandledException ("RootDomainUnhandledExceptionHandler", sender, e);
}
public static void DifferentDomainUnhandledExceptionHandler (object sender, UnhandledExceptionEventArgs e) {
Console.WriteLine ("MARKER-DDUE");
PrintUnhandledException ("DifferentDomainUnhandledExceptionHandler", sender, e);
}
}
public class DifferentDomainActor : MarshalByRefObject {
//private string configurationDescription = null;
private TestConfiguration configuration = null;
public void RunTest () {
if (configuration.DTDA) {
Console.WriteLine ("Throwing ApplicationException in new thread (different appdomain)");
} else if (configuration.DT) {
Console.WriteLine ("Throwing ApplicationException in different thread (different appdomain)");
} else {
Console.WriteLine ("Throwing ApplicationException in main thread (different appdomain)");
}
throw new ApplicationException ("This exception is unhandled");
}
// Call this method via a proxy.
public void Act (string configurationDescription) {
//this.configurationDescription = configurationDescription;
this.configuration = new TestConfiguration (configurationDescription);
if (configuration.DTDA) {
Thread thread = new Thread (new ThreadStart (this.RunTest));
thread.Start ();
thread.Join ();
} else {
RunTest ();
}
}
}
}