2 // MonoTests.System.Runtime.Remoting.SynchronizationAttributeTest.cs
4 // Author: Lluis Sanchez Gual (lluis@ximian.com)
6 // 2003 (C) Copyright, Novell, Inc.
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
22 public bool CheckConcurrency ()
25 for (int n
=0; n
<40; n
++)
33 public bool CheckUnlockedConcurrency ()
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 ();
61 public void CheckLock1 ()
68 public void CheckLock2 ()
75 public void Lock (bool b
)
77 SynchronizationAttribute thisp
= (SynchronizationAttribute
) Thread
.CurrentContext
.GetProperty ("Synchronization");
81 public bool GetLocked ()
83 SynchronizationAttribute thisp
= (SynchronizationAttribute
) Thread
.CurrentContext
.GetProperty ("Synchronization");
87 public bool CheckMonitorWait (bool exitContext
)
91 return Monitor
.Wait (this, 1000, exitContext
);
95 public void CheckMonitorPulse ()
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
139 public class SynchronizationAttributeTest
141 SincroRequiresNew sincob
= new SincroRequiresNew ();
142 SincroNotSupported notsup
= new SincroNotSupported ();
143 SincroRequiresNewReentrant reentrant
= new SincroRequiresNewReentrant ();
144 SincroRequiresNew notreentrant
= new SincroRequiresNew ();
148 public void TestSynchronization ()
150 Thread tr
= new Thread (new ThreadStart (FirstSyncThread
));
156 Assert
.IsTrue (!otResult
, "Concurrency detected in FirstSyncThread");
159 void FirstSyncThread ()
161 otResult
= sincob
.CheckConcurrency ();
164 void SecondSyncThread ()
166 bool concurrent
= sincob
.CheckConcurrency ();
167 Assert
.IsTrue (!concurrent
, "Concurrency detected");
171 public void TestSupported ()
173 SincroRequiresNew ob
= new SincroRequiresNew ();
174 SynchRes res
= ob
.CheckContextTransition (typeof(SincroSupported
));
175 Assert
.IsTrue (res
== SynchRes
.SameSync
, "Synchronizaton context expected");
177 SincroSupported ob2
= new SincroSupported ();
178 res
= ob2
.CheckContext (Thread
.CurrentContext
);
179 Assert
.IsTrue (res
== SynchRes
.NoSync
, "Synchronizaton context not expected");
183 public void TestRequired ()
185 SincroRequiresNew ob
= new SincroRequiresNew ();
186 SynchRes res
= ob
.CheckContextTransition (typeof(SincroRequired
));
187 Assert
.IsTrue (res
== SynchRes
.SameSync
, "Synchronizaton context expected 1");
189 SincroRequired ob2
= new SincroRequired ();
190 res
= ob2
.CheckContext (Thread
.CurrentContext
);
191 Assert
.IsTrue (res
== SynchRes
.NewSync
, "Synchronizaton context expected 2");
195 public void TestRequiresNew ()
197 SincroRequiresNew ob
= new SincroRequiresNew ();
198 SynchRes res
= ob
.CheckContextTransition (typeof(SincroRequiresNew
));
199 Assert
.IsTrue (res
== SynchRes
.NewSync
, "New synchronizaton context expected");
201 SincroRequiresNew ob2
= new SincroRequiresNew ();
202 res
= ob2
.CheckContext (Thread
.CurrentContext
);
203 Assert
.IsTrue (res
== SynchRes
.NewSync
, "Synchronizaton context not expected");
207 public void TestNotSupported ()
209 SincroRequiresNew ob
= new SincroRequiresNew ();
210 SynchRes res
= ob
.CheckContextTransition (typeof(SincroNotSupported
));
211 Assert
.IsTrue (res
== SynchRes
.NoSync
, "Synchronizaton context not expected 1");
213 SincroNotSupported ob2
= new SincroNotSupported ();
214 res
= ob2
.CheckContext (Thread
.CurrentContext
);
215 Assert
.IsTrue (res
== SynchRes
.NoSync
, "Synchronizaton context not expected 2");
219 public void TestLocked1 ()
222 Thread tr
= new Thread (new ThreadStart (FirstSyncThread
));
228 Assert
.IsTrue (!otResult
, "Concurrency detected in FirstSyncThread");
232 public void TestLocked2 ()
234 Thread tr
= new Thread (new ThreadStart (FirstNotSyncThread
));
237 SecondNotSyncThread ();
240 Assert
.IsTrue (otResult
, "Concurrency not detected in FirstReentryThread");
243 void FirstNotSyncThread ()
245 otResult
= sincob
.CheckUnlockedConcurrency ();
248 void SecondNotSyncThread ()
250 bool concurrent
= sincob
.CheckConcurrency ();
251 Assert
.IsTrue (concurrent
, "Concurrency not detected");
255 public void TestLocked3 ()
257 Thread tr
= new Thread (new ThreadStart (Lock1Thread
));
265 sincob
.CheckLock1 ();
270 sincob
.CheckLock2 ();
274 public void TestReentry ()
276 Thread tr
= new Thread (new ThreadStart (FirstReentryThread
));
279 SecondReentryThread ();
282 Assert
.IsTrue (otResult
, "Concurrency not detected in FirstReentryThread");
285 void FirstReentryThread ()
287 otResult
= reentrant
.CheckCalloutConcurrency (notsup
);
290 void SecondReentryThread ()
292 bool concurrent
= reentrant
.CheckCalloutConcurrency (notsup
);
293 Assert
.IsTrue (concurrent
, "Concurrency not detected");
297 public void TestNoReentry ()
299 Thread tr
= new Thread (new ThreadStart (FirstNoReentryThread
));
302 SecondNoReentryThread ();
305 Assert
.IsTrue (!otResult
, "Concurrency detected in FirstNoReentryThread");
308 void FirstNoReentryThread ()
310 otResult
= notreentrant
.CheckCalloutConcurrency (notsup
);
313 void SecondNoReentryThread ()
315 bool concurrent
= notreentrant
.CheckCalloutConcurrency (notsup
);
316 Assert
.IsTrue (!concurrent
, "Concurrency detected");
320 public void TestCallback ()
322 Thread tr
= new Thread (new ThreadStart (CallbackThread
));
325 bool concurrent
= notreentrant
.CheckConcurrency ();
326 Assert
.IsTrue (!concurrent
, "Concurrency detected");
327 notreentrant
.CheckContext (Thread
.CurrentContext
);
330 Assert
.IsTrue (!otResult
, "Concurrency detected in CallbackThread");
333 void CallbackThread ()
335 otResult
= notreentrant
.TestCallback ();
339 [Category("NotDotNet")]
340 public void TestMonitorWait ()
342 Thread tr
= new Thread (new ThreadStart (DoMonitorPulse
));
345 bool r
= sincob
.CheckMonitorWait (true);
346 Assert
.IsTrue (r
, "Wait timeout");
349 Assert
.IsTrue (r
, "Join timeout");
351 tr
= new Thread (new ThreadStart (DoMonitorPulse
));
354 r
= sincob
.CheckMonitorWait (false);
355 Assert
.IsTrue (!r
, "Expected wait timeout");
358 Assert
.IsTrue (r
, "Join timeout 2");
361 void DoMonitorPulse ()
364 sincob
.CheckMonitorPulse ();