SemaphoreSlimTests.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // SemaphoreSlimTests.cs
  2. //
  3. // Copyright (c) 2008 Jérémie "Garuma" Laval
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. //
  24. using System;
  25. using System.Linq;
  26. using System.Threading;
  27. using MonoTests.System.Threading.Tasks;
  28. using NUnit.Framework;
  29. namespace MonoTests.System.Threading
  30. {
  31. [TestFixture]
  32. public class SemaphoreSlimTests
  33. {
  34. SemaphoreSlim sem;
  35. [SetUp]
  36. public void Setup()
  37. {
  38. sem = new SemaphoreSlim(5);
  39. }
  40. [Test]
  41. public void CurrentCountMaxTestCase()
  42. {
  43. using (var semMax = new SemaphoreSlim(5, 5)) {
  44. semMax.Wait();
  45. try {
  46. semMax.Release(3);
  47. Assert.Fail ();
  48. } catch (SemaphoreFullException) {}
  49. }
  50. }
  51. [Test]
  52. public void CurrentCountTestCase()
  53. {
  54. sem.Wait();
  55. sem.Wait();
  56. sem.Release();
  57. Assert.AreEqual(4, sem.CurrentCount);
  58. }
  59. [Test]
  60. [Category ("MultiThreaded")]
  61. public void WaitStressTest()
  62. {
  63. int count = -1;
  64. bool[] array = new bool[7];
  65. int worker = 0;
  66. bool coherent = true;
  67. ParallelTestHelper.ParallelStressTest (sem, delegate (SemaphoreSlim s) {
  68. int index = Interlocked.Increment (ref count);
  69. s.Wait ();
  70. if (Interlocked.Increment (ref worker) > 5)
  71. coherent = false;
  72. Thread.Sleep (40);
  73. Interlocked.Decrement (ref worker);
  74. s.Release ();
  75. array[index] = true;
  76. }, 7);
  77. bool result = array.Aggregate ((acc, e) => acc && e);
  78. Assert.IsTrue (result, "#1");
  79. Assert.AreEqual (5, sem.CurrentCount, "#2");
  80. Assert.IsTrue (coherent, "#3");
  81. }
  82. }
  83. }