Пример #1
0
        public virtual void Send(Envelope env)
        {
            _log.Debug("Enter Send");

            // guard clause
            if (null == env)
            {
                throw new ArgumentNullException("Cannot send a null envelope");
            }

            // create a context
            EnvelopeContext context = new EnvelopeContext(EnvelopeContext.Directions.Out, env);

            // send the envelope through the outbound chain
            this.ProcessEnvelope(context, this.OutboundChain.Sort(), () =>
            {
                // send the envelope to the transport provider
                _txProvider.Send(context.Envelope);

                // log the headers of the outgoing envelope
                _log.Debug(string.Format("Outgoing headers: {0}", context.Envelope.Headers.Flatten()));
            });

            _log.Debug("Leave Send");
        }
Пример #2
0
        public void Should_Not_Continue_Processing_If_Processor_Does_Not_Call_Continuation()
        {
            // create an envelope and context
            Envelope env = new Envelope() { Payload = Encoding.UTF8.GetBytes("Test") };
            EnvelopeContext ctx = new EnvelopeContext(EnvelopeContext.Directions.In, env);

            // mock a transport provider and envelope processor
            Mock<ITransportProvider> txMock = _mocker.Create<ITransportProvider>();
            Mock<IEnvelopeProcessor> procMock = _mocker.Create<IEnvelopeProcessor>();

            // create a processor chain and add the mock processor to it
            List<IEnvelopeProcessor> processorChain = new List<IEnvelopeProcessor>();
            processorChain.Add(procMock.Object);

            // the continuation that, if called, fails the test
            Action continuation = new Action( () =>
                Assert.Fail("The continuation action should not have been called"));

            // this is what we're testing (extended class gives public access to protected method)
            ExtendedDefaultEnvelopeBus bus = new ExtendedDefaultEnvelopeBus(txMock.Object);
            bus.PublicProcessEnvelope(ctx, processorChain, continuation);

            // make sure that the processor was called once
            procMock.Verify(proc => proc.ProcessEnvelope(ctx, It.IsAny<Action>()), Times.Once());

            // and that since it did nothing, the transport provider didn't get the envelope
            txMock.Verify(tx => tx.Send(env), Times.Never());
        }
Пример #3
0
        public virtual void Handle_Dispatcher(IEnvelopeDispatcher dispatcher)
        {
            _log.Debug("Enter Handle_Dispatcher");

            try
            {
                EnvelopeContext context = new EnvelopeContext(EnvelopeContext.Directions.In, dispatcher.Envelope);

                // send the envelope through the inbound processing chain
                this.ProcessEnvelope(context, this.InboundChain.Sort(), () =>
                {
                    dispatcher.Dispatch(context.Envelope);
                    _log.Debug("Dispatched envelope");
                });
            }
            catch (Exception ex)
            {
                _log.Warn("Failed to dispatch envelope; raising EnvelopeFailed event");
                dispatcher.Fail(ex);
            }

            _log.Debug("Leave Handle_Dispatcher");
        }
Пример #4
0
        public virtual void Handle_Dispatcher(IEnvelopeDispatcher dispatcher)
        {
            _log.Debug("Enter Handle_Dispatcher");

            try
            {
                EnvelopeContext context = new EnvelopeContext(EnvelopeContext.Directions.In, dispatcher.Envelope);

                // send the envelope through the inbound processing chain
                this.ProcessEnvelope(context, this.InboundChain.Sort(), () =>
                {
                    dispatcher.Dispatch(context.Envelope);
                    _log.Debug("Dispatched envelope");
                });
            }
            catch (Exception ex)
            {
                _log.Warn("Failed to dispatch envelope; raising EnvelopeFailed event");
                dispatcher.Fail(ex);
            }

            _log.Debug("Leave Handle_Dispatcher");
        }
Пример #5
0
        // recursive function that processes envelopes
        protected virtual void ProcessEnvelope(
            EnvelopeContext context,
            IEnumerable <IEnvelopeProcessor> processorChain,
            Action processingComplete)
        {
            // if the chain is null or empty, complete processing
            if ((null == processorChain) || (!processorChain.Any()))
            {
                processingComplete();
                return;
            }

            // grab the next procesor
            IEnvelopeProcessor nextProcessor = processorChain.First();

            // let it process the envelope and pass its "next" processor: a
            // method that recursively calls this function with the current
            // processor removed
            nextProcessor.ProcessEnvelope(context, () =>
            {
                this.ProcessEnvelope(context, processorChain.Skip(1), processingComplete);
            });
        }
Пример #6
0
 public void PublicProcessEnvelope(EnvelopeContext context, IEnumerable<IEnvelopeProcessor> processorChain, Action processingComplete)
 {
     this.ProcessEnvelope(context, processorChain, processingComplete);
 }
Пример #7
0
        public void Should_Send_Incoming_Envelopes_Through_the_Chain()
        {
            // create an envelope and context
            Envelope env = new Envelope() { Payload = Encoding.UTF8.GetBytes("Test") };
            EnvelopeContext ctx = new EnvelopeContext(EnvelopeContext.Directions.In, env);

            // mock a transport provider and envelope processor
            Mock<ITransportProvider> txMock = _mocker.Create<ITransportProvider>();
            Mock<IEnvelopeProcessor> procMock = _mocker.Create<IEnvelopeProcessor>();
            Mock<IEnvelopeDispatcher> dispatcherMock = _mocker.Create<IEnvelopeDispatcher>();

            dispatcherMock.SetupGet<Envelope>(disp => disp.Envelope).Returns(env);

            DefaultEnvelopeBus bus = new DefaultEnvelopeBus(txMock.Object);
            bus.OutboundChain = null;
            bus.InboundChain = new Dictionary<int, IEnvelopeProcessor>();
            bus.InboundChain.Add(0, procMock.Object);

            txMock.Raise(tx => tx.OnEnvelopeReceived += null, dispatcherMock.Object);

            procMock.Verify(
                proc => proc.ProcessEnvelope(It.IsAny<EnvelopeContext>(), It.IsAny<Action>()), Times.Once());
        }
Пример #8
0
        // recursive function that processes envelopes
        protected virtual void ProcessEnvelope(
            EnvelopeContext context,
            IEnumerable<IEnvelopeProcessor> processorChain,
            Action processingComplete)
        {
            // if the chain is null or empty, complete processing
            if ((null == processorChain) || (!processorChain.Any()))
            {
                processingComplete();
                return;
            }

            // grab the next procesor
            IEnvelopeProcessor nextProcessor = processorChain.First();

            // let it process the envelope and pass its "next" processor: a
            // method that recursively calls this function with the current
            // processor removed
            nextProcessor.ProcessEnvelope(context, () =>
            {
                this.ProcessEnvelope(context, processorChain.Skip(1), processingComplete);
            });
        }
Пример #9
0
        public virtual void Send(Envelope env)
        {
            _log.Debug("Enter Send");

            // guard clause
            if (null == env) { throw new ArgumentNullException("Cannot send a null envelope"); }

            // create a context
            EnvelopeContext context = new EnvelopeContext(EnvelopeContext.Directions.Out, env);

            // send the envelope through the outbound chain
            this.ProcessEnvelope(context, this.OutboundChain.Sort(), () =>
            {
                // send the envelope to the transport provider
                _txProvider.Send(context.Envelope);

                // log the headers of the outgoing envelope
                _log.Debug(string.Format("Outgoing headers: {0}", context.Envelope.Headers.Flatten()));
            });

            _log.Debug("Leave Send");
        }