Skip to content

Commit

Permalink
Fix InvalidOperationException when combining ReplaySubject and Take()…
Browse files Browse the repository at this point in the history
… (issue neuecc#292)
  • Loading branch information
reluctantmedia committed Jun 23, 2020
1 parent 6ce6639 commit 740fef7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Assets/Plugins/UniRx/Scripts/Subjects/ReplaySubject.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UniRx.InternalUtil;

namespace UniRx
Expand Down Expand Up @@ -167,7 +168,7 @@ public IDisposable Subscribe(IObserver<T> observer)

ex = lastError;
Trim();
foreach (var item in queue)
foreach (var item in queue.ToList())
{
observer.OnNext(item.Value);
}
Expand Down
34 changes: 34 additions & 0 deletions Assets/Scripts/UnityTests/Rx/SubjectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,5 +477,39 @@ public void ReplaySubjectWindowReplay()
subject.Subscribe(x => onNext.Add(x), x => exception.Add(x), () => onCompletedCallCount++);
onNext.Is(10000, 2, 20);
}

///<Summary>
/// Regression test for previous InvalidOperationException
/// UniRx issue <a href="https://github.com/neuecc/UniRx/issues/292">#292</a>
///</Summary>
[Test]
public void ReplaySubject_Take_GivesCorrectResult()
{
var subject = new ReplaySubject<int>(1);

var onNext = new List<int>();

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);
}
}
}

0 comments on commit 740fef7

Please sign in to comment.