1
0

AsyncLockTest.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Reactive.Concurrency;
  6. using System.Reflection;
  7. using Xunit;
  8. namespace ReactiveTests.Tests
  9. {
  10. public class AsyncLockTest
  11. {
  12. [Fact]
  13. public void Wait_ArgumentChecking()
  14. {
  15. var asyncLock = new AsyncLock();
  16. Assert.Throws<ArgumentNullException>(() => asyncLock.Wait(null));
  17. }
  18. [Fact]
  19. public void Wait_Graceful()
  20. {
  21. var ok = false;
  22. new AsyncLock().Wait(() => { ok = true; });
  23. Assert.True(ok);
  24. }
  25. [Fact]
  26. public void Wait_Fail()
  27. {
  28. var l = new AsyncLock();
  29. var ex = new Exception();
  30. try
  31. {
  32. l.Wait(() => { throw ex; });
  33. Assert.True(false);
  34. }
  35. catch (Exception e)
  36. {
  37. Assert.Same(ex, e);
  38. }
  39. // has faulted; should not run
  40. l.Wait(() => { Assert.True(false); });
  41. }
  42. [Fact]
  43. public void Wait_QueuesWork()
  44. {
  45. var l = new AsyncLock();
  46. var l1 = false;
  47. var l2 = false;
  48. l.Wait(() => { l.Wait(() => { Assert.True(l1); l2 = true; }); l1 = true; });
  49. Assert.True(l2);
  50. }
  51. [Fact]
  52. public void Dispose()
  53. {
  54. var l = new AsyncLock();
  55. var l1 = false;
  56. var l2 = false;
  57. var l3 = false;
  58. var l4 = false;
  59. l.Wait(() =>
  60. {
  61. l.Wait(() =>
  62. {
  63. l.Wait(() =>
  64. {
  65. l3 = true;
  66. });
  67. l2 = true;
  68. l.Dispose();
  69. l.Wait(() =>
  70. {
  71. l4 = true;
  72. });
  73. });
  74. l1 = true;
  75. });
  76. Assert.True(l1);
  77. Assert.True(l2);
  78. Assert.False(l3);
  79. Assert.False(l4);
  80. }
  81. }
  82. }