FirstOrDefaultTest.cs 4.0 KB

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