NotificationTest.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. using System;
  5. using System.Reactive;
  6. using System.Threading;
  7. using Microsoft.Reactive.Testing;
  8. using Xunit;
  9. namespace ReactiveTests.Tests
  10. {
  11. public class NotificationTest : ReactiveTest
  12. {
  13. #region ToObservable
  14. [Fact]
  15. public void ToObservable_ArgumentChecking()
  16. {
  17. ReactiveAssert.Throws<ArgumentNullException>(() => Notification.CreateOnNext(1).ToObservable(null));
  18. }
  19. [Fact]
  20. public void ToObservable_Empty()
  21. {
  22. var scheduler = new TestScheduler();
  23. var res = scheduler.Start(() =>
  24. Notification.CreateOnCompleted<int>().ToObservable(scheduler)
  25. );
  26. res.Messages.AssertEqual(
  27. OnCompleted<int>(201)
  28. );
  29. }
  30. [Fact]
  31. public void ToObservable_Return()
  32. {
  33. var scheduler = new TestScheduler();
  34. var res = scheduler.Start(() =>
  35. Notification.CreateOnNext(42).ToObservable(scheduler)
  36. );
  37. res.Messages.AssertEqual(
  38. OnNext(201, 42),
  39. OnCompleted<int>(201)
  40. );
  41. }
  42. [Fact]
  43. public void ToObservable_Throw()
  44. {
  45. var ex = new Exception();
  46. var scheduler = new TestScheduler();
  47. var res = scheduler.Start(() =>
  48. Notification.CreateOnError<int>(ex).ToObservable(scheduler)
  49. );
  50. res.Messages.AssertEqual(
  51. OnError<int>(201, ex)
  52. );
  53. }
  54. [Fact]
  55. public void ToObservable_CurrentThread()
  56. {
  57. var evt = new ManualResetEvent(false);
  58. Notification.CreateOnCompleted<int>().ToObservable().Subscribe(_ => { }, () =>
  59. {
  60. evt.Set();
  61. });
  62. evt.WaitOne();
  63. }
  64. #endregion
  65. [Fact]
  66. public void Notifications_Equality()
  67. {
  68. var n = Notification.CreateOnNext(42);
  69. var e = Notification.CreateOnError<int>(new Exception());
  70. var c = Notification.CreateOnCompleted<int>();
  71. var n1 = n; var n2 = n;
  72. var e1 = e; var e2 = e;
  73. var c1 = c; var c2 = c;
  74. Assert.True(n1 == n2);
  75. Assert.True(e1 == e2);
  76. Assert.True(c1 == c2);
  77. Assert.True(n1.Equals(n2));
  78. Assert.True(e1.Equals(e2));
  79. Assert.True(c1.Equals(c2));
  80. }
  81. [Fact]
  82. public void OnNext_CtorAndProps()
  83. {
  84. var n = Notification.CreateOnNext(42);
  85. Assert.Equal(NotificationKind.OnNext, n.Kind);
  86. Assert.True(n.HasValue);
  87. Assert.Equal(42, n.Value);
  88. Assert.Null(n.Exception);
  89. }
  90. [Fact]
  91. public void OnNext_Equality()
  92. {
  93. var n1 = Notification.CreateOnNext(42);
  94. var n2 = Notification.CreateOnNext(42);
  95. var n3 = Notification.CreateOnNext(24);
  96. var n4 = Notification.CreateOnCompleted<int>();
  97. Assert.True(n1.Equals(n1));
  98. Assert.True(n1.Equals(n2));
  99. Assert.True(n2.Equals(n1));
  100. Assert.False(n1.Equals(null));
  101. Assert.False(n1.Equals(""));
  102. Assert.False(n1.Equals(n3));
  103. Assert.False(n3.Equals(n1));
  104. Assert.False(n1.Equals(n4));
  105. Assert.False(n4.Equals(n1));
  106. Assert.True(n1 == n2);
  107. Assert.True(n2 == n1);
  108. Assert.False(n1 == null);
  109. Assert.False(null == n1);
  110. Assert.True(!(n1 != n2));
  111. Assert.True(!(n2 != n1));
  112. Assert.False(!(n1 != null));
  113. Assert.False(!(null != n1));
  114. }
  115. [Fact]
  116. public void OnNext_GetHashCode()
  117. {
  118. var n1 = Notification.CreateOnNext(42);
  119. var n2 = Notification.CreateOnNext(42);
  120. Assert.NotEqual(0, n1.GetHashCode());
  121. Assert.Equal(n1.GetHashCode(), n2.GetHashCode());
  122. }
  123. [Fact]
  124. public void OnNext_ToString()
  125. {
  126. var n1 = Notification.CreateOnNext(42);
  127. Assert.True(n1.ToString().Contains("OnNext"));
  128. Assert.True(n1.ToString().Contains(42.ToString()));
  129. }
  130. [Fact]
  131. public void OnNext_AcceptObserver()
  132. {
  133. var con = new CheckOnNextObserver();
  134. var n1 = Notification.CreateOnNext(42);
  135. n1.Accept(con);
  136. Assert.Equal(42, con.Value);
  137. }
  138. private class CheckOnNextObserver : IObserver<int>
  139. {
  140. public void OnNext(int value)
  141. {
  142. Value = value;
  143. }
  144. public int Value { get; private set; }
  145. public void OnError(Exception exception)
  146. {
  147. throw new NotImplementedException();
  148. }
  149. public void OnCompleted()
  150. {
  151. throw new NotImplementedException();
  152. }
  153. }
  154. [Fact]
  155. public void OnNext_AcceptObserverWithResult()
  156. {
  157. var n1 = Notification.CreateOnNext(42);
  158. var res = n1.Accept(new AcceptObserver(x => "OK", _ => { Assert.True(false); return null; }, () => { Assert.True(false); return null; }));
  159. Assert.Equal("OK", res);
  160. }
  161. [Fact]
  162. public void OnNext_AcceptObserverWithResult_Null()
  163. {
  164. var n1 = Notification.CreateOnNext(42);
  165. ReactiveAssert.Throws<ArgumentNullException>(() => n1.Accept(default(IObserver<int, string>)));
  166. }
  167. [Fact]
  168. public void OnNext_AcceptAction()
  169. {
  170. var obs = false;
  171. var n1 = Notification.CreateOnNext(42);
  172. n1.Accept(x => { obs = true; }, _ => { Assert.True(false); }, () => { Assert.True(false); });
  173. Assert.True(obs);
  174. }
  175. [Fact]
  176. public void OnNext_Accept_ArgumentChecking()
  177. {
  178. var n = Notification.CreateOnNext(42);
  179. ReactiveAssert.Throws<ArgumentNullException>(() => n.Accept(default));
  180. ReactiveAssert.Throws<ArgumentNullException>(() => n.Accept(default, ex => { }, () => { }));
  181. ReactiveAssert.Throws<ArgumentNullException>(() => n.Accept(x => { }, default, () => { }));
  182. ReactiveAssert.Throws<ArgumentNullException>(() => n.Accept(x => { }, ex => { }, default));
  183. ReactiveAssert.Throws<ArgumentNullException>(() => n.Accept(default, ex => "", () => ""));
  184. ReactiveAssert.Throws<ArgumentNullException>(() => n.Accept(x => "", default, () => ""));
  185. ReactiveAssert.Throws<ArgumentNullException>(() => n.Accept(x => "", ex => "", default));
  186. }
  187. [Fact]
  188. public void OnNext_AcceptActionWithResult()
  189. {
  190. var n1 = Notification.CreateOnNext(42);
  191. var res = n1.Accept(x => "OK", _ => { Assert.True(false); return null; }, () => { Assert.True(false); return null; });
  192. Assert.Equal("OK", res);
  193. }
  194. [Fact]
  195. public void OnError_CtorNull()
  196. {
  197. ReactiveAssert.Throws<ArgumentNullException>(() => Notification.CreateOnError<int>(null));
  198. }
  199. [Fact]
  200. public void OnError_Accept_ArgumentChecking()
  201. {
  202. var n = Notification.CreateOnError<int>(new Exception());
  203. ReactiveAssert.Throws<ArgumentNullException>(() => n.Accept(default));
  204. ReactiveAssert.Throws<ArgumentNullException>(() => n.Accept(default, ex => { }, () => { }));
  205. ReactiveAssert.Throws<ArgumentNullException>(() => n.Accept(x => { }, default, () => { }));
  206. ReactiveAssert.Throws<ArgumentNullException>(() => n.Accept(x => { }, ex => { }, default));
  207. ReactiveAssert.Throws<ArgumentNullException>(() => n.Accept(default, ex => "", () => ""));
  208. ReactiveAssert.Throws<ArgumentNullException>(() => n.Accept(x => "", default, () => ""));
  209. ReactiveAssert.Throws<ArgumentNullException>(() => n.Accept(x => "", ex => "", default));
  210. }
  211. [Fact]
  212. public void OnError_CtorAndProps()
  213. {
  214. var e = new Exception();
  215. var n = Notification.CreateOnError<int>(e);
  216. Assert.Equal(NotificationKind.OnError, n.Kind);
  217. Assert.False(n.HasValue);
  218. Assert.Same(e, n.Exception);
  219. try
  220. {
  221. var x = n.Value;
  222. Assert.True(false);
  223. }
  224. catch (Exception _e)
  225. {
  226. Assert.Same(e, _e);
  227. }
  228. }
  229. [Fact]
  230. public void OnError_Equality()
  231. {
  232. var ex1 = new Exception();
  233. var ex2 = new Exception();
  234. var n1 = Notification.CreateOnError<int>(ex1);
  235. var n2 = Notification.CreateOnError<int>(ex1);
  236. var n3 = Notification.CreateOnError<int>(ex2);
  237. var n4 = Notification.CreateOnCompleted<int>();
  238. Assert.True(n1.Equals(n1));
  239. Assert.True(n1.Equals(n2));
  240. Assert.True(n2.Equals(n1));
  241. Assert.False(n1.Equals(null));
  242. Assert.False(n1.Equals(""));
  243. Assert.False(n1.Equals(n3));
  244. Assert.False(n3.Equals(n1));
  245. Assert.False(n1.Equals(n4));
  246. Assert.False(n4.Equals(n1));
  247. Assert.True(n1 == n2);
  248. Assert.True(n2 == n1);
  249. Assert.False(n1 == null);
  250. Assert.False(null == n1);
  251. Assert.True(!(n1 != n2));
  252. Assert.True(!(n2 != n1));
  253. Assert.False(!(n1 != null));
  254. Assert.False(!(null != n1));
  255. }
  256. [Fact]
  257. public void OnError_GetHashCode()
  258. {
  259. var ex = new Exception();
  260. var n1 = Notification.CreateOnError<int>(ex);
  261. var n2 = Notification.CreateOnError<int>(ex);
  262. Assert.NotEqual(0, n1.GetHashCode());
  263. Assert.Equal(n1.GetHashCode(), n2.GetHashCode());
  264. }
  265. [Fact]
  266. public void OnError_ToString()
  267. {
  268. var ex = new Exception();
  269. var n1 = Notification.CreateOnError<int>(ex);
  270. Assert.True(n1.ToString().Contains("OnError"));
  271. Assert.True(n1.ToString().Contains(ex.GetType().Name)); // CHECK, no message?
  272. }
  273. [Fact]
  274. public void OnError_AcceptObserver()
  275. {
  276. var ex = new Exception();
  277. var obs = new CheckOnErrorObserver();
  278. var n1 = Notification.CreateOnError<int>(ex);
  279. n1.Accept(obs);
  280. Assert.Same(ex, obs.Error);
  281. }
  282. private class CheckOnErrorObserver : IObserver<int>
  283. {
  284. public void OnNext(int value)
  285. {
  286. throw new NotImplementedException();
  287. }
  288. public Exception Error { get; private set; }
  289. public void OnError(Exception exception)
  290. {
  291. Error = exception;
  292. }
  293. public void OnCompleted()
  294. {
  295. throw new NotImplementedException();
  296. }
  297. }
  298. [Fact]
  299. public void OnError_AcceptObserverWithResult()
  300. {
  301. var ex = new Exception();
  302. var n1 = Notification.CreateOnError<int>(ex);
  303. var res = n1.Accept(new AcceptObserver(x => { Assert.True(false); return null; }, _ => { Assert.Same(ex, _); return "OK"; }, () => { Assert.True(false); return null; }));
  304. Assert.Equal("OK", res);
  305. }
  306. [Fact]
  307. public void OnError_AcceptObserverWithResult_Null()
  308. {
  309. var n1 = Notification.CreateOnError<int>(new Exception());
  310. ReactiveAssert.Throws<ArgumentNullException>(() => n1.Accept(default(IObserver<int, string>)));
  311. }
  312. [Fact]
  313. public void OnError_AcceptAction()
  314. {
  315. var ex = new Exception();
  316. var obs = false;
  317. var n1 = Notification.CreateOnError<int>(ex);
  318. n1.Accept(x => { Assert.True(false); }, _ => { obs = true; }, () => { Assert.True(false); });
  319. Assert.True(obs);
  320. }
  321. [Fact]
  322. public void OnError_AcceptActionWithResult()
  323. {
  324. var ex = new Exception();
  325. var n1 = Notification.CreateOnError<int>(ex);
  326. var res = n1.Accept(x => { Assert.True(false); return null; }, x => "OK", () => { Assert.True(false); return null; });
  327. Assert.Equal("OK", res);
  328. }
  329. [Fact]
  330. public void OnCompleted_CtorAndProps()
  331. {
  332. var n = Notification.CreateOnCompleted<int>();
  333. Assert.Equal(NotificationKind.OnCompleted, n.Kind);
  334. Assert.False(n.HasValue);
  335. Assert.Null(n.Exception);
  336. var ok = false;
  337. try
  338. {
  339. var x = n.Value;
  340. Assert.True(false);
  341. }
  342. catch (InvalidOperationException)
  343. {
  344. ok = true;
  345. }
  346. Assert.True(ok);
  347. }
  348. [Fact]
  349. public void OnCompleted_Accept_ArgumentChecking()
  350. {
  351. var n = Notification.CreateOnCompleted<int>();
  352. ReactiveAssert.Throws<ArgumentNullException>(() => n.Accept(default));
  353. ReactiveAssert.Throws<ArgumentNullException>(() => n.Accept(default, ex => { }, () => { }));
  354. ReactiveAssert.Throws<ArgumentNullException>(() => n.Accept(x => { }, default, () => { }));
  355. ReactiveAssert.Throws<ArgumentNullException>(() => n.Accept(x => { }, ex => { }, default));
  356. ReactiveAssert.Throws<ArgumentNullException>(() => n.Accept(default, ex => "", () => ""));
  357. ReactiveAssert.Throws<ArgumentNullException>(() => n.Accept(x => "", default, () => ""));
  358. ReactiveAssert.Throws<ArgumentNullException>(() => n.Accept(x => "", ex => "", default));
  359. }
  360. [Fact]
  361. public void OnCompleted_Equality()
  362. {
  363. var n1 = Notification.CreateOnCompleted<int>();
  364. var n2 = Notification.CreateOnCompleted<int>();
  365. var n3 = Notification.CreateOnNext(2);
  366. Assert.True(n1.Equals(n1));
  367. Assert.True(n1.Equals(n2));
  368. Assert.True(n2.Equals(n1));
  369. Assert.False(n1.Equals(null));
  370. Assert.False(n1.Equals(""));
  371. Assert.False(n1.Equals(n3));
  372. Assert.False(n3.Equals(n1));
  373. Assert.True(n1 == n2);
  374. Assert.True(n2 == n1);
  375. Assert.False(n1 == null);
  376. Assert.False(null == n1);
  377. Assert.True(!(n1 != n2));
  378. Assert.True(!(n2 != n1));
  379. Assert.False(!(n1 != null));
  380. Assert.False(!(null != n1));
  381. }
  382. [Fact]
  383. public void OnCompleted_GetHashCode()
  384. {
  385. var n1 = Notification.CreateOnCompleted<int>();
  386. var n2 = Notification.CreateOnCompleted<int>();
  387. Assert.NotEqual(0, n1.GetHashCode());
  388. Assert.Equal(n1.GetHashCode(), n2.GetHashCode());
  389. }
  390. [Fact]
  391. public void OnCompleted_ToString()
  392. {
  393. var n1 = Notification.CreateOnCompleted<int>();
  394. Assert.True(n1.ToString().Contains("OnCompleted"));
  395. }
  396. [Fact]
  397. public void OnCompleted_AcceptObserver()
  398. {
  399. var obs = new CheckOnCompletedObserver();
  400. var n1 = Notification.CreateOnCompleted<int>();
  401. n1.Accept(obs);
  402. Assert.True(obs.Completed);
  403. }
  404. private class CheckOnCompletedObserver : IObserver<int>
  405. {
  406. public void OnNext(int value)
  407. {
  408. throw new NotImplementedException();
  409. }
  410. public bool Completed { get; private set; }
  411. public void OnError(Exception exception)
  412. {
  413. throw new NotImplementedException();
  414. }
  415. public void OnCompleted()
  416. {
  417. Completed = true;
  418. }
  419. }
  420. [Fact]
  421. public void OnCompleted_AcceptObserverWithResult()
  422. {
  423. var n1 = Notification.CreateOnCompleted<int>();
  424. var res = n1.Accept(new AcceptObserver(x => { Assert.True(false); return null; }, _ => { Assert.True(false); return null; }, () => "OK"));
  425. Assert.Equal("OK", res);
  426. }
  427. [Fact]
  428. public void OnCompleted_AcceptObserverWithResult_Null()
  429. {
  430. var n1 = Notification.CreateOnCompleted<int>();
  431. ReactiveAssert.Throws<ArgumentNullException>(() => n1.Accept(default(IObserver<int, string>)));
  432. }
  433. [Fact]
  434. public void OnCompleted_AcceptAction()
  435. {
  436. var obs = false;
  437. var n1 = Notification.CreateOnCompleted<int>();
  438. n1.Accept(x => { Assert.True(false); }, _ => { Assert.True(false); }, () => { obs = true; });
  439. Assert.True(obs);
  440. }
  441. [Fact]
  442. public void OnCompleted_AcceptActionWithResult()
  443. {
  444. var n1 = Notification.CreateOnCompleted<int>();
  445. var res = n1.Accept(x => { Assert.True(false); return null; }, _ => { Assert.True(false); return null; }, () => "OK");
  446. Assert.Equal("OK", res);
  447. }
  448. private class AcceptObserver : IObserver<int, string>
  449. {
  450. private readonly Func<int, string> _onNext;
  451. private readonly Func<Exception, string> _onError;
  452. private readonly Func<string> _onCompleted;
  453. public AcceptObserver(Func<int, string> onNext, Func<Exception, string> onError, Func<string> onCompleted)
  454. {
  455. _onNext = onNext;
  456. _onError = onError;
  457. _onCompleted = onCompleted;
  458. }
  459. public string OnNext(int value)
  460. {
  461. return _onNext(value);
  462. }
  463. public string OnError(Exception exception)
  464. {
  465. return _onError(exception);
  466. }
  467. public string OnCompleted()
  468. {
  469. return _onCompleted();
  470. }
  471. }
  472. }
  473. }