Beispiel #1
0
        public async Task GetDisconnectedConsumersAsync_SomeConsumersNotFullyConnected_ConsumersListReturned()
        {
            var broker1 = Substitute.For <IBroker>();

            broker1.ProducerEndpointType.Returns(typeof(TestProducerEndpoint));
            broker1.ConsumerEndpointType.Returns(typeof(TestConsumerEndpoint));
            broker1.Consumers.Returns(
                new[]
            {
                _readyConsumer, _consumingConsumer, _connectedConsumer
            });
            var broker2 = Substitute.For <IBroker>();

            broker2.ProducerEndpointType.Returns(typeof(TestOtherProducerEndpoint));
            broker2.ConsumerEndpointType.Returns(typeof(TestOtherConsumerEndpoint));
            broker2.Consumers.Returns(
                new[]
            {
                _readyConsumer, _disconnectedConsumer
            });

            var brokerCollection        = new BrokerCollection(new[] { broker1, broker2 });
            var hostApplicationLifetime = Substitute.For <IHostApplicationLifetime>();
            var service = new ConsumersHealthCheckService(brokerCollection, hostApplicationLifetime);

            IReadOnlyCollection <IConsumer> result =
                await service.GetDisconnectedConsumersAsync(ConsumerStatus.Ready);

            result.Should().HaveCount(2);
            result.Should().BeEquivalentTo(_connectedConsumer, _disconnectedConsumer);
        }
Beispiel #2
0
        public async Task GetDisconnectedConsumersAsync_ShuttingDown_EmptyCollectionReturned()
        {
            var broker1 = Substitute.For <IBroker>();

            broker1.ProducerEndpointType.Returns(typeof(TestProducerEndpoint));
            broker1.ConsumerEndpointType.Returns(typeof(TestConsumerEndpoint));
            broker1.Consumers.Returns(
                new[]
            {
                _readyConsumer, _consumingConsumer, _connectedConsumer
            });
            var broker2 = Substitute.For <IBroker>();

            broker2.ProducerEndpointType.Returns(typeof(TestOtherProducerEndpoint));
            broker2.ConsumerEndpointType.Returns(typeof(TestOtherConsumerEndpoint));
            broker2.Consumers.Returns(
                new[]
            {
                _readyConsumer, _disconnectedConsumer
            });

            var brokerCollection               = new BrokerCollection(new[] { broker1, broker2 });
            var hostApplicationLifetime        = Substitute.For <IHostApplicationLifetime>();
            var applicationStoppingTokenSource = new CancellationTokenSource();

            hostApplicationLifetime.ApplicationStopping.Returns(applicationStoppingTokenSource.Token);
            var service = new ConsumersHealthCheckService(brokerCollection, hostApplicationLifetime);

            applicationStoppingTokenSource.Cancel();

            IReadOnlyCollection <IConsumer> result =
                await service.GetDisconnectedConsumersAsync(ConsumerStatus.Ready);

            result.Should().BeEmpty();
        }
Beispiel #3
0
        public async Task GetDisconnectedConsumersAsync_Filter_FilteredConsumersListReturned()
        {
            var broker = Substitute.For <IBroker>();

            broker.ProducerEndpointType.Returns(typeof(TestProducerEndpoint));
            broker.ConsumerEndpointType.Returns(typeof(TestConsumerEndpoint));
            broker.Consumers.Returns(
                new[]
            {
                _disconnectedConsumer, _connectedConsumer
            });

            var brokerCollection        = new BrokerCollection(new[] { broker });
            var hostApplicationLifetime = Substitute.For <IHostApplicationLifetime>();
            var service = new ConsumersHealthCheckService(brokerCollection, hostApplicationLifetime);

            IReadOnlyCollection <IConsumer> result1 =
                await service.GetDisconnectedConsumersAsync(
                    ConsumerStatus.Ready,
                    TimeSpan.Zero,
                    endpoint => endpoint.Name is "topic1" or "topic2");

            IReadOnlyCollection <IConsumer> result2 =
                await service.GetDisconnectedConsumersAsync(
                    ConsumerStatus.Ready,
                    TimeSpan.Zero,
                    endpoint => endpoint.Name == "topic1");

            result1.Should().HaveCount(2);
            result1.Should().BeEquivalentTo(new[] { _disconnectedConsumer, _connectedConsumer });
            result2.Should().HaveCount(1);
            result2.Should().BeEquivalentTo(new[] { _disconnectedConsumer });
        }
Beispiel #4
0
        public async Task GetDisconnectedConsumersAsync_AllConsumersConnected_EmptyCollectionReturned()
        {
            var broker1 = Substitute.For <IBroker>();

            broker1.ProducerEndpointType.Returns(typeof(TestProducerEndpoint));
            broker1.ConsumerEndpointType.Returns(typeof(TestConsumerEndpoint));
            broker1.Consumers.Returns(
                new[]
            {
                _connectedConsumer, _consumingConsumer, _readyConsumer
            });
            var broker2 = Substitute.For <IBroker>();

            broker2.ProducerEndpointType.Returns(typeof(TestOtherProducerEndpoint));
            broker2.ConsumerEndpointType.Returns(typeof(TestOtherConsumerEndpoint));
            broker2.Consumers.Returns(
                new[]
            {
                _readyConsumer, _readyConsumer
            });

            var brokerCollection        = new BrokerCollection(new[] { broker1, broker2 });
            var hostApplicationLifetime = Substitute.For <IHostApplicationLifetime>();
            var service = new ConsumersHealthCheckService(brokerCollection, hostApplicationLifetime);

            IReadOnlyCollection <IConsumer> result =
                await service.GetDisconnectedConsumersAsync(ConsumerStatus.Connected, TimeSpan.Zero, null);

            result.Should().BeEmpty();
        }
Beispiel #5
0
        public async Task GetDisconnectedConsumersAsync_ElapsedGracePeriod_ConsumersListReturned()
        {
            var statusInfo = Substitute.For <IConsumerStatusInfo>();

            statusInfo.Status.Returns(ConsumerStatus.Connected);
            var consumer = Substitute.For <IConsumer>();

            consumer.StatusInfo.Returns(statusInfo);
            consumer.StatusInfo.History.Returns(
                new List <IConsumerStatusChange>
            {
                new ConsumerStatusChange(ConsumerStatus.Connected, DateTime.UtcNow.AddSeconds(-30)),
                new ConsumerStatusChange(ConsumerStatus.Ready, DateTime.UtcNow.AddSeconds(-20)),
                new ConsumerStatusChange(ConsumerStatus.Connected, DateTime.UtcNow.AddSeconds(-15))
            });

            var broker = Substitute.For <IBroker>();

            broker.ProducerEndpointType.Returns(typeof(TestProducerEndpoint));
            broker.ConsumerEndpointType.Returns(typeof(TestConsumerEndpoint));
            broker.Consumers.Returns(new[] { consumer });

            var brokerCollection        = new BrokerCollection(new[] { broker });
            var hostApplicationLifetime = Substitute.For <IHostApplicationLifetime>();
            var service = new ConsumersHealthCheckService(brokerCollection, hostApplicationLifetime);

            IReadOnlyCollection <IConsumer> result =
                await service.GetDisconnectedConsumersAsync(
                    ConsumerStatus.Ready,
                    TimeSpan.FromSeconds(10),
                    null);

            result.Should().HaveCount(1);
            result.Should().BeEquivalentTo(new[] { consumer });
        }