SynchronizationContextSchedulerTest.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Diagnostics;
  6. using System.Reactive.Concurrency;
  7. using System.Reactive.Disposables;
  8. using System.Threading;
  9. using Microsoft.Reactive.Testing;
  10. using Microsoft.VisualStudio.TestTools.UnitTesting;
  11. using Assert = Xunit.Assert;
  12. namespace ReactiveTests.Tests
  13. {
  14. [TestClass]
  15. public class SynchronizationContextSchedulerTest
  16. {
  17. [TestMethod]
  18. public void SynchronizationContext_ArgumentChecking()
  19. {
  20. var ms = new MySync();
  21. var s = new SynchronizationContextScheduler(ms);
  22. #pragma warning disable CA1806 // (Unused new instance.) We expect the constructor to throw.
  23. ReactiveAssert.Throws<ArgumentNullException>(() => new SynchronizationContextScheduler(null));
  24. ReactiveAssert.Throws<ArgumentNullException>(() => new SynchronizationContextScheduler(null, true));
  25. #pragma warning restore CA1806
  26. ReactiveAssert.Throws<ArgumentNullException>(() => s.Schedule(42, default));
  27. ReactiveAssert.Throws<ArgumentNullException>(() => s.Schedule(42, DateTimeOffset.Now, default));
  28. ReactiveAssert.Throws<ArgumentNullException>(() => s.Schedule(42, TimeSpan.Zero, default));
  29. }
  30. [TestMethod]
  31. public void SynchronizationContext_Now()
  32. {
  33. var ms = new MySync();
  34. var s = new SynchronizationContextScheduler(ms);
  35. var res = s.Now - DateTime.Now;
  36. Assert.True(res.Seconds < 1);
  37. }
  38. [TestMethod]
  39. public void SynchronizationContext_ScheduleAction()
  40. {
  41. var ms = new MySync();
  42. var s = new SynchronizationContextScheduler(ms);
  43. var ran = false;
  44. s.Schedule(() => { ran = true; });
  45. Assert.True(ms.Count == 1);
  46. Assert.True(ran);
  47. }
  48. [TestMethod]
  49. public void SynchronizationContext_ScheduleAction_TimeSpan()
  50. {
  51. var ms = new MySync();
  52. var s = new SynchronizationContextScheduler(ms);
  53. var e = new ManualResetEvent(false);
  54. s.Schedule(TimeSpan.FromMilliseconds(1), () => { e.Set(); });
  55. e.WaitOne();
  56. Assert.True(ms.Count == 1);
  57. }
  58. [TestMethod]
  59. public void SynchronizationContext_ScheduleAction_DateTimeOffset()
  60. {
  61. var ms = new MySync();
  62. var s = new SynchronizationContextScheduler(ms);
  63. var e = new ManualResetEvent(false);
  64. s.Schedule(DateTimeOffset.Now.AddMilliseconds(100), () => { e.Set(); });
  65. e.WaitOne();
  66. Assert.True(ms.Count >= 1); // Can be > 1 in case of timer queue retry operations.
  67. }
  68. [TestMethod]
  69. public void SynchronizationContext_ScheduleActionError()
  70. {
  71. var ms = new MySync();
  72. var s = new SynchronizationContextScheduler(ms);
  73. var ex = new Exception();
  74. try
  75. {
  76. s.Schedule(() => { throw ex; });
  77. Assert.True(false);
  78. }
  79. catch (Exception e)
  80. {
  81. Assert.Same(e, ex);
  82. }
  83. Assert.True(ms.Count == 1);
  84. }
  85. [TestMethod]
  86. public void SynchronizationContext_ScheduleActionDue()
  87. {
  88. var ms = new MySync();
  89. var s = new SynchronizationContextScheduler(ms);
  90. var evt = new ManualResetEvent(false);
  91. var sw = new Stopwatch();
  92. sw.Start();
  93. s.Schedule(TimeSpan.FromSeconds(0.2), () => { sw.Stop(); evt.Set(); });
  94. evt.WaitOne();
  95. Assert.True(sw.ElapsedMilliseconds > 180, "due " + sw.ElapsedMilliseconds);
  96. Assert.True(ms.Count == 1);
  97. }
  98. private class MySync : SynchronizationContext
  99. {
  100. public int Count { get; private set; }
  101. public override void Post(SendOrPostCallback d, object state)
  102. {
  103. Count++;
  104. d(state);
  105. }
  106. public override void Send(SendOrPostCallback d, object state)
  107. {
  108. throw new NotImplementedException();
  109. }
  110. public int Started { get; private set; }
  111. public override void OperationStarted()
  112. {
  113. base.OperationStarted();
  114. Started++;
  115. }
  116. public int Completed { get; private set; }
  117. public override void OperationCompleted()
  118. {
  119. base.OperationCompleted();
  120. Completed++;
  121. }
  122. }
  123. [TestMethod]
  124. public void SynchronizationContext_StartedCompleted()
  125. {
  126. var ms = new MySync();
  127. var s = new SynchronizationContextScheduler(ms);
  128. var started = 0;
  129. s.Schedule(42, TimeSpan.Zero, (self, x) => { started = ms.Started; return Disposable.Empty; });
  130. Assert.True(started == 1);
  131. Assert.True(ms.Count == 1);
  132. Assert.True(ms.Completed == 1);
  133. }
  134. [TestMethod]
  135. public void SynchronizationContext_DontPost_Different()
  136. {
  137. var ms = new MySync();
  138. var s = new SynchronizationContextScheduler(ms, false);
  139. var ran = false;
  140. s.Schedule(() => { ran = true; });
  141. Assert.True(ms.Count == 1);
  142. Assert.True(ran);
  143. }
  144. [TestMethod]
  145. public void SynchronizationContext_DontPost_Same()
  146. {
  147. var count = 0;
  148. var ran = false;
  149. var t = new Thread(() =>
  150. {
  151. var ms = new MySync();
  152. SynchronizationContext.SetSynchronizationContext(ms);
  153. var s = new SynchronizationContextScheduler(ms, false);
  154. s.Schedule(() => { ran = true; });
  155. count = ms.Count;
  156. });
  157. t.Start();
  158. t.Join();
  159. Assert.True(count == 0 /* no post */);
  160. Assert.True(ran);
  161. }
  162. [TestMethod]
  163. public void SynchronizationContext_AlwaysPost_Different()
  164. {
  165. var ms = new MySync();
  166. var s = new SynchronizationContextScheduler(ms, true);
  167. var ran = false;
  168. s.Schedule(() => { ran = true; });
  169. Assert.True(ms.Count == 1);
  170. Assert.True(ran);
  171. }
  172. [TestMethod]
  173. public void SynchronizationContext_AlwaysPost_Same()
  174. {
  175. var count = 0;
  176. var ran = false;
  177. var t = new Thread(() =>
  178. {
  179. var ms = new MySync();
  180. SynchronizationContext.SetSynchronizationContext(ms);
  181. var s = new SynchronizationContextScheduler(ms, true);
  182. s.Schedule(() => { ran = true; });
  183. count = ms.Count;
  184. });
  185. t.Start();
  186. t.Join();
  187. Assert.True(count == 1 /* post */);
  188. Assert.True(ran);
  189. }
  190. }
  191. }