StartTest.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Reactive;
  10. using System.Reactive.Concurrency;
  11. using System.Reactive.Linq;
  12. using Microsoft.Reactive.Testing;
  13. using Xunit;
  14. using ReactiveTests.Dummies;
  15. using System.Reflection;
  16. namespace ReactiveTests.Tests
  17. {
  18. public class StartTest : ReactiveTest
  19. {
  20. [Fact]
  21. public void Start_ArgumentChecking()
  22. {
  23. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.Start(null));
  24. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.Start<int>((Func<int>)null));
  25. var someScheduler = new TestScheduler();
  26. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.Start(null, someScheduler));
  27. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.Start<int>(null, someScheduler));
  28. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.Start(() => { }, null));
  29. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.Start<int>(() => 1, null));
  30. }
  31. [Fact]
  32. public void Start_Action()
  33. {
  34. bool done = false;
  35. Assert.True(Observable.Start(() => { done = true; }).ToEnumerable().SequenceEqual(new[] { new Unit() }));
  36. Assert.True(done, "done");
  37. }
  38. [Fact]
  39. public void Start_Action2()
  40. {
  41. var scheduler = new TestScheduler();
  42. bool done = false;
  43. var res = scheduler.Start(() =>
  44. Observable.Start(() => { done = true; }, scheduler)
  45. );
  46. res.Messages.AssertEqual(
  47. OnNext(200, new Unit()),
  48. OnCompleted<Unit>(200)
  49. );
  50. Assert.True(done, "done");
  51. }
  52. [Fact]
  53. public void Start_ActionError()
  54. {
  55. var ex = new Exception();
  56. var res = Observable.Start(() => { throw ex; }).Materialize().ToEnumerable();
  57. Assert.True(res.SequenceEqual(new[] {
  58. Notification.CreateOnError<Unit>(ex)
  59. }));
  60. }
  61. [Fact]
  62. public void Start_Func()
  63. {
  64. var res = Observable.Start(() => 1).ToEnumerable();
  65. Assert.True(res.SequenceEqual(new[] {
  66. 1
  67. }));
  68. }
  69. [Fact]
  70. public void Start_Func2()
  71. {
  72. var scheduler = new TestScheduler();
  73. var res = scheduler.Start(() =>
  74. Observable.Start(() => 1, scheduler)
  75. );
  76. res.Messages.AssertEqual(
  77. OnNext(200, 1),
  78. OnCompleted<int>(200)
  79. );
  80. }
  81. [Fact]
  82. public void Start_FuncError()
  83. {
  84. var ex = new Exception();
  85. var res = Observable.Start<int>(() => { throw ex; }).Materialize().ToEnumerable();
  86. Assert.True(res.SequenceEqual(new[] {
  87. Notification.CreateOnError<int>(ex)
  88. }));
  89. }
  90. }
  91. }