public async Task WritingNotifiesCallbackWhenHostHasClosed()
        {
            var wroteMessage = new TaskCompletionSource <Unit>();
            var hostClosed   = new AutoResetEvent(false);
            var exception    = new TaskCompletionSource <Exception>();
            var disposed     = false;
            var hostTask     = Pipe.Host(_pipeName);
            var task         = Task.Run(() =>
            {
                using (_pipeName.BeginWritingMessages("name", e => exception.SetResult(e), Observable.Create <IBinaryMessage>(obs =>
                {
                    obs.OnNext(IntMessage.Compose(42));
                    wroteMessage.SetResult(Unit.Default);
                    hostClosed.WaitOne();
                    obs.OnNext(IntMessage.Compose(43));
                    return(Disposable.Create(() => disposed = true));
                }))){}
            });
            var host = await hostTask;
            await wroteMessage.Task;

            using (var reader = new BinaryReader(host))
            {
                var msg = IntMessage.TryParse(BinaryMessage.ReadFrom(reader));
                Assert.AreEqual(42, msg.Value);
            }
            Assert.IsFalse(exception.Task.IsCompleted);
            host.Close();
            hostClosed.Set();
            await task;

            Assert.IsNotNull(await exception.Task);
            Assert.IsTrue(disposed);
        }
Beispiel #2
0
 public async Task CanWriteAMessage()
 {
     using (var stream = new MemoryStream())
     {
         Exception exception       = null;
         var       message         = IntMessage.Compose(42);
         var       notifyingStream = new NotifyingStream(stream, (int)LengthOf(message));
         using (notifyingStream.BeginWritingMessages("name", e => exception = e, Observable.Return(message)))
         {
             await notifyingStream.Task;
             var msg = IntMessage.TryParse(MessageFromStream(stream));
             Assert.AreEqual(42, msg.Value);
             Assert.IsNull(exception);
         }
     }
 }