public async Task ConsumeInputRequestException()
        {
            // Setup:
            // ... Create a message reader that will return a request
            var mr = new Mock <MessageReader>(Stream.Null, null);

            mr.SetupSequence(o => o.ReadMessage())
            .ReturnsAsync(CommonObjects.RequestMessage)
            .Returns(Task.FromException <Message>(new EndOfStreamException()));

            // ... Create a JSON RPC host and register the request handler to throw exception
            var jh          = new JsonRpcHost(GetChannelBase(mr.Object, null).Object);
            var mockHandler = new Mock <Action <CommonObjects.TestMessageContents, RequestContext <CommonObjects.TestMessageContents> > >();

            mockHandler.Setup(m => m(It.IsAny <CommonObjects.TestMessageContents>(),
                                     It.IsAny <RequestContext <CommonObjects.TestMessageContents> >()))
            .Throws(new Exception());
            jh.SetRequestHandler(CommonObjects.RequestType, mockHandler.Object);

            // If: I start the input consumption loop
            await jh.ConsumeInput().WithTimeout(TimeSpan.FromSeconds(1));

            // Then:
            // ... Read message should have been called twice
            mr.Verify(o => o.ReadMessage(), Times.Exactly(2));

            // ... There should not be any outgoing messages
            var outgoing = jh.outputQueue.ToArray();

            Assert.Empty(outgoing);
        }
        public async Task SetSyncRequestHandler(bool nullContents)
        {
            // Setup: Create a mock request handler
            var requestHandler = new Mock <Action <CommonObjects.TestMessageContents, RequestContext <CommonObjects.TestMessageContents> > >();
            var message        = nullContents
                ? Message.CreateRequest(CommonObjects.RequestType, CommonObjects.MessageId, null)
                : CommonObjects.RequestMessage;

            // If: I assign a request handler on the JSON RPC host
            var jh = new JsonRpcHost(GetChannelBase(null, null).Object);

            jh.SetRequestHandler(CommonObjects.RequestType, requestHandler.Object);

            // Then: It should be the only request handler set
            Assert.Single(jh.requestHandlers);
            Assert.Contains(CommonObjects.RequestType.MethodName, jh.requestHandlers.Keys);

            // If: I call the stored request handler
            await jh.requestHandlers[CommonObjects.RequestType.MethodName](message);
            await jh.requestHandlers[CommonObjects.RequestType.MethodName](message);

            // Then: The request handler should have been called with the params and a proper request context
            var expectedContents = nullContents
                ? null
                : CommonObjects.TestMessageContents.DefaultInstance;

            requestHandler.Verify(a => a(
                                      It.Is <CommonObjects.TestMessageContents>(p => p == expectedContents),
                                      It.Is <RequestContext <CommonObjects.TestMessageContents> >(rc => rc.messageQueue == jh.outputQueue && rc.requestMessage == message)
                                      ), Times.Exactly(2));
        }
        public void SetSyncRequestHandlerNullRequestHandler()
        {
            // If: I assign a request handler on the JSON RPC host with a null request handler
            // Then: I should get an exception
            var jh = new JsonRpcHost(GetChannelBase(null, null).Object);

            Assert.Throws <ArgumentNullException>(() => jh.SetRequestHandler(CommonObjects.RequestType, null));
        }