1
0

NotificationTest.cs 18 KB

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