public void TestAsyncWriteExceptions() { var buffer = new byte [3]; using (var stream = new ExceptionalStream()) { var task = stream.WriteAsync(buffer, 0, buffer.Length); Assert.AreEqual(TaskStatus.RanToCompletion, task.Status, "#1"); stream.Throw = true; task = stream.WriteAsync(buffer, 0, buffer.Length); Assert.IsTrue(task.IsFaulted, "#2"); Assert.AreEqual(ExceptionalStream.Message, task.Exception.InnerException.Message, "#3"); } }
public void TestAsyncArgumentExceptions() { var buffer = new byte [3]; using (var stream = new ExceptionalStream()) { var task = stream.WriteAsync(buffer, 0, buffer.Length); Assert.IsTrue(task.IsCompleted); Assert.IsTrue(Throws <ArgumentException> (() => { stream.WriteAsync(buffer, 0, 1000); }), "#2"); Assert.IsTrue(Throws <ArgumentException> (() => { stream.ReadAsync(buffer, 0, 1000); }), "#3"); Assert.IsTrue(Throws <ArgumentException> (() => { stream.WriteAsync(buffer, 0, 1000, new CancellationToken(true)); }), "#4"); Assert.IsTrue(Throws <ArgumentException> (() => { stream.ReadAsync(buffer, 0, 1000, new CancellationToken(true)); }), "#5"); Assert.IsTrue(Throws <ArgumentException> (() => { stream.WriteAsync(null, 0, buffer.Length, new CancellationToken(true)); }), "#6"); Assert.IsTrue(Throws <ArgumentException> (() => { stream.ReadAsync(null, 0, buffer.Length, new CancellationToken(true)); }), "#7"); Assert.IsTrue(Throws <ArgumentException> (() => { stream.WriteAsync(buffer, 1000, buffer.Length, new CancellationToken(true)); }), "#8"); Assert.IsTrue(Throws <ArgumentException> (() => { stream.ReadAsync(buffer, 1000, buffer.Length, new CancellationToken(true)); }), "#9"); stream.AllowRead = false; var read_task = stream.ReadAsync(buffer, 0, buffer.Length); Assert.AreEqual(TaskStatus.RanToCompletion, read_task.Status, "#8"); Assert.AreEqual(0, read_task.Result, "#9"); stream.Position = 0; read_task = stream.ReadAsync(buffer, 0, buffer.Length); Assert.AreEqual(TaskStatus.RanToCompletion, read_task.Status, "#9"); Assert.AreEqual(3, read_task.Result, "#10"); var write_task = stream.WriteAsync(buffer, 0, buffer.Length); Assert.AreEqual(TaskStatus.RanToCompletion, write_task.Status, "#10"); // test what happens when CanRead is overridden using (var norm = new ExceptionalStream(buffer, false)) { write_task = norm.WriteAsync(buffer, 0, buffer.Length); Assert.AreEqual(TaskStatus.RanToCompletion, write_task.Status, "#11"); } stream.AllowWrite = false; Assert.IsTrue(Throws <NotSupportedException> (() => { stream.Write(buffer, 0, buffer.Length); }), "#12"); write_task = stream.WriteAsync(buffer, 0, buffer.Length); Assert.AreEqual(TaskStatus.Faulted, write_task.Status, "#13"); } }