**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / Test / System.Runtime.Remoting / SynchronizationAttributeTest.cs
blobfd454b19bbeb6aa41a8aefd83a2172d5abcef189
1 //
2 // MonoTests.System.Runtime.Remoting.SynchronizationAttributeTest.cs
3 //
4 // Author: Lluis Sanchez Gual (lluis@ximian.com)
5 //
6 // 2003 (C) Copyright, Novell, Inc.
7 //
9 using System;
10 using System.Threading;
11 using System.Runtime.Remoting.Contexts;
12 using NUnit.Framework;
14 namespace MonoTests.System.Runtime.Remoting
16 enum SynchRes { SameSync, NewSync, NoSync }
18 class SincroBase: ContextBoundObject
20 public int idx = 0;
22 public bool CheckConcurrency ()
24 int t = idx;
25 for (int n=0; n<40; n++)
27 idx++;
28 Thread.Sleep (25);
30 return (t+40 != idx);
33 public bool CheckUnlockedConcurrency ()
35 Lock (false);
36 return CheckConcurrency ();
39 public SynchRes CheckContext (Context ctx)
41 object otherp = ctx.GetProperty ("Synchronization");
42 object thisp = Thread.CurrentContext.GetProperty ("Synchronization");
44 if (thisp == null) return SynchRes.NoSync;
45 if (thisp == otherp) return SynchRes.SameSync;
46 return SynchRes.NewSync;
49 public SynchRes CheckContextTransition (Type type)
51 SincroBase bob = (SincroBase)Activator.CreateInstance (type);
52 return bob.CheckContext (Thread.CurrentContext);
55 public bool CheckCalloutConcurrency (SincroBase bob)
57 bool res = bob.CheckConcurrency ();
58 return res;
61 public void CheckLock1 ()
63 Thread.Sleep (2000);
64 Lock (false);
65 Thread.Sleep (6000);
68 public void CheckLock2 ()
70 Thread.Sleep (1000);
71 Lock (true);
72 Thread.Sleep (2000);
75 public void Lock (bool b)
77 SynchronizationAttribute thisp = (SynchronizationAttribute) Thread.CurrentContext.GetProperty ("Synchronization");
78 thisp.Locked = b;
81 public bool GetLocked ()
83 SynchronizationAttribute thisp = (SynchronizationAttribute) Thread.CurrentContext.GetProperty ("Synchronization");
84 return thisp.Locked;
87 public bool CheckMonitorWait (bool exitContext)
89 lock (this)
91 return Monitor.Wait (this, 1000, exitContext);
95 public void CheckMonitorPulse ()
97 lock (this)
99 Monitor.Pulse (this);
104 [Synchronization (SynchronizationAttribute.SUPPORTED)]
105 class SincroSupported: SincroBase
109 [Synchronization (SynchronizationAttribute.REQUIRED)]
110 class SincroRequired: SincroBase
114 [Synchronization (SynchronizationAttribute.REQUIRES_NEW)]
115 class SincroRequiresNew: SincroBase
117 public bool TestCallback ()
119 SincroNotSupported bob = new SincroNotSupported ();
120 return bob.CallBack (this);
124 [Synchronization (SynchronizationAttribute.NOT_SUPPORTED)]
125 class SincroNotSupported: SincroBase
127 public bool CallBack (SincroRequiresNew bob)
129 return bob.CheckConcurrency ();
133 [Synchronization (SynchronizationAttribute.REQUIRES_NEW, true)]
134 class SincroRequiresNewReentrant: SincroBase
138 [TestFixture]
139 public class SynchronizationAttributeTest: Assertion
141 SincroRequiresNew sincob = new SincroRequiresNew ();
142 SincroNotSupported notsup = new SincroNotSupported ();
143 SincroRequiresNewReentrant reentrant = new SincroRequiresNewReentrant ();
144 SincroRequiresNew notreentrant = new SincroRequiresNew ();
145 bool otResult;
147 [Test]
148 public void TestSynchronization ()
150 Thread tr = new Thread (new ThreadStart (FirstSyncThread));
151 tr.Start ();
152 Thread.Sleep (200);
153 SecondSyncThread ();
155 tr.Join ();
156 Assert ("Concurrency detected in FirstSyncThread", !otResult);
159 void FirstSyncThread ()
161 otResult = sincob.CheckConcurrency ();
164 void SecondSyncThread ()
166 bool concurrent = sincob.CheckConcurrency ();
167 Assert ("Concurrency detected", !concurrent);
170 [Test]
171 public void TestSupported ()
173 SincroRequiresNew ob = new SincroRequiresNew ();
174 SynchRes res = ob.CheckContextTransition (typeof(SincroSupported));
175 Assert ("Synchronizaton context expected", res == SynchRes.SameSync);
177 SincroSupported ob2 = new SincroSupported ();
178 res = ob2.CheckContext (Thread.CurrentContext);
179 Assert ("Synchronizaton context not expected", res == SynchRes.NoSync);
182 [Test]
183 public void TestRequired ()
185 SincroRequiresNew ob = new SincroRequiresNew ();
186 SynchRes res = ob.CheckContextTransition (typeof(SincroRequired));
187 Assert ("Synchronizaton context expected 1", res == SynchRes.SameSync);
189 SincroRequired ob2 = new SincroRequired ();
190 res = ob2.CheckContext (Thread.CurrentContext);
191 Assert ("Synchronizaton context expected 2", res == SynchRes.NewSync);
194 [Test]
195 public void TestRequiresNew ()
197 SincroRequiresNew ob = new SincroRequiresNew ();
198 SynchRes res = ob.CheckContextTransition (typeof(SincroRequiresNew));
199 Assert ("New synchronizaton context expected", res == SynchRes.NewSync);
201 SincroRequiresNew ob2 = new SincroRequiresNew ();
202 res = ob2.CheckContext (Thread.CurrentContext);
203 Assert ("Synchronizaton context not expected", res == SynchRes.NewSync);
206 [Test]
207 public void TestNotSupported ()
209 SincroRequiresNew ob = new SincroRequiresNew ();
210 SynchRes res = ob.CheckContextTransition (typeof(SincroNotSupported));
211 Assert ("Synchronizaton context not expected 1", res == SynchRes.NoSync);
213 SincroNotSupported ob2 = new SincroNotSupported ();
214 res = ob2.CheckContext (Thread.CurrentContext);
215 Assert ("Synchronizaton context not expected 2", res == SynchRes.NoSync);
218 [Test]
219 public void TestLocked1 ()
221 sincob.Lock (false);
222 Thread tr = new Thread (new ThreadStart (FirstSyncThread));
223 tr.Start ();
224 Thread.Sleep (200);
225 SecondSyncThread ();
227 tr.Join ();
228 Assert ("Concurrency detected in FirstSyncThread", !otResult);
231 [Test]
232 public void TestLocked2 ()
234 Thread tr = new Thread (new ThreadStart (FirstNotSyncThread));
235 tr.Start ();
236 Thread.Sleep (200);
237 SecondNotSyncThread ();
239 tr.Join ();
240 Assert ("Concurrency not detected in FirstReentryThread", otResult);
243 void FirstNotSyncThread ()
245 otResult = sincob.CheckUnlockedConcurrency ();
248 void SecondNotSyncThread ()
250 bool concurrent = sincob.CheckConcurrency ();
251 Assert ("Concurrency not detected", concurrent);
254 [Test]
255 public void TestLocked3 ()
257 Thread tr = new Thread (new ThreadStart (Lock1Thread));
258 tr.Start ();
259 Thread.Sleep (200);
260 Lock2Thread ();
263 void Lock1Thread ()
265 sincob.CheckLock1 ();
268 void Lock2Thread ()
270 sincob.CheckLock2 ();
273 [Test]
274 public void TestReentry ()
276 Thread tr = new Thread (new ThreadStart (FirstReentryThread));
277 tr.Start ();
278 Thread.Sleep (200);
279 SecondReentryThread ();
281 tr.Join ();
282 Assert ("Concurrency not detected in FirstReentryThread", otResult);
285 void FirstReentryThread ()
287 otResult = reentrant.CheckCalloutConcurrency (notsup);
290 void SecondReentryThread ()
292 bool concurrent = reentrant.CheckCalloutConcurrency (notsup);
293 Assert ("Concurrency not detected", concurrent);
296 [Test]
297 public void TestNoReentry ()
299 Thread tr = new Thread (new ThreadStart (FirstNoReentryThread));
300 tr.Start ();
301 Thread.Sleep (200);
302 SecondNoReentryThread ();
304 tr.Join ();
305 Assert ("Concurrency detected in FirstNoReentryThread", !otResult);
308 void FirstNoReentryThread ()
310 otResult = notreentrant.CheckCalloutConcurrency (notsup);
313 void SecondNoReentryThread ()
315 bool concurrent = notreentrant.CheckCalloutConcurrency (notsup);
316 Assert ("Concurrency detected", !concurrent);
319 [Test]
320 public void TestCallback ()
322 Thread tr = new Thread (new ThreadStart (CallbackThread));
323 tr.Start ();
324 Thread.Sleep (200);
325 bool concurrent = notreentrant.CheckConcurrency ();
326 Assert ("Concurrency detected", !concurrent);
327 notreentrant.CheckContext (Thread.CurrentContext);
329 tr.Join ();
330 Assert ("Concurrency detected in CallbackThread", !otResult);
333 void CallbackThread ()
335 otResult = notreentrant.TestCallback ();
338 [Test]
339 public void TestMonitorWait ()
341 Thread tr = new Thread (new ThreadStart (DoMonitorPulse));
342 tr.Start ();
344 bool r = sincob.CheckMonitorWait (true);
345 Assert ("Wait timeout", r);
347 r = tr.Join (1000);
348 Assert ("Join timeout", r);
350 tr = new Thread (new ThreadStart (DoMonitorPulse));
351 tr.Start ();
353 r = sincob.CheckMonitorWait (false);
354 Assert ("Expected wait timeout", !r);
356 r = tr.Join (1000);
357 Assert ("Join timeout 2", r);
360 void DoMonitorPulse ()
362 Thread.Sleep (100);
363 sincob.CheckMonitorPulse ();