Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Fix ReplaySubject InvalidOperationException #462

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
41 changes: 38 additions & 3 deletions Assets/Scripts/UnityTests/Rx/SubjectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace UniRx.Tests
{

public class SubjectTests
public class SubjectTest
{
[Test]
public void Subject()
Expand Down Expand Up @@ -401,6 +401,7 @@ public void ReplaySubject()
}
}

[Test]
public void ReplaySubjectBufferReplay()
{
var subject = new ReplaySubject<int>(bufferSize: 3);
Expand All @@ -415,7 +416,7 @@ public void ReplaySubjectBufferReplay()
subject.OnNext(100);
subject.OnNext(1000);
subject.OnNext(10000);
onNext.Is(100, 1000, 10000); // cut 1, 10
onNext.Is(1, 10, 100, 1000, 10000);

// replay subscription
onNext.Clear();
Expand All @@ -424,7 +425,7 @@ public void ReplaySubjectBufferReplay()
onNext.Is(100, 1000, 10000);

subject.OnNext(20000);
onNext.Is(1000, 10000, 20000);
onNext.Is(100, 1000, 10000, 20000);

subject.OnCompleted();
onCompletedCallCount.Is(1);
Expand Down Expand Up @@ -476,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);
}
}
}