SynchronizationContextSchedulerTest.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. ReactiveAssert.Throws<ArgumentNullException>(() => new SynchronizationContextScheduler(null));
  23. ReactiveAssert.Throws<ArgumentNullException>(() => new SynchronizationContextScheduler(null, true));
  24. ReactiveAssert.Throws<ArgumentNullException>(() => s.Schedule(42, default));
  25. ReactiveAssert.Throws<ArgumentNullException>(() => s.Schedule(42, DateTimeOffset.Now, default));
  26. ReactiveAssert.Throws<ArgumentNullException>(() => s.Schedule(42, TimeSpan.Zero, default));
  27. }
  28. [TestMethod]
  29. public void SynchronizationContext_Now()
  30. {
  31. var ms = new MySync();
  32. var s = new SynchronizationContextScheduler(ms);
  33. var res = s.Now - DateTime.Now;
  34. Assert.True(res.Seconds < 1);
  35. }
  36. [TestMethod]
  37. public void SynchronizationContext_ScheduleAction()
  38. {
  39. var ms = new MySync();
  40. var s = new SynchronizationContextScheduler(ms);
  41. var ran = false;
  42. s.Schedule(() => { ran = true; });
  43. Assert.True(ms.Count == 1);
  44. Assert.True(ran);
  45. }
  46. [TestMethod]
  47. public void SynchronizationContext_ScheduleAction_TimeSpan()
  48. {
  49. var ms = new MySync();
  50. var s = new SynchronizationContextScheduler(ms);
  51. var e = new ManualResetEvent(false);
  52. s.Schedule(TimeSpan.FromMilliseconds(1), () => { e.Set(); });
  53. e.WaitOne();
  54. Assert.True(ms.Count == 1);
  55. }
  56. [TestMethod]
  57. public void SynchronizationContext_ScheduleAction_DateTimeOffset()
  58. {
  59. var ms = new MySync();
  60. var s = new SynchronizationContextScheduler(ms);
  61. var e = new ManualResetEvent(false);
  62. s.Schedule(DateTimeOffset.Now.AddMilliseconds(100), () => { e.Set(); });
  63. e.WaitOne();
  64. Assert.True(ms.Count >= 1); // Can be > 1 in case of timer queue retry operations.
  65. }
  66. [TestMethod]
  67. public void SynchronizationContext_ScheduleActionError()
  68. {
  69. var ms = new MySync();
  70. var s = new SynchronizationContextScheduler(ms);
  71. var ex = new Exception();
  72. try
  73. {
  74. s.Schedule(() => { throw ex; });
  75. Assert.True(false);
  76. }
  77. catch (Exception e)
  78. {
  79. Assert.Same(e, ex);
  80. }
  81. Assert.True(ms.Count == 1);
  82. }
  83. [TestMethod]
  84. public void SynchronizationContext_ScheduleActionDue()
  85. {
  86. var ms = new MySync();
  87. var s = new SynchronizationContextScheduler(ms);
  88. var evt = new ManualResetEvent(false);
  89. var sw = new Stopwatch();
  90. sw.Start();
  91. s.Schedule(TimeSpan.FromSeconds(0.2), () => { sw.Stop(); evt.Set(); });
  92. evt.WaitOne();
  93. Assert.True(sw.ElapsedMilliseconds > 180, "due " + sw.ElapsedMilliseconds);
  94. Assert.True(ms.Count == 1);
  95. }
  96. private class MySync : SynchronizationContext
  97. {
  98. public int Count { get; private set; }
  99. public override void Post(SendOrPostCallback d, object state)
  100. {
  101. Count++;
  102. d(state);
  103. }
  104. public override void Send(SendOrPostCallback d, object state)
  105. {
  106. throw new NotImplementedException();
  107. }
  108. public int Started { get; private set; }
  109. public override void OperationStarted()
  110. {
  111. base.OperationStarted();
  112. Started++;
  113. }
  114. public int Completed { get; private set; }
  115. public override void OperationCompleted()
  116. {
  117. base.OperationCompleted();
  118. Completed++;
  119. }
  120. }
  121. [TestMethod]
  122. public void SynchronizationContext_StartedCompleted()
  123. {
  124. var ms = new MySync();
  125. var s = new SynchronizationContextScheduler(ms);
  126. var started = 0;
  127. s.Schedule(42, TimeSpan.Zero, (self, x) => { started = ms.Started; return Disposable.Empty; });
  128. Assert.True(started == 1);
  129. Assert.True(ms.Count == 1);
  130. Assert.True(ms.Completed == 1);
  131. }
  132. [TestMethod]
  133. public void SynchronizationContext_DontPost_Different()
  134. {
  135. var ms = new MySync();
  136. var s = new SynchronizationContextScheduler(ms, false);
  137. var ran = false;
  138. s.Schedule(() => { ran = true; });
  139. Assert.True(ms.Count == 1);
  140. Assert.True(ran);
  141. }
  142. [TestMethod]
  143. public void SynchronizationContext_DontPost_Same()
  144. {
  145. var count = 0;
  146. var ran = false;
  147. var t = new Thread(() =>
  148. {
  149. var ms = new MySync();
  150. SynchronizationContext.SetSynchronizationContext(ms);
  151. var s = new SynchronizationContextScheduler(ms, false);
  152. s.Schedule(() => { ran = true; });
  153. count = ms.Count;
  154. });
  155. t.Start();
  156. t.Join();
  157. Assert.True(count == 0 /* no post */);
  158. Assert.True(ran);
  159. }
  160. [TestMethod]
  161. public void SynchronizationContext_AlwaysPost_Different()
  162. {
  163. var ms = new MySync();
  164. var s = new SynchronizationContextScheduler(ms, true);
  165. var ran = false;
  166. s.Schedule(() => { ran = true; });
  167. Assert.True(ms.Count == 1);
  168. Assert.True(ran);
  169. }
  170. [TestMethod]
  171. public void SynchronizationContext_AlwaysPost_Same()
  172. {
  173. var count = 0;
  174. var ran = false;
  175. var t = new Thread(() =>
  176. {
  177. var ms = new MySync();
  178. SynchronizationContext.SetSynchronizationContext(ms);
  179. var s = new SynchronizationContextScheduler(ms, true);
  180. s.Schedule(() => { ran = true; });
  181. count = ms.Count;
  182. });
  183. t.Start();
  184. t.Join();
  185. Assert.True(count == 1 /* post */);
  186. Assert.True(ran);
  187. }
  188. }
  189. }