DefaultConcurrencyAbstractionLayerTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 StartPeriodicTimer_Fast()
  127. {
  128. var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) };
  129. _domain.SetData("state", e);
  130. Run(() =>
  131. {
  132. var n = 0;
  133. Scheduler.Default.SchedulePeriodic(TimeSpan.Zero, () =>
  134. {
  135. var state = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("state");
  136. if (n++ == 10)
  137. state.Value.Set();
  138. });
  139. });
  140. e.Value.WaitOne();
  141. }
  142. [TestMethod]
  143. public void StartPeriodicTimer_Fast_Cancel()
  144. {
  145. var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) };
  146. _domain.SetData("set_cancel", e);
  147. Run(() =>
  148. {
  149. var n = 0;
  150. var schedule = Scheduler.Default.SchedulePeriodic(TimeSpan.Zero, () =>
  151. {
  152. _domain.SetData("value", n++);
  153. });
  154. _domain.SetData("cancel", new MarshalByRefAction(schedule.Dispose));
  155. var setCancel = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("set_cancel");
  156. setCancel.Value.Set();
  157. });
  158. e.Value.WaitOne();
  159. var value = (int)_domain.GetData("value");
  160. var cancel = (MarshalByRefAction)_domain.GetData("cancel");
  161. cancel.Invoke();
  162. Thread.Sleep(TimeSpan.FromMilliseconds(50));
  163. var newValue = (int)_domain.GetData("value");
  164. Assert.IsTrue(newValue >= value);
  165. Thread.Sleep(TimeSpan.FromMilliseconds(50));
  166. value = (int)_domain.GetData("value");
  167. Assert.AreEqual(newValue, value);
  168. }
  169. [TestMethod]
  170. public void CreateThread()
  171. {
  172. var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) };
  173. _domain.SetData("state", e);
  174. var r = new MarshalByRefCell<string> { Value = "" };
  175. _domain.SetData("res", r);
  176. Run(() =>
  177. {
  178. var state = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("state");
  179. var res = (MarshalByRefCell<string>)_domain.GetData("res");
  180. var svc = (IServiceProvider)Scheduler.Default;
  181. var per = (ISchedulerPeriodic)svc.GetService(typeof(ISchedulerPeriodic));
  182. if (per == null)
  183. {
  184. res.Value = "Failed to get ISchedulerPeriodic.";
  185. state.Value.Set();
  186. return;
  187. }
  188. var slr = (ISchedulerLongRunning)svc.GetService(typeof(ISchedulerLongRunning));
  189. if (slr == null)
  190. {
  191. res.Value = "Failed to get ISchedulerLongRunning.";
  192. state.Value.Set();
  193. return;
  194. }
  195. var success = false;
  196. try
  197. {
  198. slr.ScheduleLongRunning(42, null);
  199. }
  200. catch (ArgumentNullException)
  201. {
  202. success = true;
  203. }
  204. if (!success)
  205. {
  206. res.Value = "Failed null check ScheduleLongRunnign.";
  207. state.Value.Set();
  208. return;
  209. }
  210. state.Value.Set();
  211. #if !NO_THREAD
  212. var w = new ManualResetEvent(false);
  213. var d = slr.ScheduleLongRunning(cancel =>
  214. {
  215. while (!cancel.IsDisposed)
  216. ;
  217. w.Set();
  218. });
  219. Thread.Sleep(50);
  220. d.Dispose();
  221. w.WaitOne();
  222. #else
  223. state.Value.Set();
  224. #endif
  225. });
  226. e.Value.WaitOne();
  227. Assert.IsTrue(string.IsNullOrEmpty(r.Value));
  228. }
  229. #if !NO_TPL
  230. [TestMethod]
  231. public void Cant_Locate_Scheduler()
  232. {
  233. if (!Utils.IsRunningWithPortableLibraryBinaries())
  234. {
  235. Cant_Locate_Scheduler_NoPlib();
  236. }
  237. }
  238. [MethodImpl(MethodImplOptions.NoInlining)]
  239. private void Cant_Locate_Scheduler_NoPlib()
  240. {
  241. var e = new MarshalByRefCell<Exception>();
  242. _domain.SetData("state", e);
  243. Run(() =>
  244. {
  245. var state = (MarshalByRefCell<Exception>)_domain.GetData("state");
  246. try
  247. {
  248. Scheduler.TaskPool.Schedule(() => { });
  249. }
  250. catch (Exception ex)
  251. {
  252. state.Value = ex;
  253. }
  254. });
  255. Assert.IsTrue(e.Value != null && e.Value is NotSupportedException);
  256. }
  257. #endif
  258. #if !NO_PERF && !NO_STOPWATCH
  259. [TestMethod]
  260. public void Stopwatch()
  261. {
  262. var e = new MarshalByRefCell<bool>();
  263. _domain.SetData("state", e);
  264. Run(() =>
  265. {
  266. var state = (MarshalByRefCell<bool>)_domain.GetData("state");
  267. var sw = Scheduler.Default.StartStopwatch();
  268. var fst = sw.Elapsed;
  269. Thread.Sleep(100);
  270. var snd = sw.Elapsed;
  271. state.Value = snd > fst;
  272. });
  273. Assert.IsTrue(e.Value);
  274. }
  275. #endif
  276. [TestMethod]
  277. public void EnsureLoaded()
  278. {
  279. Assert.IsTrue(EnlightenmentProvider.EnsureLoaded());
  280. }
  281. }
  282. public class MarshalByRefCell<T> : MarshalByRefObject
  283. {
  284. public T Value;
  285. }
  286. public class MarshalByRefAction : MarshalByRefObject
  287. {
  288. private readonly Action _action;
  289. public MarshalByRefAction(Action action)
  290. {
  291. _action = action;
  292. }
  293. public void Invoke()
  294. {
  295. _action();
  296. }
  297. }
  298. }
  299. #endif