public BusConnectionTests()
 {
     _connectionFactoryMock   = new Mock <IConnectionFactory>();
     _busSerializerMock       = new Mock <IBusSerializer>();
     _serviceScopeFactoryMock = new Mock <IServiceScopeFactory>();
     _connectionMock          = new Mock <IConnection>();
     _channelMock             = new Mock <IModel>();
     _scopeMock           = new Mock <IServiceScope>();
     _serviceProviderMock = new Mock <IServiceProvider>();
     _scopeMock.SetupGet(x => x.ServiceProvider)
     .Returns(_serviceProviderMock.Object)
     .Verifiable();
     _connectionFactoryMock.Setup(x => x.CreateConnection())
     .Returns(_connectionMock.Object)
     .Verifiable();
     _connectionMock.Setup(x => x.CreateModel())
     .Returns(_channelMock.Object)
     .Verifiable();
     _busConnection = new BusConnection(
         _connectionFactoryMock.Object,
         _busSerializerMock.Object,
         _serviceScopeFactoryMock.Object);
     _serviceScopeFactoryMock.Setup(x => x.CreateScope())
     .Returns(_scopeMock.Object)
     .Verifiable();
 }
Ejemplo n.º 2
0
 public static uint MessageCount(this BusConnection connection, Queue queue)
 {
     using (var channel = connection.ConsumerConnection.CreateModel())
     {
         var count = channel.MessageCount(queue.Name.Value);
         channel.Close();
         return(count);
     }
 }
        public void GivenConnectionWhenCreateShouldNotTryToConnect()
        {
            var busConnection = new BusConnection(
                _connectionFactoryMock.Object,
                _busSerializerMock.Object,
                _serviceScopeFactoryMock.Object);

            busConnection.Should().NotBeNull();
            _connectionFactoryMock.Verify(x => x.CreateConnection(), Times.Never());
        }
Ejemplo n.º 4
0
 internal Consumer(BusConnection connection, IBusLogger logger, IRetryBehavior retryBehavior, IServiceScopeFactory scopeFactory, ConsumerOptions <T> options)
 {
     _options       = options ?? throw new ArgumentNullException(nameof(options));
     _connection    = connection ?? throw new ArgumentNullException(nameof(connection));
     _logger        = logger;
     _retryBehavior = retryBehavior ?? throw new ArgumentNullException(nameof(retryBehavior));
     _scopeFactory  = scopeFactory ?? throw new ArgumentNullException(nameof(scopeFactory));
     _tasks         = new Tasks(_options.ConsumerMaxParallelTasks);
     _channel       = connection.ConsumerConnection.CreateModel();
     _channel.BasicQos(0, options.PrefetchCount, false);
     DeclareAndBind();
     _consumerTag = _channel.BasicConsume(_options.Queue.Name.Value, false, this);
 }
Ejemplo n.º 5
0
 public BusConnectionTests()
 {
     _connectionFactoryMock   = new Mock <IConnectionFactory>();
     _busSerializerMock       = new Mock <IBusSerializer>();
     _serviceScopeFactoryMock = new Mock <IServiceScopeFactory>();
     _connectionMock          = new Mock <IConnection>();
     _channelMock             = new Mock <IModel>();
     _scopeMock           = new Mock <IServiceScope>();
     _serviceProviderMock = new Mock <IServiceProvider>();
     _basicPropertiesMock = new Mock <IBasicProperties>();
     _loggerMock          = new Mock <IBusLogger>();
     _headersMock         = new Mock <IDictionary <string, object> >();
     _publishBatchMock    = new Mock <IBasicPublishBatch>();
     _optionsMock         = new Mock <IOptions <BusConnectionOptions> >();
     _optionsMock.SetupGet(x => x.Value)
     .Returns(new BusConnectionOptions
     {
         PublisherBufferTtlInMilliseconds = 1
     })
     .Verifiable();
     _channelMock.Setup(x => x.CreateBasicPublishBatch())
     .Returns(_publishBatchMock.Object)
     .Verifiable();
     _basicPropertiesMock.SetupGet(x => x.Headers)
     .Returns(_headersMock.Object)
     .Verifiable();
     _scopeMock.SetupGet(x => x.ServiceProvider)
     .Returns(_serviceProviderMock.Object)
     .Verifiable();
     _connectionFactoryMock.Setup(x => x.CreateConnection())
     .Returns(_connectionMock.Object)
     .Verifiable();
     _connectionMock.Setup(x => x.CreateModel())
     .Returns(_channelMock.Object)
     .Verifiable();
     _busConnection = new BusConnection(
         _connectionFactoryMock.Object,
         _busSerializerMock.Object,
         _serviceScopeFactoryMock.Object,
         _optionsMock.Object,
         _loggerMock.Object);
     _serviceScopeFactoryMock.Setup(x => x.CreateScope())
     .Returns(_scopeMock.Object)
     .Verifiable();
     _channelMock.Setup(x => x.CreateBasicProperties())
     .Returns(_basicPropertiesMock.Object)
     .Verifiable();
 }
Ejemplo n.º 6
0
        public static Message GetMessage(this BusConnection connection, Queue queue)
        {
            using (var channel = connection.ConsumerConnection.CreateModel())
            {
                var result     = channel.BasicGet(queue.Name.Value, false);
                var serializer = new BusSerializer();
                var message    = TestMessage <string> .Create(
                    result,
                    serializer,
                    msg => { },
                    (exception, msg) => { });

                channel.Close();
                return(message);
            }
        }
Ejemplo n.º 7
0
        private BusConnection GetBusConnection(string busName)
        {
            if (string.IsNullOrWhiteSpace(busName))
            {
                throw new ArgumentException("Bus name not specified.", nameof(busName));
            }

            BusConnection connection = _busSettings.Connections.FirstOrDefault(c => c.BusName == busName);

            if (connection == null)
            {
                throw new InvalidOperationException(
                          $"A bus configuration with the name: {busName} has not been configured.");
            }

            return(connection);
        }
Ejemplo n.º 8
0
 public static IConsumerMessage GetMessage(this BusConnection connection, Queue queue)
 {
     using (var channel = connection.ConsumerConnection.CreateModel())
     {
         var result     = channel.BasicGet(queue.Name.Value, false);
         var serializer = new BusSerializer();
         var @event     = new BasicDeliverEventArgs(
             string.Empty,
             result.DeliveryTag,
             result.Redelivered,
             result.Exchange,
             result.RoutingKey,
             result.BasicProperties,
             result.Body);
         var builder = new MessageBuilder(null, serializer);
         var message = builder.SetEvent(@event)
                       .Build();
         channel.Close();
         return(message);
     }
 }
Ejemplo n.º 9
0
        private void CreateBus(BusConnection conn)
        {
            if (_buses.ContainsKey(conn.BusName))
            {
                throw new ContainerException(
                          $"A bus has already been created for the bus named: {conn.BusName}." +
                          "Check configuration for duplicates.");
            }

            // Create an EasyNetQ connection configuration from settings:
            ConnectionConfiguration connConfig = new ConnectionConfiguration {
                UserName           = conn.UserName,
                Password           = conn.Password,
                VirtualHost        = conn.VHostName,
                RequestedHeartbeat = conn.Heartbeat,
                Name                 = conn.BusName,
                Product              = Context.AppHost.Name,
                Timeout              = conn.Timeout,
                PublisherConfirms    = conn.PublisherConfirms,
                PersistentMessages   = conn.PersistentMessages,
                UseBackgroundThreads = conn.UseBackgroundThreads,
                PrefetchCount        = conn.PrefetchCount
            };

            SetAdditionalClientProperties(connConfig.ClientProperties);

            // Set associated hosts:
            connConfig.Hosts = conn.Hosts.Select(h => new HostConfiguration {
                Host = h.HostName,
                Port = h.Port
            }).ToArray();

            // Allow EasyNetQ to validate the connection configuration:
            connConfig.Validate();

            _buses[conn.BusName] = BusFactory(connConfig);
        }