SemaphoreSlimTests.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. public void WaitStressTest()
  61. {
  62. int count = -1;
  63. bool[] array = new bool[7];
  64. int worker = 0;
  65. bool coherent = true;
  66. ParallelTestHelper.ParallelStressTest (sem, delegate (SemaphoreSlim s) {
  67. int index = Interlocked.Increment (ref count);
  68. s.Wait ();
  69. if (Interlocked.Increment (ref worker) > 5)
  70. coherent = false;
  71. Thread.Sleep (40);
  72. Interlocked.Decrement (ref worker);
  73. s.Release ();
  74. array[index] = true;
  75. }, 7);
  76. bool result = array.Aggregate ((acc, e) => acc && e);
  77. Assert.IsTrue (result, "#1");
  78. Assert.AreEqual (5, sem.CurrentCount, "#2");
  79. Assert.IsTrue (coherent, "#3");
  80. }
  81. }
  82. }