public async Task WritingNotifiesCallbackWhenHostHasCrashed()
        {
            var hostCrashed = new AutoResetEvent(false);
            var exception   = new TaskCompletionSource <Exception>();
            var disposed    = false;
            var p           = RunTestProcess("NotifiesCallbackWhenHostCrashes " + _pipeName);

            Thread.Sleep(1000);
            var task = Task.Run(() =>
            {
                using (_pipeName.BeginWritingMessages("name", e => exception.SetResult(e), Observable.Create <IBinaryMessage>(obs =>
                {
                    obs.OnNext(IntMessage.Compose(42));
                    hostCrashed.WaitOne();
                    obs.OnNext(IntMessage.Compose(43));
                    return(Disposable.Create(() => disposed = true));
                }))){}
            });

            Thread.Sleep(1000);             //Give writing thread time to connect, if we kill the host too early, Connect() will block
            Assert.IsFalse(exception.Task.IsCompleted);
            p.Kill();
            p.WaitForExit();
            hostCrashed.Set();
            await task;

            Assert.IsNotNull(await exception.Task);
            Assert.IsTrue(disposed);
        }
Exemple #2
0
 public async Task CanReadAMessage()
 {
     using (var inbox = StreamWithMessages(IntMessage.Compose(42)))
     {
         Assert.AreEqual(42, await inbox.ReadMessages("name").SelectSome(IntMessage.TryParse).FirstAsync());
     }
 }
        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);
        }
 IObservable <IBinaryMessage> OneMessageThenError(int i, Exception error, Action dispose)
 {
     return(Observable.Create <IBinaryMessage>(obs =>
     {
         obs.OnNext(IntMessage.Compose(i));
         obs.OnError(error);
         return dispose;
     }));
 }
Exemple #5
0
        public async Task NotifiesOnWriteError()
        {
            var stream = new CantDoAnythingStream();
            var source = new TaskCompletionSource <Unit>();

            using (stream.BeginWritingMessages("name",
                                               e =>
            {
                source.SetResult(Unit.Default);
            },
                                               Observable.Return(IntMessage.Compose(42))))
            {
                await source.Task;
            }
        }
Exemple #6
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);
         }
     }
 }
        public async Task CompletesAtClosedPipe()
        {
            WriteMessagesToCurrentPipe(IntMessage.Compose(42));
            int result    = -1;
            var completed = new TaskCompletionSource <Exception>();

            using (_pipeName.ReadMessages("name").RefCount().SelectSome(IntMessage.TryParse)
                   .Subscribe(
                       i => result = i,
                       e => completed.SetResult(e),
                       () => completed.SetResult(null)))
            {
                var exception = await completed.Task;
                Assert.AreEqual(42, result);
                Assert.IsNull(exception);
            }
        }
Exemple #8
0
 public async Task CompletesAtEndOfStream()
 {
     using (var inbox = StreamWithMessages(IntMessage.Compose(42)))
     {
         int result    = -1;
         var completed = new TaskCompletionSource <Exception>();
         using (inbox.ReadMessages("name").SelectSome(IntMessage.TryParse)
                .Subscribe(
                    i => result = i,
                    e => completed.SetResult(e),
                    () => completed.SetResult(null)))
         {
             var exception = await completed.Task;
             Assert.AreEqual(42, result);
             Assert.IsNull(exception);
         }
     }
 }
Exemple #9
0
 public async Task ErrorsAtOtherExceptions()
 {
     using (var messages = StreamWithMessages(IntMessage.Compose(42)))
         using (var closingStream = new ReadThenThrowStream(messages, new Exception("Alderaan destroyed")))
         {
             int result    = -1;
             var completed = new TaskCompletionSource <Exception>();
             using (closingStream.ReadMessages("name").SelectSome(IntMessage.TryParse)
                    .Subscribe(
                        i => result = i,
                        e => completed.SetResult(e),
                        () => completed.SetResult(null)))
             {
                 var exception = await completed.Task;
                 Assert.AreEqual(42, result);
                 Assert.AreEqual("Alderaan destroyed", exception.Message);
             }
         }
 }
Exemple #10
0
 public async Task CompletesAtClosedConnection()
 {
     using (var messages = StreamWithMessages(IntMessage.Compose(42)))
         using (var closingStream = new ReadThenThrowStream(messages, new IOException("", _forciblyClosedConnection)))
         {
             int result    = -1;
             var completed = new TaskCompletionSource <Exception>();
             using (closingStream.ReadMessages("name").SelectSome(IntMessage.TryParse)
                    .Subscribe(
                        i => result = i,
                        e => completed.SetResult(e),
                        () => completed.SetResult(null)))
             {
                 var exception = await completed.Task;
                 Assert.AreEqual(42, result);
                 Assert.IsNull(exception);
             }
         }
 }
        //Characterization test, not sure if this is the desired behaviour
        public async Task CompletesWhenClientCrashes()
        {
            var messageToSend = GetMessageAsBase64ForUseInTestProcess(IntMessage.Compose(42));             //Paste this in the testprocess if the format changes
            var result        = new TaskCompletionSource <int>();
            var completed     = new TaskCompletionSource <Exception>();
            var p             = RunTestProcess("CompletesWhenClientCrashes " + _pipeName);

            using (_pipeName.ReadMessages("name").RefCount().SelectSome(IntMessage.TryParse)
                   .Subscribe(
                       i => result.SetResult(i),
                       e => completed.SetResult(e),
                       () => completed.SetResult(null)))
            {
                await result.Task;
                p.Kill();
                p.WaitForExit();
                Console.WriteLine();
                var exception = await completed.Task;
                Assert.IsNull(exception);
            }
        }
 public async Task CanReadAMessage()
 {
     WriteMessagesToCurrentPipe(IntMessage.Compose(42));
     Assert.AreEqual(42, await _pipeName.ReadMessages("name").RefCount().SelectSome(IntMessage.TryParse).FirstAsync());
 }
        [Ignore("This is currently not the case, maybe it should be?")]         //Just wrote this as an experiment, and left it in in case we want it later
        public async Task ReaderCompletesWhenWriterCompletes()
        {
            Exception writeError = null;
            Exception readError  = null;
            var       result     = -1;
            var       completed  = new TaskCompletionSource <Unit>();

            using (_pipeName.BeginWritingMessages("name", e => { writeError = e; }, Observable.Return(IntMessage.Compose(42))))
            {
                using (_pipeName.ReadMessages("name").RefCount().SelectSome(IntMessage.TryParse)
                       .Subscribe(
                           i => result = i,
                           e => readError = e,
                           () => completed.SetResult(Unit.Default)))
                {
                    await completed.Task;
                    Assert.AreEqual(42, result);
                    Assert.IsNull(writeError);
                    Assert.IsNull(readError);
                }
            }
        }
        public async Task CanCommunicate()
        {
            Exception error = null;

            using (_pipeName.BeginWritingMessages("name", e => { error = e; }, Observable.Return(IntMessage.Compose(42))))
            {
                var msg = await _pipeName.ReadMessages("name").RefCount().SelectSome(IntMessage.TryParse).FirstAsync();

                Assert.AreEqual(42, msg);
                Assert.IsNull(error);
            }
        }