StartTest.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.Linq;
  6. using System.Reactive;
  7. using System.Reactive.Linq;
  8. using System.Threading;
  9. using Microsoft.Reactive.Testing;
  10. using Microsoft.VisualStudio.TestTools.UnitTesting;
  11. using Assert = Xunit.Assert;
  12. namespace ReactiveTests.Tests
  13. {
  14. [TestClass]
  15. public class StartTest : ReactiveTest
  16. {
  17. [TestMethod]
  18. public void Start_ArgumentChecking()
  19. {
  20. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.Start(null));
  21. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.Start<int>(null));
  22. var someScheduler = new TestScheduler();
  23. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.Start(null, someScheduler));
  24. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.Start<int>(null, someScheduler));
  25. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.Start(() => { }, null));
  26. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.Start(() => 1, null));
  27. }
  28. [TestMethod]
  29. public void Start_Action()
  30. {
  31. var done = false;
  32. Assert.True(Observable.Start(() => { done = true; }).ToEnumerable().SequenceEqual([new Unit()]));
  33. Assert.True(done, "done");
  34. }
  35. [TestMethod]
  36. public void Start_Action2()
  37. {
  38. var scheduler = new TestScheduler();
  39. var done = false;
  40. var res = scheduler.Start(() =>
  41. Observable.Start(() => { done = true; }, scheduler)
  42. );
  43. res.Messages.AssertEqual(
  44. OnNext(200, new Unit()),
  45. OnCompleted<Unit>(200)
  46. );
  47. Assert.True(done, "done");
  48. }
  49. [TestMethod]
  50. public void Start_ActionError()
  51. {
  52. var ex = new Exception();
  53. var res = Observable.Start(() => { throw ex; }).Materialize().ToEnumerable();
  54. Assert.True(res.SequenceEqual([
  55. Notification.CreateOnError<Unit>(ex)
  56. ]));
  57. }
  58. [TestMethod]
  59. public void Start_ActionErrorAfterUnsubscribeDoesNotThrow()
  60. {
  61. var ex = new Exception();
  62. using Barrier gate = new(2);
  63. var sub = Observable.Start(
  64. () =>
  65. {
  66. // 1: action running
  67. gate.SignalAndWait();
  68. // 2: unsubscribe Dispose returned
  69. gate.SignalAndWait();
  70. throw ex;
  71. })
  72. .Subscribe();
  73. // 1: action running
  74. gate.SignalAndWait();
  75. sub.Dispose();
  76. // 2: unsubscribe Dispose returned
  77. gate.SignalAndWait();
  78. }
  79. [TestMethod]
  80. public void Start_Func()
  81. {
  82. var res = Observable.Start(() => 1).ToEnumerable();
  83. Assert.True(res.SequenceEqual([
  84. 1
  85. ]));
  86. }
  87. [TestMethod]
  88. public void Start_Func2()
  89. {
  90. var scheduler = new TestScheduler();
  91. var res = scheduler.Start(() =>
  92. Observable.Start(() => 1, scheduler)
  93. );
  94. res.Messages.AssertEqual(
  95. OnNext(200, 1),
  96. OnCompleted<int>(200)
  97. );
  98. }
  99. [TestMethod]
  100. public void Start_FuncError()
  101. {
  102. var ex = new Exception();
  103. var res = Observable.Start<int>(() => { throw ex; }).Materialize().ToEnumerable();
  104. Assert.True(res.SequenceEqual([
  105. Notification.CreateOnError<int>(ex)
  106. ]));
  107. }
  108. }
  109. }