public async Task DispatchMessageEventWithoutHandler()
        {
            // Setup: Create a JSON RPC host without a request handler
            var jh = new JsonRpcHost(GetChannelBase(null, null).Object);

            // If: I dispatch a request that doesn't have a handler
            // Then: I should get an exception
            await Assert.ThrowsAsync <MethodHandlerDoesNotExistException>(() => jh.DispatchMessage(CommonObjects.EventMessage));
        }
        public async Task DispatchMessageEventException()
        {
            // Setup: Create a JSON RPC host with a request handler that throws an unhandled exception every time
            var jh          = new JsonRpcHost(GetChannelBase(null, null).Object);
            var mockHandler = new Mock <Func <CommonObjects.TestMessageContents, EventContext, Task> >();

            mockHandler.Setup(f => f(It.IsAny <CommonObjects.TestMessageContents>(), It.IsAny <EventContext>()))
            .Returns(Task.FromException(new Exception()));
            jh.SetAsyncEventHandler(CommonObjects.EventType, mockHandler.Object);

            // If: I dispatch a message whose handler throws
            // Then: I should get an exception
            await Assert.ThrowsAsync <Exception>(() => jh.DispatchMessage(CommonObjects.EventMessage));
        }
        public async Task DispatchMessageResponseWithHandler()
        {
            // Setup: Create a new JSON RPC host that has a pending request handler
            var jh = new JsonRpcHost(GetChannelBase(null, null).Object);
            var mockPendingRequest = new TaskCompletionSource <Message>();

            jh.pendingRequests.TryAdd(CommonObjects.MessageId, mockPendingRequest);

            // If: I dispatch a response
            await jh.DispatchMessage(CommonObjects.ResponseMessage);

            // Then: The task completion source should have completed with the message that was given
            await mockPendingRequest.Task.WithTimeout(TimeSpan.FromSeconds(1));

            Assert.Equal(CommonObjects.ResponseMessage, mockPendingRequest.Task.Result);
        }
        public async Task DispatchMessageEventWithHandler(Task result)
        {
            // Setup: Create a JSON RPC host with an event handler setup
            var jh          = new JsonRpcHost(GetChannelBase(null, null).Object);
            var mockHandler = new Mock <Func <CommonObjects.TestMessageContents, EventContext, Task> >();

            mockHandler.Setup(f => f(
                                  It.Is <CommonObjects.TestMessageContents>(m => m == CommonObjects.TestMessageContents.DefaultInstance),
                                  It.Is <EventContext>(ec => ec.messageQueue == jh.outputQueue)
                                  )).Returns(result);
            jh.SetAsyncEventHandler(CommonObjects.EventType, mockHandler.Object);

            // If: I dispatch an event
            await jh.DispatchMessage(CommonObjects.EventMessage);

            // Then: The event handler should have been called
            mockHandler.Verify(f => f(
                                   It.Is <CommonObjects.TestMessageContents>(m => m == CommonObjects.TestMessageContents.DefaultInstance),
                                   It.IsAny <EventContext>()
                                   ));
        }