public async Task SetAsyncEventHandlerOverrideTrue()
        {
            // Setup: Create two mock event handlers
            var eventHandler1 = new Mock <Func <CommonObjects.TestMessageContents, EventContext, Task> >();
            var eventHandler2 = new Mock <Func <CommonObjects.TestMessageContents, EventContext, Task> >();

            // If:
            // ... I assign an event handler on the JSON RPC host
            // ... And I reassign the event handler with an override
            var jh = new JsonRpcHost(GetChannelBase(null, null).Object);

            jh.SetAsyncEventHandler(CommonObjects.EventType, eventHandler1.Object);
            jh.SetAsyncEventHandler(CommonObjects.EventType, eventHandler2.Object, true);

            // Then: There should only be one event handler
            Assert.Single(jh.eventHandlers);
            Assert.Contains(CommonObjects.EventType.MethodName, jh.eventHandlers.Keys);

            // If: I call the stored event handler
            await jh.eventHandlers[CommonObjects.EventType.MethodName](CommonObjects.EventMessage);

            // Then: The correct event handler should have been called
            eventHandler2.Verify(a => a(
                                     It.Is <CommonObjects.TestMessageContents>(p => p.Equals(CommonObjects.TestMessageContents.DefaultInstance)),
                                     It.Is <EventContext>(p => p.messageQueue == jh.outputQueue)
                                     ), Times.Once);
            eventHandler1.Verify(a => a(
                                     It.IsAny <CommonObjects.TestMessageContents>(),
                                     It.IsAny <EventContext>()
                                     ), Times.Never);
        }
        public void SetAsyncEventHandlerOverrideFalse()
        {
            // Setup: Create a mock event handler
            var eventHandler = new Mock <Func <CommonObjects.TestMessageContents, EventContext, Task> >();

            // If:
            // ... I assign an event handler on the JSON RPC host
            // ... And I reassign the event handler without overriding
            // Then: I should get an exception
            var jh = new JsonRpcHost(GetChannelBase(null, null).Object);

            jh.SetAsyncEventHandler(CommonObjects.EventType, eventHandler.Object);
            Assert.ThrowsAny <Exception>(() => jh.SetAsyncEventHandler(CommonObjects.EventType, eventHandler.Object));
        }
        public async Task SetAsyncEventHandler(bool nullContents)
        {
            // Setup: Create a mock request handler
            var eventHandler = new Mock <Func <CommonObjects.TestMessageContents, EventContext, Task> >();
            var message      = nullContents
                ? Message.CreateEvent(CommonObjects.EventType, null)
                : CommonObjects.EventMessage;

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

            jh.SetAsyncEventHandler(CommonObjects.EventType, eventHandler.Object);

            // Then: It should be the only event handler set
            Assert.Single(jh.eventHandlers);
            Assert.Contains(CommonObjects.EventType.MethodName, jh.eventHandlers.Keys);

            // If: I call the stored event handler
            await jh.eventHandlers[CommonObjects.EventType.MethodName](message);
            await jh.eventHandlers[CommonObjects.EventType.MethodName](message);

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

            eventHandler.Verify(a => a(
                                    It.Is <CommonObjects.TestMessageContents>(p => p == expectedContents),
                                    It.Is <EventContext>(p => p.messageQueue == jh.outputQueue)
                                    ), Times.Exactly(2));
        }
        public void SetAsyncEventHandlerNull()
        {
            // If: I assign an event handler on the message gispatcher with a null event handler
            var jh = new JsonRpcHost(GetChannelBase(null, null).Object);

            Assert.Throws <ArgumentNullException>(() => jh.SetAsyncEventHandler(CommonObjects.EventType, null));
        }
        public void SetAsyncEventHandlerNullEventType()
        {
            // If: I assign an event handler on the JSON RPC host with a null event type
            // Then: I should get an exception
            var jh = new JsonRpcHost(GetChannelBase(null, null).Object);

            Assert.Throws <ArgumentNullException>(() =>
                                                  jh.SetAsyncEventHandler <object>(null, (a, b) => Task.FromResult(false)));
        }
        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 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>()
                                   ));
        }