From 740fef71312ec8a0f6518739c6b9ba1a06ea1916 Mon Sep 17 00:00:00 2001 From: Ian Date: Tue, 23 Jun 2020 11:47:20 +0200 Subject: [PATCH] Fix InvalidOperationException when combining ReplaySubject and Take() (issue #292) --- .../UniRx/Scripts/Subjects/ReplaySubject.cs | 3 +- Assets/Scripts/UnityTests/Rx/SubjectTest.cs | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/Assets/Plugins/UniRx/Scripts/Subjects/ReplaySubject.cs b/Assets/Plugins/UniRx/Scripts/Subjects/ReplaySubject.cs index 59d0cb69..4d8d0911 100644 --- a/Assets/Plugins/UniRx/Scripts/Subjects/ReplaySubject.cs +++ b/Assets/Plugins/UniRx/Scripts/Subjects/ReplaySubject.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using UniRx.InternalUtil; namespace UniRx @@ -167,7 +168,7 @@ public IDisposable Subscribe(IObserver observer) ex = lastError; Trim(); - foreach (var item in queue) + foreach (var item in queue.ToList()) { observer.OnNext(item.Value); } diff --git a/Assets/Scripts/UnityTests/Rx/SubjectTest.cs b/Assets/Scripts/UnityTests/Rx/SubjectTest.cs index cfbddf8c..b741582e 100644 --- a/Assets/Scripts/UnityTests/Rx/SubjectTest.cs +++ b/Assets/Scripts/UnityTests/Rx/SubjectTest.cs @@ -477,5 +477,39 @@ public void ReplaySubjectWindowReplay() subject.Subscribe(x => onNext.Add(x), x => exception.Add(x), () => onCompletedCallCount++); onNext.Is(10000, 2, 20); } + + /// + /// Regression test for previous InvalidOperationException + /// UniRx issue #292 + /// + [Test] + public void ReplaySubject_Take_GivesCorrectResult() + { + var subject = new ReplaySubject(1); + + var onNext = new List(); + + subject.OnNext(1); + + var _ = subject + .Take(1) + .Do(subject.OnNext) + .Subscribe(onNext.Add); + + onNext.Is(1); + + _.Dispose(); + onNext.Clear(); + + subject.OnNext(2); + subject.OnNext(3); + + _ = subject + .Take(1) + .Do(subject.OnNext) + .Subscribe(onNext.Add); + + onNext.Is(3); + } } }