public void SetCallBack_Valid_EventHandlerAdded()
        {
            // Arrange
            string payload = "test";

            byte[] payloadBytes = Encoding.UTF8.GetBytes(payload);
            ReadOnlyMemory <byte> readOnlyMemory = new ReadOnlyMemory <byte>(payloadBytes);
            BasicDeliverEventArgs eventArgs      =
                new BasicDeliverEventArgs(null, 1, false, null, null, null, readOnlyMemory);
            Mock <IModel> modelMock = new Mock <IModel>();

            modelMock.Setup(x => x.BasicAck(eventArgs.DeliveryTag, false))
            .Verifiable();
            Mock <EventingBasicConsumer> consumerMock     = new Mock <EventingBasicConsumer>(modelMock.Object);
            Mock <ICallbackService>      notificationmock = new Mock <ICallbackService>();

            notificationmock.Setup(x => x.ParsePayload(It.Is <string>(x => x == payload))).Verifiable();
            notificationmock.Setup(x => x.ValidatePayload()).Verifiable();
            notificationmock.Setup(x => x.ExecuteTask()).Verifiable();

            RabbitMQListener listener = new RabbitMQListener(modelMock.Object);

            // Act
            listener.CreateConsumer(notificationmock.Object);
            listener.SetCallBack(notificationmock.Object, consumerMock.Object);
            listener.CallBack(notificationmock.Object, eventArgs);

            // Assert
            notificationmock.Verify();
        }
Exemplo n.º 2
0
        private static void Main()
        {
            string environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            // Appsettings is renamed because of an issue where the project loaded another appsettings.json
            IConfiguration configuration = new ConfigurationBuilder()
                                           .AddJsonFile("appsettingsnotificationsystem.json", true, true)
                                           .AddJsonFile($"appsettingsnotificationsystem.{environmentName}.json", true, true)
                                           .AddEnvironmentVariables()
                                           .Build();
            Config config = configuration.GetSection("App").Get <Config>();

            IRabbitMQConnectionFactory connectionFactory = new RabbitMQConnectionFactory(config.RabbitMQ.Hostname, config.RabbitMQ.Username, config.RabbitMQ.Password);

            RabbitMQSubscriber subscriber = new RabbitMQSubscriber(connectionFactory);
            IModel             channel    = subscriber.SubscribeToSubject("EMAIL");

            RabbitMQListener listener = new RabbitMQListener(channel);

            // inject your notification service here
            ISendGridClient       sendGridClient      = new SendGridClient(config.SendGrid.ApiKey);
            INotificationService  notificationService = new EmailSender(sendGridClient, config.SendGrid.EmailFrom, config.SendGrid.SandboxMode);
            EventingBasicConsumer consumer            = listener.CreateConsumer(notificationService);

            listener.StartConsumer(consumer, "EMAIL");
            Console.ReadLine();
        }
        public async Task GetMetrics_ReturnsExpectedResult()
        {
            RabbitMQListener listener = new RabbitMQListener(_mockExecutor.Object, _mockService.Object, "listener_test_queue", _mockLogger.Object, new FunctionDescriptor {
                Id = "TestFunction"
            }, 30);
            var metrics = await listener.GetMetricsAsync();

            Assert.Equal((uint)5, metrics.QueueLength);
            Assert.NotEqual(default(DateTime), metrics.Timestamp);
        }
        public void CreatesHeadersAndRepublishes()
        {
            _mockService.Setup(m => m.RabbitMQModel).Returns(_mockModel.Object);

            RabbitMQListener listener = new RabbitMQListener(_mockExecutor.Object, _mockService.Object, "blah", _mockLogger.Object, _mockDescriptor.Object, 30);

            var properties             = new BasicProperties();
            BasicDeliverEventArgs args = new BasicDeliverEventArgs("tag", 1, false, "", "queue", properties, Encoding.UTF8.GetBytes("hello world"));

            listener.CreateHeadersAndRepublish(args);

            _mockModel.Verify(m => m.BasicAck(It.IsAny <ulong>(), false), Times.Exactly(1));
            _mockModel.Verify(m => m.BasicPublish(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <IBasicProperties>(), It.IsAny <byte[]>()), Times.Exactly(1));
        }
Exemplo n.º 5
0
        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            RabbitMQSubscriber subscriber = new RabbitMQSubscriber(
                new RabbitMQConnectionFactory(config.RabbitMQ.Hostname,
                                              config.RabbitMQ.Username,
                                              config.RabbitMQ.Password));
            IModel           channel  = subscriber.SubscribeToSubject(subject);
            RabbitMQListener listener = new RabbitMQListener(channel);

            ICallbackService      documentDeleterService = new DocumentDeleter(config, restClient);
            EventingBasicConsumer consumer = listener.CreateConsumer(documentDeleterService);

            listener.StartConsumer(consumer, subject);
            return(Task.CompletedTask);
        }
        public void RejectsStaleMessages()
        {
            _mockService.Setup(m => m.RabbitMQModel).Returns(_mockModel.Object);
            RabbitMQListener listener = new RabbitMQListener(_mockExecutor.Object, _mockService.Object, "blah", 1, _mockLogger.Object);

            var properties = new BasicProperties();

            properties.Headers = new Dictionary <string, object>();
            properties.Headers.Add("requeueCount", 6);
            BasicDeliverEventArgs args = new BasicDeliverEventArgs("tag", 1, false, "", "queue", properties, Encoding.UTF8.GetBytes("hello world"));

            listener.RepublishMessages(args);

            _mockModel.Verify(m => m.BasicReject(It.IsAny <ulong>(), false), Times.Exactly(1));
        }
        public RabbitMQListenerTests()
        {
            _mockExecutor   = new Mock <ITriggeredFunctionExecutor>();
            _mockService    = new Mock <IRabbitMQService>();
            _mockLogger     = new Mock <ILogger>();
            _mockModel      = new Mock <IRabbitMQModel>();
            _mockDescriptor = new Mock <FunctionDescriptor>();

            _mockService.Setup(m => m.RabbitMQModel).Returns(_mockModel.Object);
            QueueDeclareOk queueInfo = new QueueDeclareOk("blah", 5, 1);

            _mockModel.Setup(m => m.QueueDeclarePassive(It.IsAny <string>())).Returns(queueInfo);

            _testListener = new RabbitMQListener(_mockExecutor.Object, _mockService.Object, "blah", _mockLogger.Object, new FunctionDescriptor {
                Id = "TestFunction"
            }, 30);
        }
Exemplo n.º 8
0
        public void StartConsumer_Valid_BasicConsumeCalled()
        {
            // Arrange
            string        subject   = "test";
            Mock <IModel> modelMock = new Mock <IModel>();
            Mock <EventingBasicConsumer> consumerMock = new Mock <EventingBasicConsumer>(modelMock.Object);

            modelMock.Setup(x => x.BasicConsume(subject, false, "", false, false, null, consumerMock.Object)).Verifiable();

            RabbitMQListener listener = new RabbitMQListener(modelMock.Object);

            // Act
            listener.StartConsumer(consumerMock.Object, subject);

            // Assert
            modelMock.Verify();
        }
Exemplo n.º 9
0
        private static void Main()
        {
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
                         .MinimumLevel.Override("System", LogEventLevel.Warning)
                         .Enrich.FromLogContext()
                         .WriteTo.Console(outputTemplate:
                                          "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}",
                                          theme: AnsiConsoleTheme.Literate)
                         .CreateLogger();

            string environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

            // Appsettings is renamed because of an issue where the project loaded another appsettings.json
            IConfiguration configuration = new ConfigurationBuilder()
                                           .AddJsonFile("appsettingsnotificationsystem.json", true, true)
                                           .AddJsonFile($"appsettingsnotificationsystem.{environmentName}.json",
                                                        true,
                                                        true)
                                           .AddEnvironmentVariables()
                                           .Build();
            Config config = configuration.GetSection("App")
                            .Get <Config>();

            IRabbitMQConnectionFactory connectionFactory =
                new RabbitMQConnectionFactory(config.RabbitMQ.Hostname,
                                              config.RabbitMQ.Username,
                                              config.RabbitMQ.Password);

            RabbitMQSubscriber subscriber = new RabbitMQSubscriber(connectionFactory);
            IModel             channel    = subscriber.SubscribeToSubject("EMAIL");

            RabbitMQListener listener = new RabbitMQListener(channel);

            // inject your notification service here
            ISendGridClient       sendGridClient      = new SendGridClient(config.SendGrid.ApiKey);
            ICallbackService      notificationService = new EmailSender(sendGridClient, config.SendGrid.EmailFrom, config.SendGrid.SandboxMode);
            EventingBasicConsumer consumer            = listener.CreateConsumer(notificationService);

            listener.StartConsumer(consumer, "EMAIL");
            Console.ReadLine();
        }
Exemplo n.º 10
0
 private void OnStarted()
 {
     RabbitMQListener.Start();
 }
Exemplo n.º 11
0
 private void OnStopped()
 {
     RabbitMQListener.Stop();
 }