From 6cb2651a3439b50d4dc5d54ed78422f38555192c Mon Sep 17 00:00:00 2001 From: Laurynas Ruskys Date: Wed, 27 Sep 2023 17:32:49 +0300 Subject: [PATCH] Add some variance into tests to validate custom logging scope key --- .../CorrelationManagerTests.cs | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/test/Correlate.Core.Tests/CorrelationManagerTests.cs b/test/Correlate.Core.Tests/CorrelationManagerTests.cs index dc28cc6..7cf417b 100644 --- a/test/Correlate.Core.Tests/CorrelationManagerTests.cs +++ b/test/Correlate.Core.Tests/CorrelationManagerTests.cs @@ -1,5 +1,4 @@ using System.Collections; -using System.Diagnostics; using Correlate.Testing; using Correlate.Testing.TestCases; using Microsoft.Extensions.Logging; @@ -21,7 +20,7 @@ public class CorrelationManagerTests : IDisposable private readonly SerilogLoggerProvider _logProvider; private readonly CorrelationManager _sut; - protected CorrelationManagerTests() + protected CorrelationManagerTests(CorrelationManagerOptions options) { _correlationContextAccessor = new CorrelationContextAccessor(); @@ -36,7 +35,7 @@ protected CorrelationManagerTests() _logProvider = new SerilogLoggerProvider(serilogLogger); _logger = new TestLogger(_logProvider.CreateLogger(nameof(CorrelationManager))); - _options = Options.Create(new CorrelationManagerOptions()); + _options = Options.Create(options); _sut = new CorrelationManager( new CorrelationContextFactory(_correlationContextAccessor), @@ -55,6 +54,13 @@ public void Dispose() public class Async : CorrelationManagerTests { + public Async() : base(new() + { + LoggingScopeKey = "ActivityId" + }) + { + } + [Fact] public async Task Given_a_task_should_run_task_inside_correlated_context() { @@ -148,7 +154,7 @@ await _sut.CorrelateAsync(() => var logEvents = TestCorrelator.GetLogEventsFromCurrentContext().ToList(); logEvents.Should() .HaveCount(3) - .And.ContainSingle(ev => ev.MessageTemplate.Text == "Message with correlation id." && ev.Properties.ContainsKey("CorrelationId")); + .And.ContainSingle(ev => ev.MessageTemplate.Text == "Message with correlation id." && ev.Properties.ContainsKey("ActivityId")); } } @@ -236,7 +242,7 @@ Task ThrowingTask() .Cast() .ToDictionary(kvp => kvp.Key, kvp => kvp.Value) .Should() - .ContainKey(CorrelateConstants.CorrelationIdKey) + .ContainKey("ActivityId") .WhoseValue.Should() .Be(GeneratedCorrelationId); } @@ -366,6 +372,10 @@ await _sut.CorrelateAsync(innerContextId, public class Sync : CorrelationManagerTests { + public Sync() : base(new()) + { + } + [Fact] public void Given_a_action_should_run_action_inside_correlated_context() {