SubscribeOnTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Reactive.Concurrency;
  6. using System.Reactive.Linq;
  7. using System.Threading;
  8. using Microsoft.Reactive.Testing;
  9. using ReactiveTests.Dummies;
  10. using Xunit;
  11. #if HAS_DISPATCHER
  12. using System.Windows.Threading;
  13. using System.Reactive;
  14. using System.Reactive.Subjects;
  15. #endif
  16. #if HAS_WINFORMS
  17. using System.Windows.Forms;
  18. #endif
  19. namespace ReactiveTests.Tests
  20. {
  21. public class SubscribeOnTest : TestBase
  22. {
  23. #region + TestBase +
  24. [Fact]
  25. public void SubscribeOn_ArgumentChecking()
  26. {
  27. var someObservable = Observable.Empty<int>();
  28. #if HAS_WINFORMS
  29. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.SubscribeOn<int>(default(IObservable<int>), new ControlScheduler(new Label())));
  30. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.SubscribeOn<int>(someObservable, default(ControlScheduler)));
  31. ReactiveAssert.Throws<ArgumentNullException>(() => ControlObservable.SubscribeOn<int>(default(IObservable<int>), new Label()));
  32. ReactiveAssert.Throws<ArgumentNullException>(() => ControlObservable.SubscribeOn<int>(someObservable, default(Label)));
  33. #endif
  34. #if HAS_DISPATCHER
  35. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.SubscribeOn<int>(default(IObservable<int>), new DispatcherScheduler(Dispatcher.CurrentDispatcher)));
  36. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.SubscribeOn<int>(someObservable, default(DispatcherScheduler)));
  37. ReactiveAssert.Throws<ArgumentNullException>(() => DispatcherObservable.SubscribeOn<int>(default(IObservable<int>), Dispatcher.CurrentDispatcher));
  38. ReactiveAssert.Throws<ArgumentNullException>(() => DispatcherObservable.SubscribeOn<int>(someObservable, default(Dispatcher)));
  39. ReactiveAssert.Throws<ArgumentNullException>(() => DispatcherObservable.SubscribeOnDispatcher<int>(default(IObservable<int>)));
  40. #endif
  41. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.SubscribeOn<int>(default, new SynchronizationContext()));
  42. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.SubscribeOn(someObservable, default(SynchronizationContext)));
  43. }
  44. #if HAS_WINFORMS
  45. [Fact]
  46. public void SubscribeOn_Control()
  47. {
  48. var lbl = CreateLabel();
  49. var evt2 = new ManualResetEvent(false);
  50. var evt = new ManualResetEvent(false);
  51. bool okay = true;
  52. var d = Observable.Create<int>(obs =>
  53. {
  54. lbl.Text = "Subscribe";
  55. okay &= (SynchronizationContext.Current is System.Windows.Forms.WindowsFormsSynchronizationContext);
  56. evt2.Set();
  57. return () =>
  58. {
  59. lbl.Text = "Unsubscribe";
  60. okay &= (SynchronizationContext.Current is System.Windows.Forms.WindowsFormsSynchronizationContext);
  61. evt.Set();
  62. };
  63. })
  64. .SubscribeOn(lbl)
  65. .Subscribe(_ => {});
  66. evt2.WaitOne();
  67. d.Dispose();
  68. evt.WaitOne();
  69. Application.Exit();
  70. Assert.True(okay);
  71. }
  72. [Fact]
  73. public void SubscribeOn_ControlScheduler()
  74. {
  75. var lbl = CreateLabel();
  76. var evt2 = new ManualResetEvent(false);
  77. var evt = new ManualResetEvent(false);
  78. bool okay = true;
  79. var d = Observable.Create<int>(obs =>
  80. {
  81. lbl.Text = "Subscribe";
  82. okay &= (SynchronizationContext.Current is System.Windows.Forms.WindowsFormsSynchronizationContext);
  83. evt2.Set();
  84. return () =>
  85. {
  86. lbl.Text = "Unsubscribe";
  87. okay &= (SynchronizationContext.Current is System.Windows.Forms.WindowsFormsSynchronizationContext);
  88. evt.Set();
  89. };
  90. })
  91. .SubscribeOn(new ControlScheduler(lbl))
  92. .Subscribe(_ => { });
  93. evt2.WaitOne();
  94. d.Dispose();
  95. evt.WaitOne();
  96. Application.Exit();
  97. Assert.True(okay);
  98. }
  99. private Label CreateLabel()
  100. {
  101. var loaded = new ManualResetEvent(false);
  102. var lbl = default(Label);
  103. var t = new Thread(() =>
  104. {
  105. lbl = new Label();
  106. var frm = new Form { Controls = { lbl }, Width = 0, Height = 0, FormBorderStyle = FormBorderStyle.None, ShowInTaskbar = false };
  107. frm.Load += (_, __) =>
  108. {
  109. loaded.Set();
  110. };
  111. Application.Run(frm);
  112. });
  113. t.SetApartmentState(ApartmentState.STA);
  114. t.Start();
  115. loaded.WaitOne();
  116. return lbl;
  117. }
  118. #endif
  119. #if HAS_DISPATCHER
  120. [Fact]
  121. [Asynchronous]
  122. public void SubscribeOn_Dispatcher()
  123. {
  124. var dispatcher = DispatcherHelpers.EnsureDispatcher();
  125. RunAsync(evt =>
  126. {
  127. var s = new AsyncSubject<Unit>();
  128. bool okay = true;
  129. var d = Observable.Create<int>(obs =>
  130. {
  131. okay &= (SynchronizationContext.Current is System.Windows.Threading.DispatcherSynchronizationContext);
  132. s.OnNext(Unit.Default);
  133. s.OnCompleted();
  134. return () =>
  135. {
  136. okay &= (SynchronizationContext.Current is System.Windows.Threading.DispatcherSynchronizationContext);
  137. Assert.True(okay);
  138. dispatcher.InvokeShutdown();
  139. evt.Set();
  140. };
  141. })
  142. .SubscribeOn(dispatcher)
  143. .Subscribe(_ => { });
  144. s.Subscribe(_ => d.Dispose());
  145. });
  146. }
  147. [Fact]
  148. [Asynchronous]
  149. public void SubscribeOn_DispatcherScheduler()
  150. {
  151. var dispatcher = DispatcherHelpers.EnsureDispatcher();
  152. RunAsync(evt =>
  153. {
  154. var s = new AsyncSubject<Unit>();
  155. bool okay = true;
  156. var d = Observable.Create<int>(obs =>
  157. {
  158. okay &= (SynchronizationContext.Current is System.Windows.Threading.DispatcherSynchronizationContext);
  159. s.OnNext(Unit.Default);
  160. s.OnCompleted();
  161. return () =>
  162. {
  163. okay &= (SynchronizationContext.Current is System.Windows.Threading.DispatcherSynchronizationContext);
  164. Assert.True(okay);
  165. dispatcher.InvokeShutdown();
  166. evt.Set();
  167. };
  168. })
  169. .SubscribeOn(new DispatcherScheduler(dispatcher))
  170. .Subscribe(_ => { });
  171. s.Subscribe(_ => d.Dispose());
  172. });
  173. }
  174. [Fact]
  175. [Asynchronous]
  176. public void SubscribeOn_CurrentDispatcher()
  177. {
  178. var dispatcher = DispatcherHelpers.EnsureDispatcher();
  179. RunAsync(evt =>
  180. {
  181. var s = new AsyncSubject<Unit>();
  182. bool okay = true;
  183. dispatcher.BeginInvoke(new Action(() =>
  184. {
  185. var d = Observable.Create<int>(obs =>
  186. {
  187. okay &= (SynchronizationContext.Current is System.Windows.Threading.DispatcherSynchronizationContext);
  188. s.OnNext(Unit.Default);
  189. s.OnCompleted();
  190. return () =>
  191. {
  192. okay &= (SynchronizationContext.Current is System.Windows.Threading.DispatcherSynchronizationContext);
  193. Assert.True(okay);
  194. dispatcher.InvokeShutdown();
  195. evt.Set();
  196. };
  197. })
  198. .SubscribeOnDispatcher()
  199. .Subscribe(_ => { });
  200. s.Subscribe(_ => d.Dispose());
  201. }));
  202. });
  203. }
  204. #endif
  205. #endregion + TestBase +
  206. }
  207. public class SubscribeOnReactiveTest : ReactiveTest
  208. {
  209. [Fact]
  210. public void SubscribeOn_Scheduler_ArgumentChecking()
  211. {
  212. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.SubscribeOn(default(IObservable<int>), DummyScheduler.Instance));
  213. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.SubscribeOn(DummyObservable<int>.Instance, default(IScheduler)));
  214. }
  215. [Fact]
  216. public void SubscribeOn_Scheduler_Sleep()
  217. {
  218. var scheduler = new TestScheduler();
  219. var s = 0L;
  220. var d = 0L;
  221. var xs = Observable.Create<long>(observer =>
  222. {
  223. s = scheduler.Clock;
  224. return () => d = scheduler.Clock;
  225. });
  226. var results = scheduler.Start(() =>
  227. xs.SubscribeOn(scheduler)
  228. );
  229. results.Messages.AssertEqual(
  230. );
  231. Assert.Equal(201, s);
  232. Assert.Equal(1001, d);
  233. }
  234. [Fact]
  235. public void SubscribeOn_Scheduler_Completed()
  236. {
  237. var scheduler = new TestScheduler();
  238. var xs = scheduler.CreateHotObservable(
  239. OnCompleted<long>(300)
  240. );
  241. var results = scheduler.Start(() =>
  242. xs.SubscribeOn(scheduler)
  243. );
  244. results.Messages.AssertEqual(
  245. OnCompleted<long>(300)
  246. );
  247. xs.Subscriptions.AssertEqual(
  248. Subscribe(201, 301)
  249. );
  250. }
  251. [Fact]
  252. public void SubscribeOn_Scheduler_Error()
  253. {
  254. var scheduler = new TestScheduler();
  255. var ex = new Exception();
  256. var xs = scheduler.CreateHotObservable(
  257. OnError<int>(300, ex)
  258. );
  259. var results = scheduler.Start(() =>
  260. xs.SubscribeOn(scheduler)
  261. );
  262. results.Messages.AssertEqual(
  263. OnError<int>(300, ex)
  264. );
  265. xs.Subscriptions.AssertEqual(
  266. Subscribe(201, 301)
  267. );
  268. }
  269. [Fact]
  270. public void SubscribeOn_Scheduler_Dispose()
  271. {
  272. var scheduler = new TestScheduler();
  273. var xs = scheduler.CreateHotObservable<int>(
  274. );
  275. var results = scheduler.Start(() =>
  276. xs.SubscribeOn(scheduler)
  277. );
  278. results.Messages.AssertEqual(
  279. );
  280. xs.Subscriptions.AssertEqual(
  281. Subscribe(201, 1001)
  282. );
  283. }
  284. [Fact]
  285. public void SubscribeOn_SynchronizationContext_Simple()
  286. {
  287. var scheduler = new TestScheduler();
  288. var xs = scheduler.CreateHotObservable(
  289. OnNext(90, 1),
  290. OnNext(120, 2),
  291. OnNext(230, 3),
  292. OnNext(240, 4),
  293. OnNext(310, 5),
  294. OnNext(470, 6),
  295. OnCompleted<int>(530)
  296. );
  297. var results = scheduler.Start(() =>
  298. xs.SubscribeOn(new MyCtx(scheduler))
  299. );
  300. results.Messages.AssertEqual(
  301. OnNext(230, 3),
  302. OnNext(240, 4),
  303. OnNext(310, 5),
  304. OnNext(470, 6),
  305. OnCompleted<int>(530)
  306. );
  307. xs.Subscriptions.AssertEqual(
  308. Subscribe(201, 531)
  309. );
  310. }
  311. }
  312. }