StartTest.cs 3.0 KB

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