Bläddra i källkod

Add a unit test that asserts that RefCount will Connect properly if a previous subscription terminated synchronously. This test will succeed with the fix from 35757bd0820405deae38c424f4197ed73a1d6dea but will fail on the commit before (0bf8cffdaea38cac91feb28d0a5ba8c78e9815e1).

Daniel Weber 7 år sedan
förälder
incheckning
aa1f4c468e

+ 60 - 0
Rx.NET/Source/tests/Tests.System.Reactive/Tests/Linq/Observable/RefCountTest.cs

@@ -3,8 +3,11 @@
 // See the LICENSE file in the project root for more information. 
 
 using System;
+using System.Reactive;
 using System.Reactive.Concurrency;
+using System.Reactive.Disposables;
 using System.Reactive.Linq;
+using System.Reactive.Subjects;
 using Microsoft.Reactive.Testing;
 using Xunit;
 
@@ -12,6 +15,25 @@ namespace ReactiveTests.Tests
 {
     public class RefCountTest : ReactiveTest
     {
+        private sealed class DematerializingConnectableObservable<T> : IConnectableObservable<T>
+        {
+            private readonly IConnectableObservable<Notification<T>> _subject;
+
+            public DematerializingConnectableObservable(IConnectableObservable<Notification<T>> subject)
+            {
+                _subject = subject;
+            }
+
+            public IDisposable Subscribe(IObserver<T> observer)
+            {
+                return _subject.Dematerialize().Subscribe(observer);
+            }
+
+            public IDisposable Connect()
+            {
+                return _subject.Connect();
+            }
+        }
 
         [Fact]
         public void RefCount_ArgumentChecking()
@@ -177,6 +199,44 @@ namespace ReactiveTests.Tests
             );
         }
 
+        [Fact]
+        public void RefCount_can_connect_again_if_previous_subscription_terminated_synchronously()
+        {
+            var seen = 0;
+            var terminated = false;
+
+            var subject = new ReplaySubject<Notification<int>>(1);
+            var connectable = new DematerializingConnectableObservable<int>(subject.Publish());
+            var refCount = connectable.RefCount();
+
+            subject.OnNext(Notification.CreateOnNext(36));
+
+            using (refCount.Subscribe(value => seen = value, () => terminated = true))
+            {
+                Assert.Equal(36, seen);
+            }
+
+            seen = 0;
+            terminated = false;
+            subject.OnNext(Notification.CreateOnCompleted<int>());
+
+            using (refCount.Subscribe(value => seen = value, () => terminated = true))
+            {
+                Assert.Equal(0, seen);
+                Assert.True(terminated);
+            }
+
+            seen = 0;
+            terminated = false;
+            subject.OnNext(Notification.CreateOnNext(36));
+
+            using (refCount.Subscribe(value => seen = value, () => terminated = true))
+            {
+                Assert.Equal(36, seen);
+                Assert.False(terminated);
+            }
+        }
+        
         [Fact]
         public void LazyRefCount_ArgumentChecking()
         {