コード例 #1
0
        public void AddServiceFabricHealthChecks_RegisterPublisherAndChecks()
        {
            string       checkName = "MockCheck";
            IHealthCheck check     = new MockCheck();

            IServiceProvider provider = new ServiceCollection()
                                        .AddSingleton(new Mock <IAccessor <IServicePartition> >().Object)
                                        .AddServiceFabricHealthChecks()
                                        .AddCheck(checkName, check)
                                        .Services
                                        .BuildServiceProvider();

            IHealthCheckPublisher[] publishers = provider
                                                 .GetRequiredService <IEnumerable <IHealthCheckPublisher> >()
                                                 .ToArray();

            Assert.IsTrue(
                publishers.Any(p => p is ServiceFabricHealthCheckPublisher),
                FormattableString.Invariant($"{nameof(ServiceFabricHealthCheckPublisher)} publisher should be registered"));

            IOptions <HealthCheckServiceOptions> options = provider.GetRequiredService <IOptions <HealthCheckServiceOptions> >();
            HealthCheckRegistration?registration         = options.Value.Registrations.SingleOrDefault(r => string.Equals(checkName, r.Name, StringComparison.Ordinal));

            NullableAssert.IsNotNull(registration, "HealthCheck should be registered");

            Assert.AreEqual(check, registration.Factory(provider));
        }
コード例 #2
0
        public void Fail_PropagatesErrorMessage()
        {
            AssertFailedException exception = Assert.ThrowsException <AssertFailedException>(
                () => NullableAssert.Fail(s_message, s_parameters));

            StringAssert.Contains(exception.Message, s_expectedMessage);
        }
コード例 #3
0
        public void AddOmexActivitySource_ActivityCreationEnabled()
        {
            Task task = CreateHost().RunAsync();

            Activity?activity = new ActivitySource("Source")
                                .StartActivity(nameof(AddOmexActivitySource_HostedServicesRegiestered));

            NullableAssert.IsNotNull(activity, "Activity creation enabled after host started");
        }
コード例 #4
0
            public void CheckFail(object?test)
            {
                if (test == null)
                {
                    NullableAssert.Fail();
                }

                test.GetHashCode();                 // should allow calling method on nullable variable without check
            }
コード例 #5
0
        private HttpHealthCheckParameters GetParameters(IServiceProvider provider, string checkName)
        {
            IOptions <HealthCheckServiceOptions> options = provider.GetRequiredService <IOptions <HealthCheckServiceOptions> >();
            HealthCheckRegistration?registration         = options.Value.Registrations.SingleOrDefault(r => string.Equals(checkName, r.Name, StringComparison.Ordinal));

            NullableAssert.IsNotNull(registration, "HealthCheck should be registered");

            IHealthCheck check = registration.Factory(provider);

            Assert.IsInstanceOfType(check, typeof(HttpEndpointHealthCheck));
            return(((HttpEndpointHealthCheck)check).Parameters);
        }
コード例 #6
0
        private void AssertActivityReporting(
            Activity?activity,
            Mock <IActivityStartObserver> mockStartObserver,
            Mock <IActivityStopObserver> mockStopObserver)
        {
            NullableAssert.IsNotNull(activity);
            mockStartObserver.Verify(s => s.OnStart(activity, null), Times.Once);
            mockStartObserver.Reset();

            activity.Dispose();

            mockStopObserver.Verify(s => s.OnStop(activity, null), Times.Once);
            mockStopObserver.Reset();
        }
コード例 #7
0
        public void AddOmexActivitySource_ActivityCreationEnabled()
        {
            Task task = new HostBuilder()
                        .ConfigureServices(collection =>
            {
                collection.AddOmexActivitySource();
            })
                        .Build()
                        .RunAsync();

            Activity?activity = new ActivitySource("Source")
                                .StartActivity(nameof(AddOmexActivitySource_HostedServicesRegiestered));

            NullableAssert.IsNotNull(activity, "Activity creation enabled after host started");
        }
コード例 #8
0
        public async Task AbstractHealthCheck_MarksActivityWithHealthCheckFlag()
        {
            using TestActivityListener listener = new TestActivityListener();
            Activity?activity = null;

            HealthCheckResult actualResult = await new TestHealthCheck((c, t) =>
            {
                activity = Activity.Current;
                return(HealthCheckResult.Healthy());
            }).CheckHealthAsync(HealthCheckContextHelper.CreateCheckContext());

            Assert.AreEqual(actualResult.Status, HealthStatus.Healthy);
            NullableAssert.IsNotNull(activity);
            Assert.IsTrue(activity !.IsHealthCheck(), "Activity should be marked as HealthCheck activity");
            activity.AssertResult(ActivityResult.Success);
        }
コード例 #9
0
        private void TestActivityTransfer(Activity outgoingActivity)
        {
            HeaderMock header = new HeaderMock();
            Mock <IServiceRemotingRequestMessage> requestMock = new Mock <IServiceRemotingRequestMessage>();

            requestMock.Setup(m => m.GetHeader()).Returns(header);

            outgoingActivity.Start();
            requestMock.Object.AttachActivityToOutgoingRequest(outgoingActivity);
            outgoingActivity.Stop();

            using DiagnosticListener listener = CreateActiveListener("TestListeners");
            Activity?incomingActivity = requestMock.Object.StartActivityFromIncomingRequest(listener, outgoingActivity.OperationName + "_Out");

            NullableAssert.IsNotNull(incomingActivity);
            incomingActivity.Stop();

            Assert.AreEqual(outgoingActivity.Id, incomingActivity.ParentId);
            CollectionAssert.AreEqual(outgoingActivity.Baggage.ToArray(), incomingActivity.Baggage.ToArray());
        }
コード例 #10
0
        public async Task AbstractHealthCheck_WhenExceptionThrown_ReturnsUnhealtyState()
        {
            using TestActivityListener listener = new TestActivityListener();
            Activity? activity  = null;
            Exception exception = new ArrayTypeMismatchException();

            HealthCheckResult actualResult = await new TestHealthCheck((c, t) =>
            {
                activity = Activity.Current;
                throw exception;
            }
                                                                       ).CheckHealthAsync(HealthCheckContextHelper.CreateCheckContext());

            Assert.AreEqual(actualResult.Exception, exception);
            Assert.AreEqual(actualResult.Status, HealthStatus.Unhealthy);
            CollectionAssert.AreEquivalent(actualResult.Data.ToArray(), TestHealthCheck.TestParameters);
            NullableAssert.IsNotNull(activity);
            Assert.IsTrue(activity.IsHealthCheck(), "Activity should be marked as HealthCheck activity");
            activity.AssertResult(ActivityResult.SystemError);
        }
コード例 #11
0
        private void TestExecution(
            string activityName,
            Action <Task <bool>, ActivitySource, string> createTask,
            Action <TaskCompletionSource <bool> > finishTask,
            ActivityResult expectedResult)
        {
            using TestActivityListener listener = new TestActivityListener(activityName);
            ActivitySource source = new ActivitySource(activityName);

            TaskCompletionSource <bool> taskCompletionSource = new TaskCompletionSource <bool>();

            createTask(taskCompletionSource.Task, source, activityName);

            Activity?activity = listener.Activities.Single();

            NullableAssert.IsNotNull(activity);

            activity.AssertResult(ActivityResult.SystemError);

            finishTask(taskCompletionSource);

            activity.AssertResult(expectedResult);
        }
コード例 #12
0
 public void Fail_Throws()
 {
     Assert.ThrowsException <AssertFailedException>(() => NullableAssert.Fail());
 }
コード例 #13
0
 public void IsNotNull_IfNotNull_NotThrows()
 {
     NullableAssert.IsNotNull(new object());
 }
コード例 #14
0
 public void CheckIsTrue(object?test)
 {
     NullableAssert.IsTrue(test != null);
     test.GetHashCode();                 // should allow calling method on nullable variable without check
 }
コード例 #15
0
 public void IsNotNull_IfNull_Throws()
 {
     Assert.ThrowsException <AssertFailedException>(() => NullableAssert.IsNotNull(null));
 }
コード例 #16
0
 public void IsTrue_IfTrue_NotThrows()
 {
     NullableAssert.IsTrue(true);
 }
コード例 #17
0
 public void IsFalse_IfFalse_NotThrows()
 {
     NullableAssert.IsFalse(false);
 }
コード例 #18
0
 public void IsFalse_IfTrue_Throws()
 {
     Assert.ThrowsException <AssertFailedException>(() => NullableAssert.IsFalse(true));
 }
コード例 #19
0
        public async Task PublishAsync_ForValidReportWithSeveralEntries_ReportsCorrectSfHealthInformation()
        {
            // Arrange.
            PublisherTestContext testContext = new PublisherTestContext();

            testContext.ConfigureStatelessServicePartition();

            string            healthyCheckName = "HealthyCheck";
            HealthReportEntry healthyEntry     = new HealthReportEntry(
                status: HealthStatus.Healthy,
                description: "Healthy entry description",
                duration: TimeSpan.FromMilliseconds(12639),
                exception: null,
                data: null);

            string            degradedCheckName = "DegradedCheck";
            HealthReportEntry degradedEntry     = new HealthReportEntry(
                status: HealthStatus.Degraded,
                description: "Degraded entry description",
                duration: TimeSpan.FromMilliseconds(111),
                exception: null,
                data: null);

            string            unhealthyCheckName = "UnhealthyCheck";
            HealthReportEntry unhealthyEntry     = new HealthReportEntry(
                status: HealthStatus.Unhealthy,
                description: "Unhealthy entry description",
                duration: TimeSpan.FromMilliseconds(73),
                exception: new InvalidOperationException("Unhealthy check performed invalid operation."),
                data: null);

            HealthReport report = new HealthReportBuilder()
                                  .AddEntry(healthyCheckName, healthyEntry)
                                  .AddEntry(degradedCheckName, degradedEntry)
                                  .AddEntry(unhealthyCheckName, unhealthyEntry)
                                  .SetTotalDuration(TimeSpan.FromSeconds(72))
                                  .ToHealthReport();

            // Act.
            await testContext.Publisher.PublishAsync(report, cancellationToken : default);

            // Assert.
            SfHealthInformation?healthyInfo = testContext.ReportedSfHealthInformation(healthyCheckName);

            NullableAssert.IsNotNull(healthyInfo);

            Assert.AreEqual(ServiceFabricHealthCheckPublisher.HealthReportSourceId, healthyInfo.SourceId, "SourceId from healthy check is incorrect.");
            Assert.AreEqual(healthyCheckName, healthyInfo.Property, "Property from healthy check is incorrect.");
            Assert.AreEqual(SfHealthState.Ok, healthyInfo.HealthState, "HealthState from healthy check is incorrect.");
            StringAssert.Contains(healthyInfo.Description, healthyEntry.Description, "Description from healthy check is not included.");
            StringAssert.Contains(healthyInfo.Description, healthyEntry.Duration.ToString(), "Duration from healthy check is not included.");

            SfHealthInformation?degradedInfo = testContext.ReportedSfHealthInformation(degradedCheckName);

            NullableAssert.IsNotNull(degradedInfo);

            Assert.AreEqual(ServiceFabricHealthCheckPublisher.HealthReportSourceId, degradedInfo.SourceId, "SourceId from degraded check is incorrect.");
            Assert.AreEqual(degradedCheckName, degradedInfo.Property, "Property from degraded check is incorrect.");
            Assert.AreEqual(SfHealthState.Warning, degradedInfo.HealthState, "HealthState from degraded check is incorrect.");
            StringAssert.Contains(degradedInfo.Description, degradedEntry.Description, "Description from degraded check is not included.");
            StringAssert.Contains(degradedInfo.Description, degradedEntry.Duration.ToString(), "Duration from degraded check is not included.");

            SfHealthInformation?unhealthyInfo = testContext.ReportedSfHealthInformation(unhealthyCheckName);

            NullableAssert.IsNotNull(unhealthyInfo);

            Assert.AreEqual(ServiceFabricHealthCheckPublisher.HealthReportSourceId, unhealthyInfo.SourceId, "SourceId from unhealthy check is incorrect.");
            Assert.AreEqual(unhealthyCheckName, unhealthyInfo.Property, "Property from unhealthy check is incorrect.");
            Assert.AreEqual(SfHealthState.Error, unhealthyInfo.HealthState, "HealthState from unhealthy check is incorrect.");
            StringAssert.Contains(unhealthyInfo.Description, unhealthyEntry.Description, "Description from unhealthy check is not included.");
            StringAssert.Contains(unhealthyInfo.Description, unhealthyEntry.Duration.ToString(), "Duration from unhealthy check is not included.");
            StringAssert.Contains(unhealthyInfo.Description, unhealthyEntry.Exception?.GetType().ToString(), "Exception from unhealthy check is not included.");

            SfHealthInformation?summaryInfo = testContext.ReportedSfHealthInformation(ServiceFabricHealthCheckPublisher.HealthReportSummaryProperty);

            NullableAssert.IsNotNull(summaryInfo);

            Assert.AreEqual(ServiceFabricHealthCheckPublisher.HealthReportSourceId, summaryInfo.SourceId, "SourceId from report is incorrect.");
            Assert.AreEqual(ServiceFabricHealthCheckPublisher.HealthReportSummaryProperty, summaryInfo.Property, "Property from report is incorrect.");
            Assert.AreEqual(SfHealthState.Error, summaryInfo.HealthState, "HealthState from report is incorrect.");
            StringAssert.Contains(summaryInfo.Description, report.TotalDuration.ToString(), "TotalDuration from report is not included.");
        }