1
0

SynchronizationContextSchedulerTest.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. using System;
  3. using System.Diagnostics;
  4. using System.Reactive.Concurrency;
  5. using System.Reactive.Disposables;
  6. using System.Threading;
  7. using Microsoft.Reactive.Testing;
  8. using Microsoft.VisualStudio.TestTools.UnitTesting;
  9. namespace ReactiveTests.Tests
  10. {
  11. [TestClass]
  12. public class SynchronizationContextSchedulerTest
  13. {
  14. [TestMethod]
  15. public void SynchronizationContext_ArgumentChecking()
  16. {
  17. var ms = new MySync();
  18. var s = new SynchronizationContextScheduler(ms);
  19. ReactiveAssert.Throws<ArgumentNullException>(() => new SynchronizationContextScheduler(null));
  20. ReactiveAssert.Throws<ArgumentNullException>(() => new SynchronizationContextScheduler(null, true));
  21. ReactiveAssert.Throws<ArgumentNullException>(() => s.Schedule<int>(42, default(Func<IScheduler, int, IDisposable>)));
  22. ReactiveAssert.Throws<ArgumentNullException>(() => s.Schedule<int>(42, DateTimeOffset.Now, default(Func<IScheduler, int, IDisposable>)));
  23. ReactiveAssert.Throws<ArgumentNullException>(() => s.Schedule<int>(42, TimeSpan.Zero, default(Func<IScheduler, int, IDisposable>)));
  24. }
  25. [TestMethod]
  26. public void SynchronizationContext_Now()
  27. {
  28. var ms = new MySync();
  29. var s = new SynchronizationContextScheduler(ms);
  30. var res = s.Now - DateTime.Now;
  31. Assert.IsTrue(res.Seconds < 1);
  32. }
  33. [TestMethod]
  34. public void SynchronizationContext_ScheduleAction()
  35. {
  36. var ms = new MySync();
  37. var s = new SynchronizationContextScheduler(ms);
  38. var ran = false;
  39. s.Schedule(() => { ran = true; });
  40. Assert.IsTrue(ms.Count == 1);
  41. Assert.IsTrue(ran);
  42. }
  43. [TestMethod]
  44. public void SynchronizationContext_ScheduleAction_TimeSpan()
  45. {
  46. var ms = new MySync();
  47. var s = new SynchronizationContextScheduler(ms);
  48. var e = new ManualResetEvent(false);
  49. s.Schedule(TimeSpan.FromMilliseconds(1), () => { e.Set(); });
  50. e.WaitOne();
  51. Assert.IsTrue(ms.Count == 1);
  52. }
  53. [TestMethod]
  54. public void SynchronizationContext_ScheduleAction_DateTimeOffset()
  55. {
  56. var ms = new MySync();
  57. var s = new SynchronizationContextScheduler(ms);
  58. var e = new ManualResetEvent(false);
  59. s.Schedule(DateTimeOffset.Now.AddMilliseconds(100), () => { e.Set(); });
  60. e.WaitOne();
  61. Assert.IsTrue(ms.Count >= 1); // Can be > 1 in case of timer queue retry operations.
  62. }
  63. [TestMethod]
  64. public void SynchronizationContext_ScheduleActionError()
  65. {
  66. var ms = new MySync();
  67. var s = new SynchronizationContextScheduler(ms);
  68. var ex = new Exception();
  69. try
  70. {
  71. s.Schedule(() => { throw ex; });
  72. Assert.Fail();
  73. }
  74. catch (Exception e)
  75. {
  76. Assert.AreSame(e, ex);
  77. }
  78. Assert.IsTrue(ms.Count == 1);
  79. }
  80. #if !SILVERLIGHT
  81. [TestMethod]
  82. [Ignore]
  83. public void SynchronizationContext_ScheduleActionDue()
  84. {
  85. var ms = new MySync();
  86. var s = new SynchronizationContextScheduler(ms);
  87. var evt = new ManualResetEvent(false);
  88. var sw = new Stopwatch();
  89. sw.Start();
  90. s.Schedule(TimeSpan.FromSeconds(0.2), () => { sw.Stop(); evt.Set(); });
  91. evt.WaitOne();
  92. Assert.IsTrue(sw.ElapsedMilliseconds > 180, "due " + sw.ElapsedMilliseconds);
  93. Assert.IsTrue(ms.Count == 1);
  94. }
  95. #endif
  96. 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<int>(42, TimeSpan.Zero, (self, x) => { started = ms.Started; return Disposable.Empty; });
  128. Assert.IsTrue(started == 1);
  129. Assert.IsTrue(ms.Count == 1);
  130. Assert.IsTrue(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.IsTrue(ms.Count == 1);
  140. Assert.IsTrue(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.IsTrue(count == 0 /* no post */);
  158. Assert.IsTrue(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.IsTrue(ms.Count == 1);
  168. Assert.IsTrue(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.IsTrue(count == 1 /* post */);
  186. Assert.IsTrue(ran);
  187. }
  188. }
  189. }