NotificationTest.cs 18 KB

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