SpinLockTests.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #if NET_4_0
  2. //
  3. // SpinLockTests.cs
  4. //
  5. // Author:
  6. // Jérémie "Garuma" Laval <[email protected]>
  7. //
  8. // Copyright (c) 2010 Jérémie "Garuma" Laval
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy
  11. // of this software and associated documentation files (the "Software"), to deal
  12. // in the Software without restriction, including without limitation the rights
  13. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. // copies of the Software, and to permit persons to whom the Software is
  15. // furnished to do so, subject to the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be included in
  18. // all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. // THE SOFTWARE.
  27. using System;
  28. using System.Threading;
  29. using NUnit.Framework;
  30. using MonoTests.System.Threading.Tasks;
  31. namespace MonoTests.System.Threading
  32. {
  33. [TestFixture]
  34. public class SpinLockTests
  35. {
  36. SpinLock sl;
  37. [SetUp]
  38. public void Setup ()
  39. {
  40. sl = new SpinLock (true);
  41. }
  42. [Test, ExpectedException (typeof (LockRecursionException))]
  43. public void RecursionExceptionTest ()
  44. {
  45. sl = new SpinLock (true);
  46. bool taken = false, taken2 = false;
  47. sl.Enter (ref taken);
  48. Assert.IsTrue (taken, "#1");
  49. sl.Enter (ref taken2);
  50. }
  51. [Test]
  52. public void SimpleEnterExitSchemeTest ()
  53. {
  54. bool taken = false;
  55. for (int i = 0; i < 50000; i++) {
  56. sl.Enter (ref taken);
  57. Assert.IsTrue (taken, "#" + i.ToString ());
  58. sl.Exit ();
  59. taken = false;
  60. }
  61. }
  62. [Test]
  63. public void SemanticCorrectnessTest ()
  64. {
  65. sl = new SpinLock (false);
  66. bool taken = false;
  67. bool taken2 = false;
  68. sl.Enter (ref taken);
  69. Assert.IsTrue (taken, "#1");
  70. sl.TryEnter (ref taken2);
  71. Assert.IsFalse (taken2, "#2");
  72. sl.Exit ();
  73. sl.TryEnter (ref taken2);
  74. Assert.IsTrue (taken2, "#3");
  75. sl.Exit ();
  76. Assert.IsFalse (sl.IsHeld);
  77. }
  78. [Test, ExpectedException (typeof (ArgumentException))]
  79. public void FirstTakenParameterTest ()
  80. {
  81. bool taken = true;
  82. sl.Enter (ref taken);
  83. }
  84. [Test, ExpectedException (typeof (ArgumentException))]
  85. public void SecondTakenParameterTest ()
  86. {
  87. bool taken = true;
  88. sl.TryEnter (ref taken);
  89. }
  90. internal class SpinLockWrapper
  91. {
  92. public SpinLock Lock = new SpinLock (false);
  93. }
  94. [Test]
  95. public void LockUnicityTest ()
  96. {
  97. ParallelTestHelper.Repeat (delegate {
  98. int currentCount = 0;
  99. bool fail = false;
  100. SpinLockWrapper wrapper = new SpinLockWrapper ();
  101. ParallelTestHelper.ParallelStressTest (wrapper, delegate {
  102. bool taken = false;
  103. wrapper.Lock.Enter (ref taken);
  104. int current = currentCount++;
  105. if (current != 0)
  106. fail = true;
  107. SpinWait sw = new SpinWait ();
  108. for (int i = 0; i < 200; i++)
  109. sw.SpinOnce ();
  110. currentCount -= 1;
  111. wrapper.Lock.Exit ();
  112. }, 4);
  113. Assert.IsFalse (fail);
  114. }, 200);
  115. }
  116. [Test]
  117. public void IsHeldByCurrentThreadTest ()
  118. {
  119. bool lockTaken = false;
  120. sl.Enter (ref lockTaken);
  121. Assert.IsTrue (lockTaken, "#1");
  122. Assert.IsTrue (sl.IsHeldByCurrentThread, "#2");
  123. lockTaken = false;
  124. sl = new SpinLock (true);
  125. sl.Enter (ref lockTaken);
  126. Assert.IsTrue (lockTaken, "#3");
  127. Assert.IsTrue (sl.IsHeldByCurrentThread, "#4");
  128. }
  129. }
  130. }
  131. #endif