1
0

AsyncLockTest.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #if !SILVERLIGHT // MethodAccessException
  5. using System;
  6. using System.Threading.Tasks;
  7. using System.Reactive.Concurrency;
  8. using System.Reflection;
  9. using Xunit;
  10. namespace ReactiveTests.Tests
  11. {
  12. public class AsyncLockTest
  13. {
  14. [Fact]
  15. public void Wait_ArgumentChecking()
  16. {
  17. var asyncLock = new AsyncLock();
  18. Assert.Throws<ArgumentNullException>(() => asyncLock.Wait(null));
  19. }
  20. [Fact]
  21. public void Wait_Graceful()
  22. {
  23. var ok = false;
  24. new AsyncLock().Wait(() => { ok = true; });
  25. Assert.True(ok);
  26. }
  27. [Fact]
  28. public void Wait_Fail()
  29. {
  30. var l = new AsyncLock();
  31. var ex = new Exception();
  32. try
  33. {
  34. l.Wait(() => { throw ex; });
  35. Assert.True(false);
  36. }
  37. catch (Exception e)
  38. {
  39. Assert.Same(ex, e);
  40. }
  41. // has faulted; should not run
  42. l.Wait(() => { Assert.True(false); });
  43. }
  44. [Fact]
  45. public void Wait_QueuesWork()
  46. {
  47. var l = new AsyncLock();
  48. var l1 = false;
  49. var l2 = false;
  50. l.Wait(() => { l.Wait(() => { Assert.True(l1); l2 = true; }); l1 = true; });
  51. Assert.True(l2);
  52. }
  53. [Fact]
  54. public void Dispose()
  55. {
  56. var l = new AsyncLock();
  57. var l1 = false;
  58. var l2 = false;
  59. var l3 = false;
  60. var l4 = false;
  61. l.Wait(() =>
  62. {
  63. l.Wait(() =>
  64. {
  65. l.Wait(() =>
  66. {
  67. l3 = true;
  68. });
  69. l2 = true;
  70. l.Dispose();
  71. l.Wait(() =>
  72. {
  73. l4 = true;
  74. });
  75. });
  76. l1 = true;
  77. });
  78. Assert.True(l1);
  79. Assert.True(l2);
  80. Assert.False(l3);
  81. Assert.False(l4);
  82. }
  83. public class AsyncLock
  84. {
  85. object instance;
  86. public AsyncLock()
  87. {
  88. instance = typeof(Scheduler).GetTypeInfo().Assembly.GetType("System.Reactive.Concurrency.AsyncLock").GetConstructor(new Type[] { }).Invoke(new object[] { });
  89. }
  90. public void Wait(Action action)
  91. {
  92. try
  93. {
  94. instance.GetType().GetMethod(nameof(AsyncLock.Wait)).Invoke(instance, new object[] { action });
  95. }
  96. catch (TargetInvocationException ex)
  97. {
  98. throw ex.InnerException;
  99. }
  100. }
  101. public void Dispose()
  102. {
  103. try
  104. {
  105. instance.GetType().GetMethod(nameof(AsyncLock.Dispose)).Invoke(instance, new object[0]);
  106. }
  107. catch (TargetInvocationException ex)
  108. {
  109. throw ex.InnerException;
  110. }
  111. }
  112. }
  113. }
  114. }
  115. #endif