|
1 | 1 | // Copyright (c) Microsoft Corporation. |
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
4 | | -using Microsoft.DurableTask.Entities; |
5 | 4 | using Microsoft.Extensions.Logging; |
6 | 5 |
|
7 | 6 | namespace Microsoft.DurableTask.Tests; |
@@ -65,17 +64,53 @@ public void ReplaySafeLoggerFactory_AddProvider_ThrowsWithoutMutatingUnderlyingF |
65 | 64 | TrackingLoggerProvider provider = new(); |
66 | 65 | TrackingLoggerFactory loggerFactory = new(provider); |
67 | 66 | TestTaskOrchestrationContext context = new(loggerFactory, isReplaying: false); |
68 | | - Mock<ILoggerProvider> additionalProvider = new(); |
| 67 | + TrackingLoggerProvider additionalProvider = new(); |
69 | 68 |
|
70 | 69 | // Act |
71 | | - Action act = () => context.ReplaySafeLoggerFactory.AddProvider(additionalProvider.Object); |
| 70 | + Action act = () => context.ReplaySafeLoggerFactory.AddProvider(additionalProvider); |
72 | 71 |
|
73 | 72 | // Assert |
74 | 73 | act.Should().Throw<NotSupportedException>() |
75 | 74 | .WithMessage("*replay-safe logger factory*not supported*"); |
76 | 75 | loggerFactory.AddProviderCallCount.Should().Be(0); |
77 | 76 | } |
78 | 77 |
|
| 78 | + [Fact] |
| 79 | + public void ReplaySafeLoggerFactory_CreateLogger_FromWrappedContext_ChecksReplayOnce() |
| 80 | + { |
| 81 | + // Arrange |
| 82 | + TrackingLoggerProvider provider = new(); |
| 83 | + TrackingLoggerFactory loggerFactory = new(provider); |
| 84 | + TestTaskOrchestrationContext innerContext = new(loggerFactory, isReplaying: false); |
| 85 | + WrappingTaskOrchestrationContext wrappedContext = new(innerContext); |
| 86 | + ILogger logger = wrappedContext.ReplaySafeLoggerFactory.CreateLogger("ReplaySafe"); |
| 87 | + |
| 88 | + // Act |
| 89 | + logger.LogInformation("This log should be written."); |
| 90 | + |
| 91 | + // Assert |
| 92 | + innerContext.IsReplayingAccessCount.Should().Be(1); |
| 93 | + provider.Entries.Should().ContainSingle(entry => |
| 94 | + entry.CategoryName == "ReplaySafe" && |
| 95 | + entry.Message.Contains("This log should be written.", StringComparison.Ordinal)); |
| 96 | + } |
| 97 | + |
| 98 | + [Fact] |
| 99 | + public void ReplaySafeLoggerFactory_CreateLogger_ThrowsOnCyclicLoggerFactory() |
| 100 | + { |
| 101 | + // Arrange |
| 102 | + TrackingLoggerProvider provider = new(); |
| 103 | + TrackingLoggerFactory loggerFactory = new(provider); |
| 104 | + SelfReferencingContext cyclicContext = new(loggerFactory); |
| 105 | + |
| 106 | + // Act |
| 107 | + Action act = () => cyclicContext.ReplaySafeLoggerFactory.CreateLogger("Test"); |
| 108 | + |
| 109 | + // Assert |
| 110 | + act.Should().Throw<InvalidOperationException>() |
| 111 | + .WithMessage("*Cycle detected*"); |
| 112 | + } |
| 113 | + |
79 | 114 | [Fact] |
80 | 115 | public void ReplaySafeLoggerFactory_Dispose_DoesNotDisposeUnderlyingFactory() |
81 | 116 | { |
@@ -110,11 +145,18 @@ public TestTaskOrchestrationContext(ILoggerFactory loggerFactory, bool isReplayi |
110 | 145 |
|
111 | 146 | public override DateTime CurrentUtcDateTime => DateTime.UnixEpoch; |
112 | 147 |
|
113 | | - public override bool IsReplaying => this.isReplaying; |
| 148 | + public int IsReplayingAccessCount { get; private set; } |
114 | 149 |
|
115 | | - public override IReadOnlyDictionary<string, object?> Properties => new Dictionary<string, object?>(); |
| 150 | + public override bool IsReplaying |
| 151 | + { |
| 152 | + get |
| 153 | + { |
| 154 | + this.IsReplayingAccessCount++; |
| 155 | + return this.isReplaying; |
| 156 | + } |
| 157 | + } |
116 | 158 |
|
117 | | - public override TaskOrchestrationEntityFeature Entities => throw new NotSupportedException(); |
| 159 | + public override IReadOnlyDictionary<string, object?> Properties => new Dictionary<string, object?>(); |
118 | 160 |
|
119 | 161 | protected override ILoggerFactory LoggerFactory => this.loggerFactory; |
120 | 162 |
|
@@ -150,6 +192,119 @@ public override Guid NewGuid() |
150 | 192 | => throw new NotImplementedException(); |
151 | 193 | } |
152 | 194 |
|
| 195 | + sealed class WrappingTaskOrchestrationContext : TaskOrchestrationContext |
| 196 | + { |
| 197 | + readonly TaskOrchestrationContext innerContext; |
| 198 | + |
| 199 | + public WrappingTaskOrchestrationContext(TaskOrchestrationContext innerContext) |
| 200 | + { |
| 201 | + this.innerContext = innerContext ?? throw new ArgumentNullException(nameof(innerContext)); |
| 202 | + } |
| 203 | + |
| 204 | + public override TaskName Name => this.innerContext.Name; |
| 205 | + |
| 206 | + public override string InstanceId => this.innerContext.InstanceId; |
| 207 | + |
| 208 | + public override ParentOrchestrationInstance? Parent => this.innerContext.Parent; |
| 209 | + |
| 210 | + public override DateTime CurrentUtcDateTime => this.innerContext.CurrentUtcDateTime; |
| 211 | + |
| 212 | + public override bool IsReplaying => this.innerContext.IsReplaying; |
| 213 | + |
| 214 | + public override string Version => this.innerContext.Version; |
| 215 | + |
| 216 | + public override IReadOnlyDictionary<string, object?> Properties => this.innerContext.Properties; |
| 217 | + |
| 218 | + protected override ILoggerFactory LoggerFactory => this.innerContext.ReplaySafeLoggerFactory; |
| 219 | + |
| 220 | + public override T GetInput<T>() |
| 221 | + where T : default |
| 222 | + => this.innerContext.GetInput<T>()!; |
| 223 | + |
| 224 | + public override Task<TResult> CallActivityAsync<TResult>(TaskName name, object? input = null, TaskOptions? options = null) |
| 225 | + => this.innerContext.CallActivityAsync<TResult>(name, input, options); |
| 226 | + |
| 227 | + public override Task CreateTimer(DateTime fireAt, CancellationToken cancellationToken) |
| 228 | + => this.innerContext.CreateTimer(fireAt, cancellationToken); |
| 229 | + |
| 230 | + public override Task<T> WaitForExternalEvent<T>(string eventName, CancellationToken cancellationToken = default) |
| 231 | + => this.innerContext.WaitForExternalEvent<T>(eventName, cancellationToken); |
| 232 | + |
| 233 | + public override void SendEvent(string instanceId, string eventName, object payload) |
| 234 | + => this.innerContext.SendEvent(instanceId, eventName, payload); |
| 235 | + |
| 236 | + public override void SetCustomStatus(object? customStatus) |
| 237 | + => this.innerContext.SetCustomStatus(customStatus); |
| 238 | + |
| 239 | + public override Task<TResult> CallSubOrchestratorAsync<TResult>( |
| 240 | + TaskName orchestratorName, |
| 241 | + object? input = null, |
| 242 | + TaskOptions? options = null) |
| 243 | + => this.innerContext.CallSubOrchestratorAsync<TResult>(orchestratorName, input, options); |
| 244 | + |
| 245 | + public override void ContinueAsNew(object? newInput = null, bool preserveUnprocessedEvents = true) |
| 246 | + => this.innerContext.ContinueAsNew(newInput, preserveUnprocessedEvents); |
| 247 | + |
| 248 | + public override Guid NewGuid() |
| 249 | + => this.innerContext.NewGuid(); |
| 250 | + } |
| 251 | + |
| 252 | + sealed class SelfReferencingContext : TaskOrchestrationContext |
| 253 | + { |
| 254 | + readonly ILoggerFactory loggerFactory; |
| 255 | + |
| 256 | + public SelfReferencingContext(ILoggerFactory loggerFactory) |
| 257 | + { |
| 258 | + this.loggerFactory = loggerFactory; |
| 259 | + } |
| 260 | + |
| 261 | + public override TaskName Name => default; |
| 262 | + |
| 263 | + public override string InstanceId => "cyclic-instance"; |
| 264 | + |
| 265 | + public override ParentOrchestrationInstance? Parent => null; |
| 266 | + |
| 267 | + public override DateTime CurrentUtcDateTime => DateTime.UnixEpoch; |
| 268 | + |
| 269 | + public override bool IsReplaying => false; |
| 270 | + |
| 271 | + public override IReadOnlyDictionary<string, object?> Properties => new Dictionary<string, object?>(); |
| 272 | + |
| 273 | + // Bug: points at self instead of an inner context — should cause cycle detection. |
| 274 | + protected override ILoggerFactory LoggerFactory => this.ReplaySafeLoggerFactory; |
| 275 | + |
| 276 | + public override T GetInput<T>() |
| 277 | + where T : default |
| 278 | + => default!; |
| 279 | + |
| 280 | + public override Task<TResult> CallActivityAsync<TResult>(TaskName name, object? input = null, TaskOptions? options = null) |
| 281 | + => throw new NotImplementedException(); |
| 282 | + |
| 283 | + public override Task CreateTimer(DateTime fireAt, CancellationToken cancellationToken) |
| 284 | + => throw new NotImplementedException(); |
| 285 | + |
| 286 | + public override Task<T> WaitForExternalEvent<T>(string eventName, CancellationToken cancellationToken = default) |
| 287 | + => throw new NotImplementedException(); |
| 288 | + |
| 289 | + public override void SendEvent(string instanceId, string eventName, object payload) |
| 290 | + => throw new NotImplementedException(); |
| 291 | + |
| 292 | + public override void SetCustomStatus(object? customStatus) |
| 293 | + => throw new NotImplementedException(); |
| 294 | + |
| 295 | + public override Task<TResult> CallSubOrchestratorAsync<TResult>( |
| 296 | + TaskName orchestratorName, |
| 297 | + object? input = null, |
| 298 | + TaskOptions? options = null) |
| 299 | + => throw new NotImplementedException(); |
| 300 | + |
| 301 | + public override void ContinueAsNew(object? newInput = null, bool preserveUnprocessedEvents = true) |
| 302 | + => throw new NotImplementedException(); |
| 303 | + |
| 304 | + public override Guid NewGuid() |
| 305 | + => throw new NotImplementedException(); |
| 306 | + } |
| 307 | + |
153 | 308 | sealed class TrackingLoggerFactory : ILoggerFactory |
154 | 309 | { |
155 | 310 | readonly TrackingLoggerProvider provider; |
|
0 commit comments