public void Disposed() { var us = new UnicastSubject <int>(); var to = new TestObserverLocal(); bool[] disposed = { false }; var d = us.ConcatMap(v => new OnDispose <int>(Enumerable.Range(v * 100, 5), disposed)) .Subscribe(to); to.OnSubscribe(d); Assert.True(us.HasObserver()); to.AssertEmpty(); us.OnNext(1); to.AssertValuesOnly(100, 101, 102, 103, 104); Assert.True(us.HasObserver()); us.OnNext(2); Assert.False(us.HasObserver()); Assert.True(disposed[0]); to.AssertResult(100, 101, 102, 103, 104, 200, 201); }
public void Error() { var count = 0; var us = new UnicastSubject <int>(); var to = us .ConcatMap(v => { if (v == 2) { return(CompletableSource.Error(new InvalidOperationException())); } return(CompletableSource.FromAction(() => count++)); }) .Test(); to.AssertEmpty() .AssertSubscribed(); us.OnNext(0); us.OnNext(1); us.OnNext(2); to.AssertFailure(typeof(InvalidOperationException)); Assert.False(us.HasObserver()); Assert.AreEqual(2, count); }
public void Mapper_Crash() { var us1 = new UnicastSubject <int>(); var us2 = new UnicastSubject <int>(); var to = ReactiveExtensions.CombineLatest(a => { int v = 0; foreach (var e in a) { v += e; } if (v == 22) { throw new InvalidOperationException(); } return(v); }, us1, us2).Test(); to.AssertEmpty(); us1.OnNext(1); us2.OnNext(10); us1.OnNext(2); us2.OnNext(20); Assert.False(us1.HasObserver()); Assert.False(us2.HasObserver()); to.AssertFailure(typeof(InvalidOperationException), 11, 12); }
public void Switch() { var s = new UnicastSubject <ICompletableSource>(); var cs1 = new CompletableSubject(); var cs2 = new CompletableSubject(); var cs3 = new CompletableSubject(); var to = s .SwitchMap(v => v) .Test(); to.AssertEmpty(); Assert.False(cs1.HasObserver()); Assert.False(cs2.HasObserver()); Assert.False(cs3.HasObserver()); s.OnNext(cs1); Assert.True(cs1.HasObserver()); Assert.False(cs2.HasObserver()); Assert.False(cs3.HasObserver()); cs1.OnCompleted(); Assert.False(cs2.HasObserver()); Assert.False(cs3.HasObserver()); s.OnNext(cs2); Assert.True(cs2.HasObserver()); Assert.False(cs3.HasObserver()); s.OnNext(cs3); Assert.False(cs2.HasObserver()); Assert.True(cs3.HasObserver()); to.AssertEmpty(); s.OnCompleted(); to.AssertEmpty(); cs3.OnCompleted(); to.AssertResult(); }
public void Error_Delayed() { var us1 = new UnicastSubject <int>(); var us2 = new UnicastSubject <int>(); var to = ReactiveExtensions.CombineLatest(a => { int v = 0; foreach (var e in a) { v += e; } return(v); }, true, us1, us2).Test(); to.AssertEmpty(); us1.OnNext(1); us1.OnError(new InvalidOperationException()); Assert.True(us2.HasObserver()); us2.EmitAll(10, 20, 30, 40, 50); to.AssertFailure(typeof(InvalidOperationException), 11, 21, 31, 41, 51); }
public void Dispose() { var us = new UnicastSubject <int>(); var n0 = Thread.CurrentThread.Name; var cdl = new CountdownEvent(1); var n1 = default(string); var to = us.DoOnDispose(() => { n1 = Thread.CurrentThread.Name; cdl.Signal(); }) .UnsubscribeOn(NewThreadScheduler.Default) .Test(); us.OnNext(1); to.AssertValuesOnly(1); Assert.Null(n1); to.Dispose(); Assert.True(cdl.Wait(TimeSpan.FromSeconds(5))); Assert.AreNotEqual(n0, n1); }
public void Race_While() { for (int i = 0; i < TestHelper.RACE_LOOPS; i++) { var us = new UnicastSubject <int>(); var to = new TestObserver <int>(); TestHelper.Race(() => { for (int j = 0; j < 1000; j++) { us.OnNext(j); } us.OnCompleted(); }, () => { us.BlockingSubscribeWhile(v => { to.OnNext(v); return(true); }, to.OnError, to.OnCompleted); }); to.AssertValueCount(1000) .AssertNoError() .AssertCompleted(); } }
public void Race() { for (int i = 0; i < TestHelper.RACE_LOOPS; i++) { var us = new UnicastSubject <int>(); var to = new TestObserver <int>(); TestHelper.Race(() => { for (int j = 0; j < 1000; j++) { us.OnNext(j); } us.OnCompleted(); }, () => { foreach (var v in us.BlockingEnumerable()) { to.OnNext(v); } to.OnCompleted(); }); to.AssertValueCount(1000) .AssertNoError() .AssertCompleted(); } }
public void Complete() { var us = new UnicastSubject <int>(); var n0 = Thread.CurrentThread.ManagedThreadId; var cdl = new CountdownEvent(1); var n1 = default(int); var to = us.DoOnDispose(() => { n1 = Thread.CurrentThread.ManagedThreadId; cdl.Signal(); }) .UnsubscribeOn(NewThreadScheduler.Default) .Test(); us.OnNext(1); us.OnCompleted(); to.AssertResult(1); Assert.True(cdl.Wait(TimeSpan.FromSeconds(5))); Assert.AreNotEqual(n0, n1); }
public void Error() { var us = new UnicastSubject <int>(); var n0 = Thread.CurrentThread.ManagedThreadId; var cdl = new CountdownEvent(1); var n1 = default(int); var to = us.DoOnDispose(() => { n1 = Thread.CurrentThread.ManagedThreadId; cdl.Signal(); }) .UnsubscribeOn(NewThreadScheduler.Default) .Test(); us.OnNext(1); us.OnError(new InvalidOperationException()); to.AssertFailure(typeof(InvalidOperationException), 1); Assert.True(cdl.Wait(TimeSpan.FromSeconds(5))); Assert.AreNotEqual(n0, n1); }
public void Basic() { var us1 = new UnicastSubject <int>(); var us2 = new UnicastSubject <int>(); var to = ReactiveExtensions.CombineLatest(a => { int v = 0; foreach (var e in a) { v += e; } return(v); }, us1, us2).Test(); to.AssertEmpty(); us1.OnNext(1); to.AssertEmpty(); us1.OnNext(2); to.AssertEmpty(); us2.OnNext(10); to.AssertValuesOnly(12); us2.OnNext(20); to.AssertValuesOnly(12, 22); us2.OnCompleted(); to.AssertValuesOnly(12, 22); us1.OnNext(3); to.AssertValuesOnly(12, 22, 23); us1.OnCompleted(); to.AssertResult(12, 22, 23); }
public void Race_DelayErrors() { for (int i = 0; i < TestHelper.RACE_LOOPS; i++) { var us1 = new UnicastSubject <int>(); var us2 = new UnicastSubject <int>(); var to = new[] { us1, us2 }.CombineLatest(a => { int v = 0; foreach (var e in a) { v += e; } return(v); }, true).Test(); us1.OnNext(0); us2.OnNext(1000); TestHelper.Race(() => { for (int j = 1; j < 1000; j++) { us1.OnNext(j); } us1.OnCompleted(); }, () => { for (int j = 1001; j < 2000; j++) { us2.OnNext(j); } us2.OnCompleted(); }); to.AssertValueCount(1999) .AssertNoError() .AssertCompleted(); } }
public void Error_Second_Source() { var us1 = new UnicastSubject <int>(); var us2 = new UnicastSubject <int>(); var to = ReactiveExtensions.Zip(a => { int s = 0; foreach (var v in a) { s += v; } return(s); }, us1, us2 ).Test(); us1.OnNext(1); to.AssertEmpty(); us1.OnNext(2); to.AssertEmpty(); us2.OnNext(10); to.AssertValuesOnly(11); us2.OnError(new InvalidOperationException()); Assert.False(us1.HasObserver()); to.AssertFailure(typeof(InvalidOperationException), 11); }
public void Observable_Basic_Other_OnNext() { var cs1 = new SingleSubject <int>(); var cs2 = new UnicastSubject <int>(); var to = cs1 .TakeUntil(cs2) .Test(); Assert.True(cs1.HasObserver()); Assert.True(cs2.HasObserver()); cs2.OnNext(1); Assert.False(cs1.HasObserver()); Assert.False(cs2.HasObserver()); to.AssertFailure(typeof(IndexOutOfRangeException)); }
public void Observable_Basic_Other_OnNext() { var cs1 = new CompletableSubject(); var cs2 = new UnicastSubject <int>(); var to = cs1 .TakeUntil(cs2) .Test(); Assert.True(cs1.HasObserver()); Assert.True(cs2.HasObserver()); cs2.OnNext(1); Assert.False(cs1.HasObserver()); Assert.False(cs2.HasObserver()); to.AssertResult(); }
public void Race_DelayErrors() { for (int i = 0; i < TestHelper.RACE_LOOPS; i++) { var us1 = new UnicastSubject <int>(); var us2 = new UnicastSubject <int>(); var to = new[] { us1, us2 }.Zip(a => { int v = 0; foreach (var e in a) { v += e; } return(v); }, true).Test(); TestHelper.Race(() => { for (int j = 0; j < 1000; j++) { us1.OnNext(j); } us1.OnCompleted(); }, () => { for (int j = 1000; j < 2000; j++) { us2.OnNext(j); } us2.OnCompleted(); }); to.AssertValueCount(1000) .AssertNoError() .AssertCompleted(); var list = to.Items; for (int j = 0; j < 1000; j++) { Assert.AreEqual(j + (1000 + j), list[j]); } } }
public void OnNext_Race() { for (int i = 0; i < TestHelper.RACE_LOOPS; i++) { var us = new UnicastSubject <int>().ToSerialized(); var to = us.Test(); Action emit = () => { for (int j = 0; j < 500; j++) { us.OnNext(j); } }; TestHelper.Race(emit, emit); to.AssertValueCount(1000); } }
public void Race_With_Error() { var exc = new InvalidOperationException(); for (int i = 0; i < TestHelper.RACE_LOOPS; i++) { var us = new UnicastSubject <int>(); var to = new TestObserver <int>(); TestHelper.Race(() => { for (int j = 0; j < 1000; j++) { us.OnNext(j); } us.OnError(exc); }, () => { try { foreach (var v in us.BlockingEnumerable()) { to.OnNext(v); } } catch (Exception ex) { to.OnError(ex); } }); to.AssertValueCount(1000) .AssertError(typeof(InvalidOperationException)) .AssertNotCompleted(); } }
public void Publish(byte[] message) { publishSubject.OnNext(message); }