SpinLockTests.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. }
  76. [Test, ExpectedException (typeof (ArgumentException))]
  77. public void FirstTakenParameterTest ()
  78. {
  79. bool taken = true;
  80. sl.Enter (ref taken);
  81. }
  82. [Test, ExpectedException (typeof (ArgumentException))]
  83. public void SecondTakenParameterTest ()
  84. {
  85. bool taken = true;
  86. sl.TryEnter (ref taken);
  87. }
  88. internal class SpinLockWrapper
  89. {
  90. public SpinLock Lock = new SpinLock (false);
  91. }
  92. [Test]
  93. public void LockUnicityTest ()
  94. {
  95. ParallelTestHelper.Repeat (delegate {
  96. int currentCount = 0;
  97. bool fail = false;
  98. SpinLockWrapper wrapper = new SpinLockWrapper ();
  99. ParallelTestHelper.ParallelStressTest (wrapper, delegate {
  100. bool taken = false;
  101. wrapper.Lock.Enter (ref taken);
  102. int current = currentCount++;
  103. if (current != 0)
  104. fail = true;
  105. SpinWait sw = new SpinWait ();
  106. for (int i = 0; i < 200; i++)
  107. sw.SpinOnce ();
  108. currentCount -= 1;
  109. wrapper.Lock.Exit ();
  110. }, 4);
  111. Assert.IsFalse (fail);
  112. }, 200);
  113. }
  114. [Test]
  115. public void IsHeldByCurrentThreadTest ()
  116. {
  117. bool lockTaken = false;
  118. sl.Enter (ref lockTaken);
  119. Assert.IsTrue (lockTaken, "#1");
  120. Assert.IsTrue (sl.IsHeldByCurrentThread, "#2");
  121. lockTaken = false;
  122. sl = new SpinLock (true);
  123. sl.Enter (ref lockTaken);
  124. Assert.IsTrue (lockTaken, "#3");
  125. Assert.IsTrue (sl.IsHeldByCurrentThread, "#4");
  126. }
  127. }
  128. }
  129. #endif