FirstOrDefaultTest.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.Concurrency;
  7. using System.Reactive.Disposables;
  8. using System.Reactive.Linq;
  9. using System.Threading;
  10. using Microsoft.Reactive.Testing;
  11. using ReactiveTests.Dummies;
  12. using Microsoft.VisualStudio.TestTools.UnitTesting;
  13. using Assert = Xunit.Assert;
  14. namespace ReactiveTests.Tests
  15. {
  16. [TestClass]
  17. public class FirstOrDefaultTest : ReactiveTest
  18. {
  19. [TestMethod]
  20. public void FirstOrDefault_ArgumentChecking()
  21. {
  22. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.FirstOrDefault(default(IObservable<int>)));
  23. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.FirstOrDefault(default(IObservable<int>), _ => true));
  24. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.FirstOrDefault(DummyObservable<int>.Instance, default));
  25. }
  26. [TestMethod]
  27. public void FirstOrDefault_Empty()
  28. {
  29. Assert.Equal(default, Observable.Empty<int>().FirstOrDefault());
  30. }
  31. [TestMethod]
  32. public void FirstOrDefaultPredicate_Empty()
  33. {
  34. Assert.Equal(default, Observable.Empty<int>().FirstOrDefault(_ => true));
  35. }
  36. [TestMethod]
  37. public void FirstOrDefault_Return()
  38. {
  39. var value = 42;
  40. Assert.Equal(value, Observable.Return(value).FirstOrDefault());
  41. }
  42. [TestMethod]
  43. public void FirstOrDefault_Throw()
  44. {
  45. var ex = new Exception();
  46. var xs = Observable.Throw<int>(ex);
  47. ReactiveAssert.Throws(ex, () => xs.FirstOrDefault());
  48. }
  49. [TestMethod]
  50. public void FirstOrDefault_Range()
  51. {
  52. var value = 42;
  53. Assert.Equal(value, Observable.Range(value, 10).FirstOrDefault());
  54. }
  55. #if !NO_THREAD
  56. [TestMethod]
  57. public void FirstOrDefault_NoDoubleSet()
  58. {
  59. //
  60. // Regression test for a possible race condition caused by Return style operators
  61. // that could trigger two Set calls on a ManualResetEvent, causing it to get
  62. // disposed in between those two calls (cf. FirstOrDefaultInternal). This led
  63. // to an exception will the following stack trace:
  64. //
  65. // System.ObjectDisposedException: Safe handle has been closed
  66. // at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)
  67. // at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)
  68. // at Microsoft.Win32.Win32Native.SetEvent(SafeWaitHandle handle)
  69. // at System.Threading.EventWaitHandle.Set()
  70. // at System.Reactive.Linq.QueryLanguage.<>c__DisplayClass458_1`1.<FirstOrDefaultInternal>b__2()
  71. //
  72. var o = new O();
  73. Scheduler.Default.Schedule(() =>
  74. {
  75. var x = o.FirstOrDefault();
  76. });
  77. o.Wait();
  78. o.Next();
  79. Thread.Sleep(100); // enough time to let the ManualResetEvent dispose
  80. o.Done();
  81. }
  82. #endif
  83. private class O : IObservable<int>
  84. {
  85. private readonly ManualResetEvent _event = new ManualResetEvent(false);
  86. private IObserver<int> _observer;
  87. public void Wait()
  88. {
  89. _event.WaitOne();
  90. }
  91. public void Next()
  92. {
  93. _observer.OnNext(42);
  94. }
  95. public void Done()
  96. {
  97. _observer.OnCompleted();
  98. }
  99. public IDisposable Subscribe(IObserver<int> observer)
  100. {
  101. _observer = observer;
  102. _event.Set();
  103. return Disposable.Empty;
  104. }
  105. }
  106. }
  107. }