private async Task Start(IObserver <byte[]> observer, CancellationToken token) { var isException = false; try { var length = new byte[sizeof(int)]; using (var stream = streamFactory.CreateStream(Address)) { await stream.ConnectionAsync(token); using (publishSubject.Subscribe(async message => await SendMessageAsync(stream, message, token))) { while (!token.IsCancellationRequested) { var count = await stream.ReadAsync(length, 0, length.Length, token); if (count <= 0 || token.IsCancellationRequested) { return; } var messageLength = BitConverter.ToInt32(length, 0); var message = new byte[messageLength]; count = await stream.ReadAsync(message, 0, messageLength, token); if (count != messageLength || token.IsCancellationRequested) { return; } observer.OnNext(message); } } } } catch (TaskCanceledException) { } catch (Exception ex) { isException = true; publishSubject.OnError(ex); throw; } finally { if (!isException && !token.IsCancellationRequested) { var processTerminatedException = new SandboxTerminatedException(); publishSubject.OnError(processTerminatedException); throw processTerminatedException; } } }
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 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 Observable_Error_Other() { 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.OnError(new InvalidOperationException()); Assert.False(cs1.HasObserver()); Assert.False(cs2.HasObserver()); to.AssertFailure(typeof(InvalidOperationException)); }
public void Error_Main_Delayed() { var count = 0; var us = new UnicastSubject <int>(); var cs = new CompletableSubject(); var to = us .ConcatMap(v => { if (v == 2) { return(cs); } return(CompletableSource.FromAction(() => count++)); }, true) .Test(); to.AssertEmpty() .AssertSubscribed(); us.OnNext(0); us.OnNext(1); us.OnNext(2); us.OnError(new InvalidOperationException()); Assert.AreEqual(2, count); to.AssertEmpty(); Assert.True(cs.HasObserver()); Assert.AreEqual(2, count); cs.OnCompleted(); to.AssertFailure(typeof(InvalidOperationException)); Assert.AreEqual(2, count); }
public void Error() { 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.OnError(new InvalidOperationException()); Assert.False(us2.HasObserver()); to.AssertFailure(typeof(InvalidOperationException)); }
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 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); }