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
|
//
// SpinLockTests.cs
//
// Author:
// Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
//
// Copyright (c) 2010 Jérémie "Garuma" Laval
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Threading;
using NUnit.Framework;
using MonoTests.System.Threading.Tasks;
namespace MonoTests.System.Threading
{
[TestFixture]
public class SpinLockTests
{
SpinLock sl;
[SetUp]
public void Setup ()
{
sl = new SpinLock (true);
}
[Test, ExpectedException (typeof (LockRecursionException))]
public void RecursionExceptionTest ()
{
sl = new SpinLock (true);
bool taken = false, taken2 = false;
sl.Enter (ref taken);
Assert.IsTrue (taken, "#1");
sl.Enter (ref taken2);
}
[Test]
public void SimpleEnterExitSchemeTest ()
{
bool taken = false;
for (int i = 0; i < 50000; i++) {
sl.Enter (ref taken);
Assert.IsTrue (taken, "#" + i.ToString ());
sl.Exit ();
taken = false;
}
}
[Test]
public void SemanticCorrectnessTest ()
{
sl = new SpinLock (false);
bool taken = false;
bool taken2 = false;
sl.Enter (ref taken);
Assert.IsTrue (taken, "#1");
sl.TryEnter (ref taken2);
Assert.IsFalse (taken2, "#2");
sl.Exit ();
sl.TryEnter (ref taken2);
Assert.IsTrue (taken2, "#3");
sl.Exit ();
Assert.IsFalse (sl.IsHeld);
}
[Test, ExpectedException (typeof (ArgumentException))]
public void FirstTakenParameterTest ()
{
bool taken = true;
sl.Enter (ref taken);
}
[Test, ExpectedException (typeof (ArgumentException))]
public void SecondTakenParameterTest ()
{
bool taken = true;
sl.TryEnter (ref taken);
}
internal class SpinLockWrapper
{
public SpinLock Lock = new SpinLock (false);
}
[Test]
[Category ("MultiThreaded")]
public void LockUnicityTest ()
{
ParallelTestHelper.Repeat (delegate {
int currentCount = 0;
bool fail = false;
SpinLockWrapper wrapper = new SpinLockWrapper ();
ParallelTestHelper.ParallelStressTest (wrapper, delegate {
bool taken = false;
wrapper.Lock.Enter (ref taken);
int current = currentCount++;
if (current != 0)
fail = true;
SpinWait sw = new SpinWait ();
for (int i = 0; i < 200; i++)
sw.SpinOnce ();
currentCount -= 1;
wrapper.Lock.Exit ();
}, 4);
Assert.IsFalse (fail);
}, 5);
}
[Test]
public void IsHeldByCurrentThreadTest ()
{
bool lockTaken = false;
sl.Enter (ref lockTaken);
Assert.IsTrue (lockTaken, "#1");
Assert.IsTrue (sl.IsHeldByCurrentThread, "#2");
lockTaken = false;
sl = new SpinLock (true);
sl.Enter (ref lockTaken);
Assert.IsTrue (lockTaken, "#3");
Assert.IsTrue (sl.IsHeldByCurrentThread, "#4");
}
}
}
|