|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Reflection; |
| 7 | +using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace NuGet.Services.Logging.Tests |
| 11 | +{ |
| 12 | + public class DiagnosticsTelemetryModuleExtensionsTests |
| 13 | + { |
| 14 | + public class TheAddOrSetHeartbeatPropertyMethod |
| 15 | + { |
| 16 | + [Fact] |
| 17 | + public void ThrowsForNullModule() |
| 18 | + { |
| 19 | + Assert.Throws<ArgumentNullException>( |
| 20 | + "module", |
| 21 | + () => DiagnosticsTelemetryModuleExtensions.AddOrSetHeartbeatProperty(null, "name", "value", true)); |
| 22 | + } |
| 23 | + |
| 24 | + [Fact] |
| 25 | + public void ReturnsFalseForNullPropertyName() |
| 26 | + { |
| 27 | + // Arrange |
| 28 | + var module = new DiagnosticsTelemetryModule(); |
| 29 | + |
| 30 | + // Act |
| 31 | + var result = DiagnosticsTelemetryModuleExtensions.AddOrSetHeartbeatProperty(module, null, "value", true); |
| 32 | + |
| 33 | + // Assert |
| 34 | + Assert.False(result); |
| 35 | + } |
| 36 | + |
| 37 | + [Theory] |
| 38 | + [InlineData("propertyName", "propertyValue", true)] |
| 39 | + [InlineData("propertyName", "propertyValue", false)] |
| 40 | + public void ReturnsTrueAndAddsPayloadWhenNotAddedYet(string propertyName, string propertyValue, bool isHealthy) |
| 41 | + { |
| 42 | + // Arrange |
| 43 | + var module = new DiagnosticsTelemetryModule(); |
| 44 | + |
| 45 | + // Act |
| 46 | + var result = DiagnosticsTelemetryModuleExtensions.AddOrSetHeartbeatProperty( |
| 47 | + module, |
| 48 | + propertyName, |
| 49 | + propertyValue, |
| 50 | + isHealthy); |
| 51 | + |
| 52 | + // Assert |
| 53 | + Assert.True(result); |
| 54 | + |
| 55 | + VerifyHeartbeatPropertyPayload(module, propertyName, propertyValue, isHealthy); |
| 56 | + } |
| 57 | + |
| 58 | + [Theory] |
| 59 | + [InlineData("propertyName", "propertyValue", true)] |
| 60 | + [InlineData("propertyName", "propertyValue", false)] |
| 61 | + public void ReturnsTrueAndSetsPayloadWhenAlreadyAdded(string propertyName, string propertyValue, bool isHealthy) |
| 62 | + { |
| 63 | + // Arrange |
| 64 | + var module = new DiagnosticsTelemetryModule(); |
| 65 | + module.AddHeartbeatProperty(propertyName, propertyValue, isHealthy); |
| 66 | + |
| 67 | + // Act |
| 68 | + var result = DiagnosticsTelemetryModuleExtensions.AddOrSetHeartbeatProperty( |
| 69 | + module, |
| 70 | + propertyName, |
| 71 | + propertyValue, |
| 72 | + isHealthy); |
| 73 | + |
| 74 | + // Assert |
| 75 | + Assert.True(result); |
| 76 | + |
| 77 | + VerifyHeartbeatPropertyPayload(module, propertyName, propertyValue, isHealthy); |
| 78 | + } |
| 79 | + |
| 80 | + private void VerifyHeartbeatPropertyPayload( |
| 81 | + DiagnosticsTelemetryModule module, |
| 82 | + string propertyName, |
| 83 | + string propertyValue, |
| 84 | + bool isHealthy) |
| 85 | + { |
| 86 | + // Verify payload settings using reflection |
| 87 | + var heartbeatPropertyManager = typeof(DiagnosticsTelemetryModule) |
| 88 | + .GetField("HeartbeatProvider", BindingFlags.NonPublic | BindingFlags.Instance) |
| 89 | + .GetValue(module) as IHeartbeatPropertyManager; |
| 90 | + |
| 91 | + var heartbeatProperties = heartbeatPropertyManager |
| 92 | + .GetType() |
| 93 | + .GetField("heartbeatProperties", BindingFlags.NonPublic | BindingFlags.Instance) |
| 94 | + .GetValue(heartbeatPropertyManager); // returns ConcurrentDictionary<string, HeartbeatPropertyPayload> |
| 95 | + |
| 96 | + var propertyNames = heartbeatProperties |
| 97 | + .GetType() |
| 98 | + .GetProperty("Keys", BindingFlags.Public | BindingFlags.Instance) |
| 99 | + .GetValue(heartbeatProperties) as ICollection<string>; |
| 100 | + |
| 101 | + Assert.Contains(propertyNames, i => i.Equals(propertyName)); |
| 102 | + |
| 103 | + var propertyPayload = heartbeatProperties |
| 104 | + .GetType() |
| 105 | + .GetProperty("Item", BindingFlags.Public | BindingFlags.Instance) |
| 106 | + .GetValue(heartbeatProperties, new object[] { propertyName }); // returns internal type HeartbeatPropertyPayload |
| 107 | + |
| 108 | + var payloadValue = propertyPayload |
| 109 | + .GetType() |
| 110 | + .GetProperty("PayloadValue", BindingFlags.Public | BindingFlags.Instance) |
| 111 | + .GetValue(propertyPayload) as string; |
| 112 | + |
| 113 | + var payloadIsHealthy = (bool)propertyPayload |
| 114 | + .GetType() |
| 115 | + .GetProperty("IsHealthy", BindingFlags.Public | BindingFlags.Instance) |
| 116 | + .GetValue(propertyPayload); |
| 117 | + |
| 118 | + Assert.Equal(propertyValue, payloadValue); |
| 119 | + Assert.Equal(isHealthy, payloadIsHealthy); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments