public void On_DuplicateRequestType_ThrowsDuplicateHandlerForTypeException()
        {
            var connection = TestableConnection.Create();

            connection.On <string>("requestType", _ => { });

            var exception = Assert.Throws <DuplicateHandlerForRequestTypeException>(() => connection.On <string>("requestType", _ => { }));

            Assert.Contains("requestType", exception.Message);
        }
        public void Start_ValidInputs_CallsInitOnChannelRequestDispatcherAndRequestExecutor()
        {
            var connection = TestableConnection.Create();

            connection.ChannelMock.Setup(c => c.IsOpen).Returns(false);
            var inputStream  = new MemoryStream();
            var outputWriter = new StringWriter();

            connection.Start(inputStream, outputWriter);

            connection.ChannelMock.Verify(c => c.Init(inputStream, outputWriter));
            connection.RequestExecutorMock.Verify(c => c.Init(It.IsAny <ICollection <IRequestHandler> >(), connection.BufferBlock));
            connection.MessageDispatcherMock.Verify(c => c.Init(connection.BufferBlock, connection.ChannelMock.Object));
            connection.ResponseHandlerExecutorMock.Verify(c => c.Init(It.IsAny <ICollection <IResponseHandler> >(), connection.BufferBlock));
        }
        public void Start_ValidRequestHandler_PassesHandlerToRequestExecutor()
        {
            var connection = TestableConnection.Create();

            connection.On <string>("requestType", _ => { });

            connection.ChannelMock.Setup(c => c.IsOpen).Returns(false);
            connection.MessageDispatcherMock.Setup(re => re.StartAsync(It.IsAny <CancellationToken>())).Returns(Task.CompletedTask);
            ICollection <IRequestHandler> requestHandlers = null;

            connection.RequestExecutorMock
            .Setup(re => re.Init(It.IsAny <ICollection <IRequestHandler> >(), It.IsAny <ITargetBlock <IChannelMessage> >()))
            .Callback <ICollection <IRequestHandler>, ITargetBlock <IChannelMessage> >((handlers, target) => {
                requestHandlers = handlers;
            });
            var inputStream  = new MemoryStream();
            var outputWriter = new StringWriter();

            connection.Start(inputStream, outputWriter);

            Assert.NotNull(requestHandlers);
            Assert.Single(requestHandlers);
            Assert.Contains("requestType", requestHandlers.Select(r => r.RequestType));
        }