Example #1
0
        public async Task TcpHealthListener_ProbeForHealthReport_ResponseHealthy()
        {
            // Arrange
            var service = new TcpHealthService(_healthTcpPort, Logger);

            // Act
            HealthReport report = await service.GetHealthReportAsync();

            // Assert
            Assert.NotNull(report);
            Assert.Equal(HealthStatus.Healthy, report.Status);
            (string entryName, HealthReportEntry entry) = Assert.Single(report.Entries);
            Assert.Equal("sample", entryName);
            Assert.Equal(HealthStatus.Healthy, entry.Status);
        }
        public async Task TcpHealthProbe_RejectsTcpConnection_WhenHealthCheckIsUnhealthy()
        {
            // Arrange
            var service = new TcpHealthService(TcpPort, _logger);
            var options = new WorkerOptions();

            options.Configuration.Add(HealthPortConfigurationName, TcpPort.ToString());
            options.Services.AddTcpHealthProbes(HealthPortConfigurationName,
                                                configureHealthChecks: builder => builder.AddCheck("unhealhty", () => HealthCheckResult.Unhealthy()),
                                                configureTcpListenerOptions: opt => opt.RejectTcpConnectionWhenUnhealthy = true,
                                                configureHealthCheckPublisherOptions: opt => opt.Period = TimeSpan.FromSeconds(3));

            // Act
            await using (var worker = await Worker.StartNewAsync(options))
            {
                // Assert
                await RetryAssert <ThrowsException, SocketException>(
                    () => Assert.ThrowsAnyAsync <SocketException>(() => service.GetHealthReportAsync()));
            }
        }
        public async Task TcpHealthProbe_AcceptsTcpConnection_WhenHealthCheckIsHealthy()
        {
            // Arrange
            var service = new TcpHealthService(TcpPort, _logger);
            var options = new WorkerOptions();

            options.Configuration.Add(HealthPortConfigurationName, TcpPort.ToString());
            options.Services.AddTcpHealthProbes(HealthPortConfigurationName,
                                                configureHealthChecks: builder => builder.AddCheck("healhty", () => HealthCheckResult.Healthy()),
                                                configureTcpListenerOptions: opt => opt.RejectTcpConnectionWhenUnhealthy = true);

            // Act
            await using (var worker = await Worker.StartNewAsync(options))
            {
                // Assert
                HealthReport afterReport = await RetryAssert <SocketException, HealthReport>(
                    () => service.GetHealthReportAsync());

                Assert.NotNull(afterReport);
                Assert.Equal(HealthStatus.Healthy, afterReport.Status);
            }
        }