DefaultConcurrencyAbstractionLayerTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. #if !NO_REMOTING
  5. using System;
  6. using System.IO;
  7. using System.Reactive.Concurrency;
  8. using System.Reactive.PlatformServices;
  9. using System.Runtime.CompilerServices;
  10. using System.Threading;
  11. using Xunit;
  12. namespace ReactiveTests.Tests
  13. {
  14. [Serializable]
  15. public class DefaultConcurrencyAbstractionLayerTest
  16. {
  17. private AppDomain _domain;
  18. public DefaultConcurrencyAbstractionLayerTest()
  19. {
  20. if (_domain == null)
  21. {
  22. _domain = AppDomain.CreateDomain("Default_CAL", null, new AppDomainSetup { ApplicationBase = AppDomain.CurrentDomain.BaseDirectory });
  23. }
  24. }
  25. private void Run(CrossAppDomainDelegate a)
  26. {
  27. _domain.DoCallBack(a);
  28. }
  29. [Fact]
  30. public void Sleep()
  31. {
  32. var ran = new MarshalByRefCell<bool>();
  33. _domain.SetData("state", ran);
  34. Run(() =>
  35. {
  36. Scheduler.Immediate.Schedule(TimeSpan.FromMilliseconds(1), () =>
  37. {
  38. var state = (MarshalByRefCell<bool>)_domain.GetData("state");
  39. state.Value = true;
  40. });
  41. });
  42. Assert.True(ran.Value);
  43. }
  44. [Fact]
  45. public void QueueUserWorkItem()
  46. {
  47. var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) };
  48. _domain.SetData("state", e);
  49. Run(() =>
  50. {
  51. Scheduler.Default.Schedule(() =>
  52. {
  53. var state = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("state");
  54. state.Value.Set();
  55. });
  56. });
  57. e.Value.WaitOne();
  58. }
  59. [Fact]
  60. public void StartTimer()
  61. {
  62. var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) };
  63. _domain.SetData("state", e);
  64. Run(() =>
  65. {
  66. Scheduler.Default.Schedule(TimeSpan.FromMilliseconds(10), () =>
  67. {
  68. var state = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("state");
  69. state.Value.Set();
  70. });
  71. });
  72. e.Value.WaitOne();
  73. }
  74. [Fact]
  75. public void StartTimer_Cancel()
  76. {
  77. Run(StartTimer_Cancel_Callback);
  78. }
  79. private static void StartTimer_Cancel_Callback()
  80. {
  81. Scheduler.Default.Schedule(TimeSpan.FromSeconds(60), () =>
  82. {
  83. throw new InvalidOperationException("This shouldn't have happened!");
  84. }).Dispose();
  85. }
  86. [Fact]
  87. public void StartPeriodicTimer()
  88. {
  89. var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) };
  90. _domain.SetData("state", e);
  91. Run(() =>
  92. {
  93. var n = 0;
  94. Scheduler.Default.SchedulePeriodic(TimeSpan.FromMilliseconds(10), () =>
  95. {
  96. var state = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("state");
  97. if (n++ == 10)
  98. {
  99. state.Value.Set();
  100. }
  101. });
  102. });
  103. e.Value.WaitOne();
  104. }
  105. [Fact]
  106. public void StartPeriodicTimer_Cancel()
  107. {
  108. Run(StartPeriodicTimer_Cancel_Callback);
  109. }
  110. private static void StartPeriodicTimer_Cancel_Callback()
  111. {
  112. Scheduler.Default.SchedulePeriodic(TimeSpan.FromSeconds(60), () =>
  113. {
  114. throw new InvalidOperationException("This shouldn't have happened!");
  115. }).Dispose();
  116. }
  117. [Fact]
  118. public void StartPeriodicTimer_Fast()
  119. {
  120. var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) };
  121. _domain.SetData("state", e);
  122. Run(() =>
  123. {
  124. var n = 0;
  125. Scheduler.Default.SchedulePeriodic(TimeSpan.Zero, () =>
  126. {
  127. var state = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("state");
  128. if (n++ == 10)
  129. {
  130. state.Value.Set();
  131. }
  132. });
  133. });
  134. e.Value.WaitOne();
  135. }
  136. [Fact]
  137. public void StartPeriodicTimer_Fast_Cancel()
  138. {
  139. var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) };
  140. _domain.SetData("set_cancel", e);
  141. Run(() =>
  142. {
  143. var n = 0;
  144. var hasAtLeastOneValue = new ManualResetEvent(false);
  145. var schedule = Scheduler.Default.SchedulePeriodic(TimeSpan.Zero, () =>
  146. {
  147. _domain.SetData("value", n++);
  148. hasAtLeastOneValue.Set();
  149. });
  150. _domain.SetData("cancel", new MarshalByRefAction(schedule.Dispose));
  151. hasAtLeastOneValue.WaitOne();
  152. var setCancel = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("set_cancel");
  153. setCancel.Value.Set();
  154. });
  155. e.Value.WaitOne();
  156. var value = (int)_domain.GetData("value");
  157. var cancel = (MarshalByRefAction)_domain.GetData("cancel");
  158. cancel.Invoke();
  159. Thread.Sleep(TimeSpan.FromMilliseconds(50));
  160. var newValue = (int)_domain.GetData("value");
  161. Assert.True(newValue >= value);
  162. Thread.Sleep(TimeSpan.FromMilliseconds(50));
  163. value = (int)_domain.GetData("value");
  164. Assert.Equal(newValue, value);
  165. }
  166. [Fact]
  167. public void CreateThread()
  168. {
  169. var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) };
  170. _domain.SetData("state", e);
  171. var r = new MarshalByRefCell<string> { Value = "" };
  172. _domain.SetData("res", r);
  173. Run(() =>
  174. {
  175. var state = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("state");
  176. var res = (MarshalByRefCell<string>)_domain.GetData("res");
  177. var svc = (IServiceProvider)Scheduler.Default;
  178. var per = (ISchedulerPeriodic)svc.GetService(typeof(ISchedulerPeriodic));
  179. if (per == null)
  180. {
  181. res.Value = "Failed to get ISchedulerPeriodic.";
  182. state.Value.Set();
  183. return;
  184. }
  185. var slr = (ISchedulerLongRunning)svc.GetService(typeof(ISchedulerLongRunning));
  186. if (slr == null)
  187. {
  188. res.Value = "Failed to get ISchedulerLongRunning.";
  189. state.Value.Set();
  190. return;
  191. }
  192. var success = false;
  193. try
  194. {
  195. slr.ScheduleLongRunning(42, null);
  196. }
  197. catch (ArgumentNullException)
  198. {
  199. success = true;
  200. }
  201. if (!success)
  202. {
  203. res.Value = "Failed null check ScheduleLongRunning.";
  204. state.Value.Set();
  205. return;
  206. }
  207. state.Value.Set();
  208. #if !NO_THREAD
  209. var w = new ManualResetEvent(false);
  210. var d = slr.ScheduleLongRunning(cancel =>
  211. {
  212. while (!cancel.IsDisposed)
  213. {
  214. ;
  215. }
  216. w.Set();
  217. });
  218. Thread.Sleep(50);
  219. d.Dispose();
  220. w.WaitOne();
  221. #else
  222. state.Value.Set();
  223. #endif
  224. });
  225. e.Value.WaitOne();
  226. Assert.True(string.IsNullOrEmpty(r.Value));
  227. }
  228. [Fact]
  229. public void Cant_Locate_Scheduler()
  230. {
  231. Cant_Locate_Scheduler_NoPlib();
  232. }
  233. [MethodImpl(MethodImplOptions.NoInlining)]
  234. private void Cant_Locate_Scheduler_NoPlib()
  235. {
  236. var e = new MarshalByRefCell<bool>();
  237. _domain.SetData("state", e);
  238. Run(() =>
  239. {
  240. var state = (MarshalByRefCell<bool>)_domain.GetData("state");
  241. try
  242. {
  243. state.Value = Scheduler.TaskPool != null;
  244. }
  245. catch (Exception)
  246. {
  247. state.Value = false;
  248. }
  249. });
  250. Assert.True(e.Value);
  251. }
  252. #if !NO_PERF
  253. [Fact]
  254. public void Stopwatch()
  255. {
  256. var e = new MarshalByRefCell<bool>();
  257. _domain.SetData("state", e);
  258. Run(() =>
  259. {
  260. var state = (MarshalByRefCell<bool>)_domain.GetData("state");
  261. var sw = Scheduler.Default.StartStopwatch();
  262. var fst = sw.Elapsed;
  263. Thread.Sleep(100);
  264. var snd = sw.Elapsed;
  265. state.Value = snd > fst;
  266. });
  267. Assert.True(e.Value);
  268. }
  269. #endif
  270. [Fact]
  271. public void EnsureLoaded()
  272. {
  273. Assert.True(EnlightenmentProvider.EnsureLoaded());
  274. }
  275. }
  276. public class MarshalByRefCell<T> : MarshalByRefObject
  277. {
  278. public T Value;
  279. public override object InitializeLifetimeService()
  280. {
  281. return null;
  282. }
  283. }
  284. public class MarshalByRefAction : MarshalByRefObject
  285. {
  286. private readonly Action _action;
  287. public MarshalByRefAction(Action action)
  288. {
  289. _action = action;
  290. }
  291. public void Invoke()
  292. {
  293. _action();
  294. }
  295. public override object InitializeLifetimeService()
  296. {
  297. return null;
  298. }
  299. }
  300. }
  301. #endif