public void When_ErrorHandler_Is_Not_Given_Any_Error_Is_Immediately_Rethrown() { var stm1 = Substitute.For <Stream>(); var stm2 = Substitute.For <Stream>(); stm2.WriteAsync(Arg.Any <byte[]>(), Arg.Any <int>(), Arg.Any <int>(), Arg.Any <CancellationToken>()) .Returns(x => throw new Exception("Test")); stm1.CanWrite.Returns(true); stm2.CanWrite.Returns(true); using (var instance = new BroadcastStream(new PushFuncStream(stm1, true, CancellationToken.None), stm2, true, null)) { var bytes = new byte[0]; var ex = Assert.Throws <Exception>(() => instance.Write(bytes, 0, 0)); Assert.AreEqual(ex.Message, "Error during Concurrent streaming."); ex = ex.InnerException; Assert.NotNull(ex); Assert.AreEqual(ex.Message, "Test"); } }
// we keep internal extensions here internal static Func <PushFuncStream, Task> ApplyConcurrentStream(this Func <PushFuncStream, Task> pipe, Stream stream, bool disposeStream, Action <Stream, Exception> errorHandler) { return(async pfs => { #if NETFRAMEWORK || NETSTANDARD2_0 using (var concurrentStream = new BroadcastStream(pfs, stream, disposeStream, errorHandler)) #else var concurrentStream = new BroadcastStream(pfs, stream, disposeStream, errorHandler); await using (concurrentStream.ConfigureAwait(false)) #endif { var t = pfs.Token; await pipe(new PushFuncStream(concurrentStream, false, t)).StartIfNeeded() .ConfigureAwait(false); await concurrentStream.FlushAsync(t).ConfigureAwait(false); } }); }