1
0

DefaultConcurrencyAbstractionLayerTest.cs 10 KB

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