DefaultConcurrencyAbstractionLayerTest.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. #if !NO_REMOTING
  3. using System;
  4. using System.IO;
  5. using System.Reactive.Concurrency;
  6. using System.Reactive.PlatformServices;
  7. using System.Runtime.CompilerServices;
  8. using System.Threading;
  9. using Microsoft.VisualStudio.TestTools.UnitTesting;
  10. namespace ReactiveTests.Tests
  11. {
  12. [TestClass]
  13. [Serializable]
  14. public class DefaultConcurrencyAbstractionLayerTest
  15. {
  16. private AppDomain _domain;
  17. [TestInitialize]
  18. public void Init()
  19. {
  20. if (_domain == null)
  21. {
  22. var cur = AppDomain.CurrentDomain.BaseDirectory;
  23. var sub = Path.Combine(cur, "NoCAL");
  24. if (!Directory.Exists(sub))
  25. {
  26. Directory.CreateDirectory(sub);
  27. foreach (var file in Directory.GetFiles(cur))
  28. {
  29. var fn = Path.GetFileName(file);
  30. if (!file.Contains("PlatformServices"))
  31. File.Copy(Path.Combine(cur, fn), Path.Combine(sub, fn));
  32. }
  33. }
  34. _domain = AppDomain.CreateDomain("Default_CAL", null, new AppDomainSetup { ApplicationBase = sub });
  35. }
  36. }
  37. private void Run(CrossAppDomainDelegate a)
  38. {
  39. _domain.DoCallBack(a);
  40. }
  41. [TestMethod]
  42. public void Sleep()
  43. {
  44. var ran = new MarshalByRefCell<bool>();
  45. _domain.SetData("state", ran);
  46. Run(() =>
  47. {
  48. Scheduler.Immediate.Schedule(TimeSpan.FromMilliseconds(1), () =>
  49. {
  50. var state = (MarshalByRefCell<bool>)_domain.GetData("state");
  51. state.Value = true;
  52. });
  53. });
  54. Assert.IsTrue(ran.Value);
  55. }
  56. [TestMethod]
  57. public void QueueUserWorkItem()
  58. {
  59. var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) };
  60. _domain.SetData("state", e);
  61. Run(() =>
  62. {
  63. Scheduler.Default.Schedule(() =>
  64. {
  65. var state = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("state");
  66. state.Value.Set();
  67. });
  68. });
  69. e.Value.WaitOne();
  70. }
  71. [TestMethod]
  72. public void StartTimer()
  73. {
  74. var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) };
  75. _domain.SetData("state", e);
  76. Run(() =>
  77. {
  78. Scheduler.Default.Schedule(TimeSpan.FromMilliseconds(10), () =>
  79. {
  80. var state = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("state");
  81. state.Value.Set();
  82. });
  83. });
  84. e.Value.WaitOne();
  85. }
  86. [TestMethod]
  87. public void StartTimer_Cancel()
  88. {
  89. Run(() =>
  90. {
  91. Scheduler.Default.Schedule(TimeSpan.FromSeconds(60), () =>
  92. {
  93. throw new InvalidOperationException("This shouldn't have happened!");
  94. }).Dispose();
  95. });
  96. }
  97. [TestMethod]
  98. public void StartPeriodicTimer()
  99. {
  100. var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) };
  101. _domain.SetData("state", e);
  102. Run(() =>
  103. {
  104. var n = 0;
  105. Scheduler.Default.SchedulePeriodic(TimeSpan.FromMilliseconds(10), () =>
  106. {
  107. var state = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("state");
  108. if (n++ == 10)
  109. state.Value.Set();
  110. });
  111. });
  112. e.Value.WaitOne();
  113. }
  114. [TestMethod]
  115. public void StartPeriodicTimer_Cancel()
  116. {
  117. Run(() =>
  118. {
  119. Scheduler.Default.SchedulePeriodic(TimeSpan.FromSeconds(60), () =>
  120. {
  121. throw new InvalidOperationException("This shouldn't have happened!");
  122. }).Dispose();
  123. });
  124. }
  125. [TestMethod]
  126. public void CreateThread()
  127. {
  128. var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) };
  129. _domain.SetData("state", e);
  130. var r = new MarshalByRefCell<string> { Value = "" };
  131. _domain.SetData("res", r);
  132. Run(() =>
  133. {
  134. var state = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("state");
  135. var res = (MarshalByRefCell<string>)_domain.GetData("res");
  136. var svc = (IServiceProvider)Scheduler.Default;
  137. var per = (ISchedulerPeriodic)svc.GetService(typeof(ISchedulerPeriodic));
  138. if (per == null)
  139. {
  140. res.Value = "Failed to get ISchedulerPeriodic.";
  141. state.Value.Set();
  142. return;
  143. }
  144. var slr = (ISchedulerLongRunning)svc.GetService(typeof(ISchedulerLongRunning));
  145. if (slr == null)
  146. {
  147. res.Value = "Failed to get ISchedulerLongRunning.";
  148. state.Value.Set();
  149. return;
  150. }
  151. var success = false;
  152. try
  153. {
  154. slr.ScheduleLongRunning(42, null);
  155. }
  156. catch (ArgumentNullException)
  157. {
  158. success = true;
  159. }
  160. if (!success)
  161. {
  162. res.Value = "Failed null check ScheduleLongRunnign.";
  163. state.Value.Set();
  164. return;
  165. }
  166. state.Value.Set();
  167. #if !NO_THREAD
  168. var w = new ManualResetEvent(false);
  169. var d = slr.ScheduleLongRunning(cancel =>
  170. {
  171. while (!cancel.IsDisposed)
  172. ;
  173. w.Set();
  174. });
  175. Thread.Sleep(50);
  176. d.Dispose();
  177. w.WaitOne();
  178. #else
  179. state.Value.Set();
  180. #endif
  181. });
  182. e.Value.WaitOne();
  183. Assert.IsTrue(string.IsNullOrEmpty(r.Value));
  184. }
  185. #if !NO_TPL
  186. [TestMethod]
  187. public void Cant_Locate_Scheduler()
  188. {
  189. if (!Utils.IsRunningWithPortableLibraryBinaries())
  190. {
  191. Cant_Locate_Scheduler_NoPlib();
  192. }
  193. }
  194. [MethodImpl(MethodImplOptions.NoInlining)]
  195. private void Cant_Locate_Scheduler_NoPlib()
  196. {
  197. var e = new MarshalByRefCell<Exception>();
  198. _domain.SetData("state", e);
  199. Run(() =>
  200. {
  201. var state = (MarshalByRefCell<Exception>)_domain.GetData("state");
  202. try
  203. {
  204. Scheduler.TaskPool.Schedule(() => { });
  205. }
  206. catch (Exception ex)
  207. {
  208. state.Value = ex;
  209. }
  210. });
  211. Assert.IsTrue(e.Value != null && e.Value is NotSupportedException);
  212. }
  213. #endif
  214. #if !NO_PERF && !NO_STOPWATCH
  215. [TestMethod]
  216. public void Stopwatch()
  217. {
  218. var e = new MarshalByRefCell<bool>();
  219. _domain.SetData("state", e);
  220. Run(() =>
  221. {
  222. var state = (MarshalByRefCell<bool>)_domain.GetData("state");
  223. var sw = Scheduler.Default.StartStopwatch();
  224. var fst = sw.Elapsed;
  225. Thread.Sleep(100);
  226. var snd = sw.Elapsed;
  227. state.Value = snd > fst;
  228. });
  229. Assert.IsTrue(e.Value);
  230. }
  231. #endif
  232. [TestMethod]
  233. public void EnsureLoaded()
  234. {
  235. Assert.IsTrue(EnlightenmentProvider.EnsureLoaded());
  236. }
  237. }
  238. public class MarshalByRefCell<T> : MarshalByRefObject
  239. {
  240. public T Value;
  241. }
  242. }
  243. #endif