| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- // MutexTest.cs - NUnit Test Cases for System.Threading.Mutex
- //
- // Eduardo Garcia Cebollero <[email protected]>
- //
- // (C) Eduardo Garcia Cebollero
- //
- using NUnit.Framework;
- using System;
- using System.Threading;
- namespace MonoTests.System.Threading
- {
- public class MutexTest : TestCase
- {
-
- //Auxiliary Classes (Future Threads)
- private class ConcClass
- {
- public int id;
- public Mutex mut;
- public ConcClass(int id,Mutex mut)
- {
- this.id = id;
- this.mut = mut;
- }
- public void wait()
- {
- mut.WaitOne();
- }
- public void signal()
- {
- mut.ReleaseMutex();
- }
- }
- private class ConcClassLoop: ConcClass
- {
- public int marker;
- public ConcClassLoop(int id,Mutex mut) :
- base(id,mut)
- {
- this.marker = 0;
- }
-
- public void WithoutWait()
- {
- this.marker = this.id;
- this.signal();
- }
- public void Loop()
- {
- while (this.marker<100)
- {
- this.wait();
- this.marker++;
- this.signal();
- }
- }
- public void WaitAndForget()
- {
- this.wait();
- this.marker = id;
- }
- public void WaitAndWait()
- {
- mut.WaitOne();
- this.marker = this.id;
- mut.WaitOne();
- this.marker = this.id+1;
- }
- }
- protected override void SetUp() {}
- protected override void TearDown() {}
- public void TestCtor1()
- {
- try
- {
- Mutex Sem = new Mutex();
- }
- catch (Exception e)
- {
- Fail("#01 Error Creating The Simple Mutex:" + e.ToString());
- }
- }
- // These tests produce mutex release errors
- /*
- public void TestCtorDefaultValue()
- {
- Mutex Sem = new Mutex();
- ConcClassLoop class1 = new ConcClassLoop(1,Sem);
- Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
- thread1.Start();
- while(thread1.IsAlive);
- AssertEquals("#02 The default value of The mutex wrong set:",class1.id,class1.marker);
- }
- public void TestCtorCtor2()
- {
- Mutex Sem = new Mutex(false);
- ConcClassLoop class1 = new ConcClassLoop(1,Sem);
- Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
- thread1.Start();
- while(thread1.IsAlive);
- AssertEquals("#03 The value of The mutex wrong set:",class1.id,class1.marker);
- }
-
- public void TestCtorCtor3()
- {
- Mutex Sem = new Mutex(true);
- ConcClassLoop class1 = new ConcClassLoop(1,Sem);
- Thread thread1 = new Thread(new ThreadStart(class1.WithoutWait));
- thread1.Start();
- while(thread1.IsAlive);
- AssertEquals("#04 The default value of The mutex wrong set:",class1.id,class1.marker);
- }
- */
-
- // Hangs #72534
- [Category("NotWorking")]
- public void TestWaitAndSignal1()
- {
- Mutex Sem = new Mutex(false);
- ConcClassLoop class1 = new ConcClassLoop(1,Sem);
- Thread thread1 = new Thread(new ThreadStart(class1.Loop));
- try {
- thread1.Start();
- TestUtil.WaitForNotAlive (thread1, "");
- AssertEquals("#41 Mutex Worked InCorrecly:",100,class1.marker);
- }
- finally {
- thread1.Abort ();
- }
- }
- // Hangs
- [Category("NotWorking")]
- [Ignore ("It hangs and breaks the domain which runs nunit-console itself")]
- public void TestWaitAndFoget1()
- {
- Mutex Sem = new Mutex(false);
- ConcClassLoop class1 = new ConcClassLoop(1,Sem);
- ConcClassLoop class2 = new ConcClassLoop(2,Sem);
- Thread thread1 = new Thread(new ThreadStart(class1.WaitAndForget));
- Thread thread2 = new Thread(new ThreadStart(class2.WaitAndForget));
-
- try {
- thread1.Start();
- TestUtil.WaitForNotAlive (thread1, "t1");
-
- thread2.Start();
- TestUtil.WaitForNotAlive (thread2, "t2");
-
- AssertEquals("#51 The Mutex Has been Kept after end of the thread:", class2.id,class2.marker);
- }
- finally {
- thread1.Abort ();
- thread2.Abort ();
- }
- }
- public void TestHandle()
- {
- Mutex Sem = new Mutex();
- try
- {
- IntPtr Handle = Sem.Handle;
- }
- catch (Exception e)
- {
- Fail("#61 Unexpected Exception accessing Sem.Handle:" + e.ToString());
- }
- }
- }
- }
|