PageRenderTime 167ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Rx.NET/Source/Tests.System.Reactive/Tests/DefaultConcurrencyAbstractionLayerTest.cs

https://gitlab.com/svsamipillai/Rx.NET
C# | 408 lines | 319 code | 78 blank | 11 comment | 20 complexity | 62c26cf5ef5b19df26e3173240b2c341 MD5 | raw file
  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. }
  28. //
  29. // We want to replace the files to make the debugging experience
  30. // better. If the directory already exists, a recompilation does
  31. // not get rid of it. At some point, we should revisit this whole
  32. // directory creation during the test run and try to do away with
  33. // it by making it part of the build process.
  34. //
  35. foreach (var file in Directory.GetFiles(cur))
  36. {
  37. var fn = Path.GetFileName(file);
  38. if (!file.Contains("PlatformServices"))
  39. {
  40. var dest = Path.Combine(sub, fn);
  41. if (File.Exists(dest))
  42. {
  43. try
  44. {
  45. File.Delete(dest);
  46. }
  47. catch (UnauthorizedAccessException)
  48. {
  49. //
  50. // File in use; expected after first pass.
  51. //
  52. }
  53. }
  54. if (!File.Exists(dest))
  55. {
  56. File.Copy(Path.Combine(cur, fn), Path.Combine(sub, fn));
  57. }
  58. }
  59. }
  60. _domain = AppDomain.CreateDomain("Default_CAL", null, new AppDomainSetup { ApplicationBase = sub });
  61. }
  62. }
  63. private void Run(CrossAppDomainDelegate a)
  64. {
  65. _domain.DoCallBack(a);
  66. }
  67. [TestMethod]
  68. public void Sleep()
  69. {
  70. var ran = new MarshalByRefCell<bool>();
  71. _domain.SetData("state", ran);
  72. Run(() =>
  73. {
  74. Scheduler.Immediate.Schedule(TimeSpan.FromMilliseconds(1), () =>
  75. {
  76. var state = (MarshalByRefCell<bool>)_domain.GetData("state");
  77. state.Value = true;
  78. });
  79. });
  80. Assert.IsTrue(ran.Value);
  81. }
  82. [TestMethod]
  83. public void QueueUserWorkItem()
  84. {
  85. var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) };
  86. _domain.SetData("state", e);
  87. Run(() =>
  88. {
  89. Scheduler.Default.Schedule(() =>
  90. {
  91. var state = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("state");
  92. state.Value.Set();
  93. });
  94. });
  95. e.Value.WaitOne();
  96. }
  97. [TestMethod]
  98. public void StartTimer()
  99. {
  100. var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) };
  101. _domain.SetData("state", e);
  102. Run(() =>
  103. {
  104. Scheduler.Default.Schedule(TimeSpan.FromMilliseconds(10), () =>
  105. {
  106. var state = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("state");
  107. state.Value.Set();
  108. });
  109. });
  110. e.Value.WaitOne();
  111. }
  112. [TestMethod]
  113. public void StartTimer_Cancel()
  114. {
  115. Run(StartTimer_Cancel_Callback);
  116. }
  117. private static void StartTimer_Cancel_Callback()
  118. {
  119. Scheduler.Default.Schedule(TimeSpan.FromSeconds(60), () =>
  120. {
  121. throw new InvalidOperationException("This shouldn't have happened!");
  122. }).Dispose();
  123. }
  124. [TestMethod]
  125. public void StartPeriodicTimer()
  126. {
  127. var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) };
  128. _domain.SetData("state", e);
  129. Run(() =>
  130. {
  131. var n = 0;
  132. Scheduler.Default.SchedulePeriodic(TimeSpan.FromMilliseconds(10), () =>
  133. {
  134. var state = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("state");
  135. if (n++ == 10)
  136. state.Value.Set();
  137. });
  138. });
  139. e.Value.WaitOne();
  140. }
  141. [TestMethod]
  142. public void StartPeriodicTimer_Cancel()
  143. {
  144. Run(StartPeriodicTimer_Cancel_Callback);
  145. }
  146. private static void StartPeriodicTimer_Cancel_Callback()
  147. {
  148. Scheduler.Default.SchedulePeriodic(TimeSpan.FromSeconds(60), () =>
  149. {
  150. throw new InvalidOperationException("This shouldn't have happened!");
  151. }).Dispose();
  152. }
  153. [TestMethod]
  154. public void StartPeriodicTimer_Fast()
  155. {
  156. var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) };
  157. _domain.SetData("state", e);
  158. Run(() =>
  159. {
  160. var n = 0;
  161. Scheduler.Default.SchedulePeriodic(TimeSpan.Zero, () =>
  162. {
  163. var state = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("state");
  164. if (n++ == 10)
  165. state.Value.Set();
  166. });
  167. });
  168. e.Value.WaitOne();
  169. }
  170. [TestMethod]
  171. public void StartPeriodicTimer_Fast_Cancel()
  172. {
  173. var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) };
  174. _domain.SetData("set_cancel", e);
  175. Run(() =>
  176. {
  177. var n = 0;
  178. var hasAtLeastOneValue = new ManualResetEvent(false);
  179. var schedule = Scheduler.Default.SchedulePeriodic(TimeSpan.Zero, () =>
  180. {
  181. _domain.SetData("value", n++);
  182. hasAtLeastOneValue.Set();
  183. });
  184. _domain.SetData("cancel", new MarshalByRefAction(schedule.Dispose));
  185. hasAtLeastOneValue.WaitOne();
  186. var setCancel = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("set_cancel");
  187. setCancel.Value.Set();
  188. });
  189. e.Value.WaitOne();
  190. var value = (int)_domain.GetData("value");
  191. var cancel = (MarshalByRefAction)_domain.GetData("cancel");
  192. cancel.Invoke();
  193. Thread.Sleep(TimeSpan.FromMilliseconds(50));
  194. var newValue = (int)_domain.GetData("value");
  195. Assert.IsTrue(newValue >= value);
  196. Thread.Sleep(TimeSpan.FromMilliseconds(50));
  197. value = (int)_domain.GetData("value");
  198. Assert.AreEqual(newValue, value);
  199. }
  200. [TestMethod]
  201. public void CreateThread()
  202. {
  203. var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) };
  204. _domain.SetData("state", e);
  205. var r = new MarshalByRefCell<string> { Value = "" };
  206. _domain.SetData("res", r);
  207. Run(() =>
  208. {
  209. var state = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("state");
  210. var res = (MarshalByRefCell<string>)_domain.GetData("res");
  211. var svc = (IServiceProvider)Scheduler.Default;
  212. var per = (ISchedulerPeriodic)svc.GetService(typeof(ISchedulerPeriodic));
  213. if (per == null)
  214. {
  215. res.Value = "Failed to get ISchedulerPeriodic.";
  216. state.Value.Set();
  217. return;
  218. }
  219. var slr = (ISchedulerLongRunning)svc.GetService(typeof(ISchedulerLongRunning));
  220. if (slr == null)
  221. {
  222. res.Value = "Failed to get ISchedulerLongRunning.";
  223. state.Value.Set();
  224. return;
  225. }
  226. var success = false;
  227. try
  228. {
  229. slr.ScheduleLongRunning(42, null);
  230. }
  231. catch (ArgumentNullException)
  232. {
  233. success = true;
  234. }
  235. if (!success)
  236. {
  237. res.Value = "Failed null check ScheduleLongRunning.";
  238. state.Value.Set();
  239. return;
  240. }
  241. state.Value.Set();
  242. #if !NO_THREAD
  243. var w = new ManualResetEvent(false);
  244. var d = slr.ScheduleLongRunning(cancel =>
  245. {
  246. while (!cancel.IsDisposed)
  247. ;
  248. w.Set();
  249. });
  250. Thread.Sleep(50);
  251. d.Dispose();
  252. w.WaitOne();
  253. #else
  254. state.Value.Set();
  255. #endif
  256. });
  257. e.Value.WaitOne();
  258. Assert.IsTrue(string.IsNullOrEmpty(r.Value));
  259. }
  260. #if !NO_TPL
  261. [TestMethod]
  262. public void Cant_Locate_Scheduler()
  263. {
  264. if (!Utils.IsRunningWithPortableLibraryBinaries())
  265. {
  266. Cant_Locate_Scheduler_NoPlib();
  267. }
  268. }
  269. [MethodImpl(MethodImplOptions.NoInlining)]
  270. private void Cant_Locate_Scheduler_NoPlib()
  271. {
  272. var e = new MarshalByRefCell<Exception>();
  273. _domain.SetData("state", e);
  274. Run(() =>
  275. {
  276. var state = (MarshalByRefCell<Exception>)_domain.GetData("state");
  277. try
  278. {
  279. Scheduler.TaskPool.Schedule(() => { });
  280. }
  281. catch (Exception ex)
  282. {
  283. state.Value = ex;
  284. }
  285. });
  286. Assert.IsTrue(e.Value != null && e.Value is NotSupportedException);
  287. }
  288. #endif
  289. #if !NO_PERF && !NO_STOPWATCH
  290. [TestMethod]
  291. public void Stopwatch()
  292. {
  293. var e = new MarshalByRefCell<bool>();
  294. _domain.SetData("state", e);
  295. Run(() =>
  296. {
  297. var state = (MarshalByRefCell<bool>)_domain.GetData("state");
  298. var sw = Scheduler.Default.StartStopwatch();
  299. var fst = sw.Elapsed;
  300. Thread.Sleep(100);
  301. var snd = sw.Elapsed;
  302. state.Value = snd > fst;
  303. });
  304. Assert.IsTrue(e.Value);
  305. }
  306. #endif
  307. [TestMethod]
  308. public void EnsureLoaded()
  309. {
  310. Assert.IsTrue(EnlightenmentProvider.EnsureLoaded());
  311. }
  312. }
  313. public class MarshalByRefCell<T> : MarshalByRefObject
  314. {
  315. public T Value;
  316. }
  317. public class MarshalByRefAction : MarshalByRefObject
  318. {
  319. private readonly Action _action;
  320. public MarshalByRefAction(Action action)
  321. {
  322. _action = action;
  323. }
  324. public void Invoke()
  325. {
  326. _action();
  327. }
  328. }
  329. }
  330. #endif