|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Linq; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.Azure.WebJobs.Script.WebHost; |
| 10 | +using Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics; |
| 11 | +using Microsoft.Extensions.Options; |
| 12 | +using Moq; |
| 13 | +using Xunit; |
| 14 | +using static Microsoft.Azure.WebJobs.Script.ScriptConstants; |
| 15 | + |
| 16 | +namespace Microsoft.Azure.WebJobs.Script.Tests.Diagnostics |
| 17 | +{ |
| 18 | + public class DiagnosticListenerServiceTests |
| 19 | + { |
| 20 | + /// <summary> |
| 21 | + /// This test ensures that: |
| 22 | + /// 1 - Diagnostic sources with the host prefix is correctly observed |
| 23 | + /// 2 - Diagnostic sources with names not starting with the host prefix |
| 24 | + /// are not observed. |
| 25 | + /// </summary> |
| 26 | + [Fact] |
| 27 | + public async Task CreatesExpectedListeners() |
| 28 | + { |
| 29 | + var logger = new TestLogger<DiagnosticListenerService>(); |
| 30 | + var om = new Mock<IOptionsMonitor<StandbyOptions>>(); |
| 31 | + om.Setup(m => m.CurrentValue).Returns(new StandbyOptions()); |
| 32 | + |
| 33 | + var service = new DiagnosticListenerService(logger, om.Object); |
| 34 | + |
| 35 | + await service.StartAsync(CancellationToken.None); |
| 36 | + |
| 37 | + // Host diagnostic source |
| 38 | + using var source = new DiagnosticListener(HostDiagnosticSourcePrefix + "Test"); |
| 39 | + source.Write("TestEvent", "test"); |
| 40 | + |
| 41 | + // Diagnostic source without hosts prefix |
| 42 | + using var source2 = new DiagnosticListener("OtherSource.Test"); |
| 43 | + source2.Write("TestEvent2", "test"); |
| 44 | + |
| 45 | + Assert.Contains(GetEventMessge(source.Name, "TestEvent", "test"), logger.GetLogMessages().Select(l => l.FormattedMessage)); |
| 46 | + |
| 47 | + // This assertion accounts for the log message written when a listener handler is created. |
| 48 | + Assert.Equal(2, logger.GetLogMessages().Count); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// This test ensures that enablement evaluation for events starting wiht the debug prefix |
| 53 | + /// return false if debug tracing is not enabled. |
| 54 | + /// </summary> |
| 55 | + [Theory] |
| 56 | + [InlineData(true)] |
| 57 | + [InlineData(false)] |
| 58 | + public async Task CapturesDebugEvents_WhenDebugTracesAreEnabled(bool debugTracingEnabled) |
| 59 | + { |
| 60 | + TestScopedEnvironmentVariable featureFlags = null; |
| 61 | + |
| 62 | + try |
| 63 | + { |
| 64 | + if (debugTracingEnabled) |
| 65 | + { |
| 66 | + featureFlags = new TestScopedEnvironmentVariable(EnvironmentSettingNames.AzureWebJobsFeatureFlags, FeatureFlagEnableDebugTracing); |
| 67 | + } |
| 68 | + |
| 69 | + var logger = new TestLogger<DiagnosticListenerService>(); |
| 70 | + var om = new Mock<IOptionsMonitor<StandbyOptions>>(); |
| 71 | + om.Setup(m => m.CurrentValue).Returns(new StandbyOptions()); |
| 72 | + |
| 73 | + var service = new DiagnosticListenerService(logger, om.Object); |
| 74 | + |
| 75 | + await service.StartAsync(CancellationToken.None); |
| 76 | + |
| 77 | + // Host diagnostic source |
| 78 | + using var source = new DiagnosticListener(HostDiagnosticSourcePrefix + "Test"); |
| 79 | + |
| 80 | + // Debug event. This will only be captured if debug tracing is enabled: |
| 81 | + var eventName = HostDiagnosticSourceDebugEventNamePrefix + "TestEvent"; |
| 82 | + if (source.IsEnabled(eventName)) |
| 83 | + { |
| 84 | + source.Write(eventName, "test debug"); |
| 85 | + } |
| 86 | + |
| 87 | + // This event should always be captured |
| 88 | + source.Write("TestEvent", "test"); |
| 89 | + |
| 90 | + var messages = logger.GetLogMessages().Select(l => l.FormattedMessage).ToList(); |
| 91 | + |
| 92 | + Assert.Contains(GetEventMessge(source.Name, "TestEvent", "test"), messages); |
| 93 | + |
| 94 | + if (debugTracingEnabled) |
| 95 | + { |
| 96 | + Assert.Contains(GetEventMessge(source.Name, eventName, "test debug"), messages); |
| 97 | + } |
| 98 | + |
| 99 | + int expectedMessageCount = debugTracingEnabled ? 3 : 2; |
| 100 | + // This assertion accounts for the log message written when a listener handler is created. |
| 101 | + Assert.Equal(expectedMessageCount, messages.Count); |
| 102 | + |
| 103 | + Assert.Equal(debugTracingEnabled, source.IsEnabled(eventName)); |
| 104 | + } |
| 105 | + finally |
| 106 | + { |
| 107 | + featureFlags?.Dispose(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// This test ensures that specialization events trigger evaluation of the debug flag and source subscription. |
| 113 | + /// </summary> |
| 114 | + [Fact] |
| 115 | + public async Task StandbyChanges_TriggerFeatureEvaluation() |
| 116 | + { |
| 117 | + Action<StandbyOptions, string> action = null; |
| 118 | + void ChangeCallback(Action<StandbyOptions, string> callback) |
| 119 | + { |
| 120 | + action = callback; |
| 121 | + } |
| 122 | + |
| 123 | + var logger = new TestLogger<DiagnosticListenerService>(); |
| 124 | + var om = new Mock<IOptionsMonitor<StandbyOptions>>(); |
| 125 | + om.Setup(m => m.CurrentValue).Returns(new StandbyOptions { InStandbyMode = true }); |
| 126 | + om.Setup(m => m.OnChange(It.IsAny<Action<StandbyOptions, string>>())) |
| 127 | + .Callback(ChangeCallback); |
| 128 | + |
| 129 | + var service = new DiagnosticListenerService(logger, om.Object); |
| 130 | + |
| 131 | + await service.StartAsync(CancellationToken.None); |
| 132 | + |
| 133 | + // Host diagnostic source |
| 134 | + using var source = new DiagnosticListener(HostDiagnosticSourcePrefix + "Test"); |
| 135 | + |
| 136 | + // Debug event. This will only be captured if debug tracing is enabled: |
| 137 | + var eventName = HostDiagnosticSourceDebugEventNamePrefix + "TestEvent"; |
| 138 | + |
| 139 | + Assert.False(source.IsEnabled(eventName)); |
| 140 | + |
| 141 | + using var featureFlags = new TestScopedEnvironmentVariable(EnvironmentSettingNames.AzureWebJobsFeatureFlags, FeatureFlagEnableDebugTracing); |
| 142 | + |
| 143 | + // Invoke change callback |
| 144 | + action(new StandbyOptions(), string.Empty); |
| 145 | + |
| 146 | + Assert.True(source.IsEnabled(eventName)); |
| 147 | + } |
| 148 | + |
| 149 | + private static string GetEventMessge(string source, string eventName, string payload) |
| 150 | + => $"Diagnostic source '{source}' emitted event '{eventName}': {payload}"; |
| 151 | + } |
| 152 | +} |
0 commit comments