Skip to content

Commit 11ff616

Browse files
committed
more coverage to cover the Result property retrieval from different Task types.
1 parent ccc3268 commit 11ff616

File tree

1 file changed

+41
-27
lines changed

1 file changed

+41
-27
lines changed

libraries/tests/AWS.Lambda.Powertools.Tracing.Tests/TracingAspectTests.cs

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -525,44 +525,58 @@ public void CaptureError_WithDifferentModes_ReturnsExpectedResult(TracingCapture
525525
}
526526

527527
[Fact]
528-
public void CaptureResponse_WhenTracingIsDisabled_ReturnsFalseDirectly()
528+
public async Task Around_AsyncMethodWithResult_HandlesTaskResultProperty()
529529
{
530530
// Arrange
531-
_mockConfigurations.TracingDisabled.Returns(true);
532-
var methodName = "TestMethod";
533-
var result = "test result";
534-
Func<object[], object> target = _ => result;
531+
var attribute = new TracingAttribute();
532+
var methodName = "TestAsyncMethod";
533+
var expectedResult = "test result";
534+
var taskWithResult = Task.FromResult(expectedResult);
535+
Func<object[], object> target = _ => taskWithResult;
535536

536537
// Act
537-
_handler.Around(methodName, Array.Empty<object>(), target, new Attribute[] { new TracingAttribute() });
538+
var taskResult = _handler.Around(methodName, Array.Empty<object>(), target, new Attribute[] { attribute });
538539

539-
// Assert - No metadata should be added since we return false immediately
540-
_mockXRayRecorder.DidNotReceive().AddMetadata(
541-
Arg.Any<string>(),
542-
Arg.Any<string>(),
543-
Arg.Any<object>());
540+
// Wait for the async operation to complete
541+
if (taskResult is Task task)
542+
{
543+
await task;
544+
}
545+
546+
// Assert with wait
547+
await Task.Delay(100); // Give time for the continuation to complete
548+
549+
_mockXRayRecorder.Received(1).AddMetadata(
550+
"TestService", // This matches what's set in the test constructor
551+
$"{methodName} response",
552+
expectedResult);
544553
}
545554

546555
[Fact]
547-
public void CaptureError_WhenTracingIsDisabled_ReturnsFalseDirectly()
556+
public async Task Around_AsyncMethodWithoutResult_HandlesNullTaskResultProperty()
548557
{
549558
// Arrange
550-
_mockConfigurations.TracingDisabled.Returns(true);
551-
var methodName = "TestMethod";
552-
var expectedException = new Exception("Test exception");
553-
Func<object[], object> target = _ => throw expectedException;
559+
var attribute = new TracingAttribute();
560+
var methodName = "TestAsyncMethod";
561+
var taskWithoutResult = new Task(() => { }); // Task without Result property
562+
taskWithoutResult.Start();
563+
Func<object[], object> target = _ => taskWithoutResult;
554564

555-
// Act & Assert
556-
Assert.Throws<Exception>(() => _handler.Around(
557-
methodName,
558-
Array.Empty<object>(),
559-
target,
560-
new Attribute[] { new TracingAttribute() }));
565+
// Act
566+
var taskResult = _handler.Around(methodName, Array.Empty<object>(), target, new Attribute[] { attribute });
561567

562-
// Verify no error metadata was added since we return false immediately
563-
_mockXRayRecorder.DidNotReceive().AddMetadata(
564-
Arg.Any<string>(),
565-
Arg.Any<string>(),
566-
Arg.Any<string>());
568+
// Wait for the async operation to complete
569+
if (taskResult is Task task)
570+
{
571+
await task;
572+
}
573+
574+
// Assert with wait
575+
await Task.Delay(100); // Give time for the continuation to complete
576+
577+
_mockXRayRecorder.Received(1).AddMetadata(
578+
"TestService",
579+
$"{methodName} response",
580+
null);
567581
}
568582
}

0 commit comments

Comments
 (0)