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
|
// MutexTest.cs - NUnit Test Cases for System.Threading.Mutex
//
// Eduardo Garcia Cebollero <kiwnix@yahoo.es>
//
// (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());
}
}
}
}
|