| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- //
- // MonitorTest.cs - NUnit test cases for System.Threading.Monitor
- //
- // Authors:
- // Gonzalo Paniagua Javier ([email protected])
- // Sebastien Pouliot ([email protected])
- //
- // Copyright (C) 2005, 2009 Novell, Inc (http://www.novell.com)
- //
- using NUnit.Framework;
- using System;
- using System.Threading;
- namespace MonoTests.System.Threading {
- [TestFixture]
- public class MonitorTest {
- TimeSpan Infinite = new TimeSpan (-10000); // -10000 ticks == -1 ms
- TimeSpan SmallNegative = new TimeSpan (-2); // between 0 and -1.0 (infinite) ms
- TimeSpan Negative = new TimeSpan (-20000); // really negative
- TimeSpan MaxValue = TimeSpan.FromMilliseconds ((long) Int32.MaxValue);
- TimeSpan TooLarge = TimeSpan.FromMilliseconds ((long) Int32.MaxValue + 1);
- [Test]
- [ExpectedException (typeof (SynchronizationLockException))]
- [Category ("NotWorking")] // test fails under MS FX 2.0 - maybe that worked on 1.x ?
- public void ExitNoEnter ()
- {
- object o = new object ();
- Monitor.Exit (o);
- }
- [Test]
- [ExpectedException (typeof (SynchronizationLockException))]
- [Category ("NotWorking")] // test fails under MS FX 2.0 - maybe that worked on 1.x ?
- public void OneEnterSeveralExits ()
- {
- object o = new object ();
- Monitor.Enter (o);
- Monitor.Exit (o);
- // fails here
- Monitor.Exit (o);
- Monitor.Exit (o);
- Monitor.Exit (o);
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void Enter_Null ()
- {
- Monitor.Enter (null);
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void Exit_Null ()
- {
- Monitor.Exit (null);
- }
- [Test]
- public void Enter_Exit ()
- {
- object o = new object ();
- Monitor.Enter (o);
- try {
- Assert.IsNotNull (o);
- }
- finally {
- Monitor.Exit (o);
- }
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void Pulse_Null ()
- {
- Monitor.Pulse (null);
- }
- [Test]
- [ExpectedException (typeof (SynchronizationLockException))]
- public void Pulse_Unlocked ()
- {
- object o = new object ();
- Monitor.Pulse (o);
- }
- [Test]
- public void Pulse ()
- {
- object o = new object ();
- lock (o) {
- Monitor.Pulse (o);
- }
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void PulseAll_Null ()
- {
- Monitor.PulseAll (null);
- }
- [Test]
- [ExpectedException (typeof (SynchronizationLockException))]
- public void PulseAll_Unlocked ()
- {
- object o = new object ();
- Monitor.PulseAll (o);
- }
- [Test]
- public void PulseAll ()
- {
- object o = new object ();
- lock (o) {
- Monitor.PulseAll (o);
- }
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void TryEnter_Null ()
- {
- Monitor.TryEnter (null);
- }
- [Test]
- public void TryEnter ()
- {
- object o = new object ();
- Assert.IsTrue (Monitor.TryEnter (o), "TryEnter");
- Assert.IsTrue (Monitor.TryEnter (o), "TryEnter-2");
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void TryEnter_Null_Int ()
- {
- Monitor.TryEnter (null, Timeout.Infinite);
- }
- [Test]
- [ExpectedException (typeof (ArgumentOutOfRangeException))]
- public void TryEnter_Int_Negative ()
- {
- object o = new object ();
- Monitor.TryEnter (o, -2);
- }
- [Test]
- public void TryEnter_Int ()
- {
- object o = new object ();
- Assert.IsTrue (Monitor.TryEnter (o, 1), "TryEnter");
- Assert.IsTrue (Monitor.TryEnter (o, 2), "TryEnter-2");
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void TryEnter_Null_TimeSpan ()
- {
- Monitor.TryEnter (null, Timeout.Infinite);
- }
- [Test]
- [ExpectedException (typeof (ArgumentOutOfRangeException))]
- public void TryEnter_TimeSpan_Negative ()
- {
- object o = new object ();
- Monitor.TryEnter (o, Negative);
- }
- [Test]
- [ExpectedException (typeof (ArgumentOutOfRangeException))]
- public void TryEnter_TimeSpan_TooLarge ()
- {
- object o = new object ();
- Monitor.TryEnter (o, TooLarge);
- }
- [Test]
- [ExpectedException (typeof (ArgumentOutOfRangeException))]
- public void TryEnter_Null_TimeSpan_TooLarge ()
- {
- // exception ordering test
- Monitor.TryEnter (null, TooLarge);
- }
- [Test]
- public void TryEnter_TimeSpan ()
- {
- object o = new object ();
- Assert.IsTrue (Monitor.TryEnter (o, Infinite), "TryEnter");
- Assert.IsTrue (Monitor.TryEnter (o, SmallNegative), "TryEnter-2");
- Assert.IsTrue (Monitor.TryEnter (o, MaxValue), "TryEnter-3");
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void Wait_Null ()
- {
- Monitor.Wait (null);
- }
- [Test]
- [ExpectedException (typeof (SynchronizationLockException))]
- public void Wait_Unlocked ()
- {
- object o = new object ();
- Assert.IsTrue (Monitor.Wait (o), "Wait");
- }
- // [Test] that would be Infinite
- public void Wait ()
- {
- object o = new object ();
- lock (o) {
- Assert.IsFalse (Monitor.Wait (o), "Wait");
- }
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void Wait_Null_Int ()
- {
- Monitor.Wait (null, Timeout.Infinite);
- }
- [Test]
- [ExpectedException (typeof (ArgumentOutOfRangeException))]
- public void Wait_Int_Negative ()
- {
- object o = new object ();
- Monitor.Wait (o, -2);
- }
- [Test]
- [ExpectedException (typeof (SynchronizationLockException))]
- public void Wait_Int_Unlocked ()
- {
- object o = new object ();
- Assert.IsTrue (Monitor.Wait (o, 1), "Wait");
- }
- [Test]
- public void Wait_Int ()
- {
- object o = new object ();
- lock (o) {
- Assert.IsFalse (Monitor.Wait (o, 1), "Wait");
- }
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void Wait_Null_TimeSpan ()
- {
- Monitor.Wait (null, Timeout.Infinite);
- }
- [Test]
- [ExpectedException (typeof (ArgumentOutOfRangeException))]
- public void Wait_TimeSpan_Negative ()
- {
- object o = new object ();
- Monitor.Wait (o, Negative);
- }
- [Test]
- [ExpectedException (typeof (ArgumentOutOfRangeException))]
- public void Wait_TimeSpan_TooLarge ()
- {
- object o = new object ();
- Monitor.Wait (o, TooLarge);
- }
- [Test]
- [ExpectedException (typeof (ArgumentOutOfRangeException))]
- public void Wait_Null_TimeSpan_TooLarge ()
- {
- // exception ordering test
- Monitor.Wait (null, TooLarge);
- }
- [Test]
- [ExpectedException (typeof (SynchronizationLockException))]
- public void Wait_TimeSpan_Unlocked ()
- {
- object o = new object ();
- Assert.IsTrue (Monitor.Wait (o, Infinite), "Wait");
- }
- [Test]
- public void Wait_TimeSpan ()
- {
- object o = new object ();
- lock (o) {
- Assert.IsFalse (Monitor.Wait (o, SmallNegative), "Wait");
- }
- }
- [Test]
- public void Enter_bool ()
- {
- object o = new object ();
- bool taken = false;
- Monitor.Enter (o, ref taken);
- Assert.IsTrue (taken, "Monitor.Enter (obj, ref taken)");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void Enter_bool_argcheck ()
- {
- object o = new object ();
- bool taken = true;
- Monitor.Enter (o, ref taken);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void Enter_bool_argcheck_fastpath ()
- {
- object o = new object ();
- bool taken = false;
- Monitor.Enter (o, ref taken);
- taken = true;
- Monitor.Enter (o, ref taken);
- }
- }
- }
|