Ejemplo n.º 1
0
        public void CanSendMessageWithSentException()
        {
            IAmqpConnectionFactory  connectionFactory = A.Fake <IAmqpConnectionFactory>();
            AmqpMessageBuilder      messageBuilder    = A.Fake <AmqpMessageBuilder>();
            ISourceBlock <IMessage> sourceBlock       = A.Fake <ISourceBlock <IMessage> >();
            IConnection             connection        = A.Fake <IConnection>();
            IAmqpProperties         properties        = A.Fake <IAmqpProperties>();
            IModel channel = A.Fake <IModel>();

            A.CallTo(() => connectionFactory.CreateConnection()).Returns(connection);
            A.CallTo(() => connection.CreateModel()).Returns(channel);
            A.CallTo(() => channel.CreateBasicProperties()).Returns(null);
            A.CallTo(() => channel.BasicPublish(
                         A <string> ._,
                         A <string> ._,
                         A <bool> ._,
                         A <IBasicProperties> ._,
                         A <byte[]> ._))
            .Throws <Exception>()
            .Once();

            A.CallTo(() => messageBuilder.Serialize(A <IMessage> ._))
            .Returns(new AmqpMessage(properties, null, null, null)).Twice();

            var sut = new AmqpSender(connectionFactory, messageBuilder, new LogServiceProvider());

            Result <AmqpMessage, ExceptionDispatchInfo> result =
                new Result <AmqpMessage, ExceptionDispatchInfo>(
                    new AmqpMessage(new AmqpProperties(), string.Empty, string.Empty, new byte[0]));

            Action action = () => sut.Send(result);

            action.Should().NotThrow("because the AmqpSender should catch all exceptions to prevent from destroying of the DataFlow chain.");
        }
Ejemplo n.º 2
0
 public AmqpSender(IAmqpConnectionFactory connectionFactory, IAmqpMessageBuilder amqpMessageBuilder, ILogServiceProvider logServiceProvider = null)
 {
     this.connectionFactory  = connectionFactory;
     this.amqpMessageBuilder = amqpMessageBuilder;
     this.logServiceProvider = logServiceProvider ?? new NullLogServiceProvider();
     this.logService         = this.logServiceProvider.GetLogServiceOf(typeof(AmqpSender));
 }
Ejemplo n.º 3
0
        public async Task CanSendMessageAfterSentException()
        {
            // Arrange
            var channel = A.Fake <IModel>();

            A.CallTo(() => channel.CreateBasicProperties())
            .Returns(null);

            A.CallTo(() => channel.BasicPublish(
                         A <string> ._,
                         A <string> ._,
                         A <bool> ._,
                         A <IBasicProperties> ._,
                         A <byte[]> ._))
            .Throws <Exception>()
            .Once();

            IAmqpConnectionFactory connectionFactory = GetConnectionFactory(channel);

            var tasks = new List <TaskCompletionSource <bool> >()
            {
                new TaskCompletionSource <bool>(),
                new TaskCompletionSource <bool>()
            };

            var properties     = A.Fake <IAmqpProperties>();
            var messageBuilder = A.Fake <AmqpMessageBuilder>();

            A.CallTo(() => messageBuilder.Serialize(A <IMessage> ._))
            .ReturnsNextFromSequence(new AmqpMessage[]
            {
                new AmqpMessage(properties, null, null, new byte[1], tasks[0]),
                new AmqpMessage(properties, null, null, new byte[1], tasks[1])
            });

            var sut = new AmqpSender(connectionFactory, messageBuilder, new LogServiceProvider());

            var input = new BufferBlock <IMessage>();

            sut.SubscribeTo(input);

            // Act
            input.Post(new Message <string>("hello", new Dictionary <string, string>(), tasks[0]));
            input.Post(new Message <string>("hello", new Dictionary <string, string>(), tasks[1]));

            // Assert
            await tasks[0].Task.ContinueWith(t =>
            {
                t.Status.Should().Be(TaskStatus.Faulted);
            });

            (await tasks[1].Task).Should().Be(true);
        }
 /// <summary>
 /// 构造方法
 /// </summary>
 /// <param name="amqpConfigReader">AMQP配置读取</param>
 /// <param name="connectionFactory">连接工厂</param>
 public AmqpRpcClientMethodBase(IAmqpConfigReader amqpConfigReader, IAmqpConnectionFactory connectionFactory)
 {
     if (amqpConfigReader == null)
     {
         this.amqpConfigReader = AmqpUtil.GetConfigReader(amqpConfigReader);
     }
     else
     {
         this.amqpConfigReader = amqpConfigReader;
     }
     this.connectionFactory = connectionFactory;
 }
Ejemplo n.º 5
0
 public AsyncAmqpBasicConsumer(
     IAmqpConnectionFactory connectionFactory,
     IAmqpPropertyBuilder amqpPropertyBuilder,
     IAmqpMessageBuilder amqpMessageBuilder,
     bool continueOnCapturedContext,
     string queue)
 {
     this.connectionFactory         = connectionFactory;
     this.amqpPropertyBuilder       = amqpPropertyBuilder;
     this.amqpMessageBuilder        = amqpMessageBuilder;
     this.continueOnCapturedContext = continueOnCapturedContext;
     this.queue = queue;
 }
Ejemplo n.º 6
0
 public AsyncAmqpBasicConsumer(
     IAmqpConnectionFactory connectionFactory,
     IAmqpPropertyBuilder amqpPropertyBuilder,
     IAmqpMessageBuilder amqpMessageBuilder,
     bool continueOnCapturedContext,
     string queue,
     ILogServiceProvider logServiceProvder = null)
 {
     this.connectionFactory         = connectionFactory;
     this.amqpPropertyBuilder       = amqpPropertyBuilder;
     this.amqpMessageBuilder        = amqpMessageBuilder;
     this.continueOnCapturedContext = continueOnCapturedContext;
     this.queue             = queue;
     this.logServiceProvder = logServiceProvder ?? new NullLogServiceProvider();
     this.logService        = this.logServiceProvder.GetLogServiceOf(typeof(AsyncAmqpBasicConsumer <T>));
 }
Ejemplo n.º 7
0
        public void CanLinkTo()
        {
            IAmqpConnectionFactory  connectionFactory = A.Fake <IAmqpConnectionFactory>();
            AmqpMessageBuilder      messageBuilder    = A.Fake <AmqpMessageBuilder>();
            ISourceBlock <IMessage> sourceBlock       = A.Fake <ISourceBlock <IMessage> >();
            IConnection             connection        = A.Fake <IConnection>();
            IModel channel = A.Fake <IModel>();

            A.CallTo(() => connectionFactory.CreateConnection()).Returns(connection);
            A.CallTo(() => connection.CreateModel()).Returns(channel);

            var sut = new AmqpSender(connectionFactory, messageBuilder, new LogServiceProvider());

            ISubscriptionTag tag = sut.SubscribeTo(sourceBlock);

            tag.Id.Should().NotBeNull();
        }
Ejemplo n.º 8
0
        public void CanSendMessageWithSerializationException()
        {
            IAmqpConnectionFactory  connectionFactory = A.Fake <IAmqpConnectionFactory>();
            AmqpMessageBuilder      messageBuilder    = A.Fake <AmqpMessageBuilder>();
            ISourceBlock <IMessage> sourceBlock       = A.Fake <ISourceBlock <IMessage> >();
            IConnection             connection        = A.Fake <IConnection>();
            IAmqpProperties         properties        = A.Fake <IAmqpProperties>();
            IModel channel = A.Fake <IModel>();

            A.CallTo(() => connectionFactory.CreateConnection()).Returns(connection);
            A.CallTo(() => connection.CreateModel()).Returns(channel);

            A.CallTo(() => messageBuilder.Serialize(A <IMessage> ._))
            .Throws <Exception>()
            .Once()
            .Then.
            Returns(new AmqpMessage(properties, null, null, null));

            var      sut     = new AmqpSender(connectionFactory, messageBuilder, new LogServiceProvider());
            IMessage message = new Message <string>("hello", new Dictionary <string, string>());
            Action   action  = () => sut.Transform(message);

            action.Should().NotThrow("because the AmqpSender should catch all exception to prevent from destroying the DataFlow chain.");
        }
Ejemplo n.º 9
0
 public WeatherForecastController(ILogger <WeatherForecastController> logger, IAmqpConnectionFactory factory)
 {
     _logger      = logger;
     this.factory = factory;
 }
Ejemplo n.º 10
0
        public IAmqpSubscriptionBuilder WithConnectionFactory(IAmqpConnectionFactory connectionFactory)
        {
            this.ConnectionFactory = connectionFactory;

            return(this);
        }
Ejemplo n.º 11
0
 /// <summary>
 /// 构造方法
 /// </summary>
 /// <param name="amqpConfigReader">AMQP配置读取</param>
 /// <param name="connectionFactory">连接工厂</param>
 public RabbitRpcClientMethod(IAmqpConfigReader amqpConfigReader, IAmqpConnectionFactory connectionFactory)
     : base(amqpConfigReader, connectionFactory)
 {
 }
 /// <summary>
 /// 构造方法
 /// </summary>
 /// <param name="connectionFactory">连接工厂</param>
 public AmqpRpcClientMethodBase(IAmqpConnectionFactory connectionFactory)
     : this(null, connectionFactory)
 {
 }
Ejemplo n.º 13
0
 public AmqpSender(IAmqpConnectionFactory connectionFactory, IAmqpMessageBuilder amqpMessageBuilder)
 {
     this.connectionFactory  = connectionFactory;
     this.amqpMessageBuilder = amqpMessageBuilder;
 }
Ejemplo n.º 14
0
        public IAmqpPublishingBuilder WithConnectionFactory(IAmqpConnectionFactory connectionFactory)
        {
            this.ConnectionFactory = connectionFactory;

            return(this);
        }