Skip to content

Commit 37473c8

Browse files
committed
add full test scenario. refactor
1 parent 4878968 commit 37473c8

File tree

6 files changed

+123
-16
lines changed

6 files changed

+123
-16
lines changed

libraries/tests/AWS.Lambda.Powertools.Tracing.Tests/AWS.Lambda.Powertools.Tracing.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
<ItemGroup>
1414
<!-- Package versions are Centrally managed in Directory.Packages.props file -->
1515
<!-- More info https://learn.microsoft.com/en-us/nuget/consume-packages/central-package-management -->
16+
<PackageReference Include="Amazon.Lambda.Core" />
17+
<PackageReference Include="Amazon.Lambda.TestUtilities" />
1618
<PackageReference Include="coverlet.collector">
1719
<PrivateAssets>all</PrivateAssets>
1820
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

libraries/tests/AWS.Lambda.Powertools.Tracing.Tests/Handlers/FullExampleHandler.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,42 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16+
using System.Threading.Tasks;
17+
using Amazon.Lambda.Core;
18+
1619
namespace AWS.Lambda.Powertools.Tracing.Tests.Handlers;
1720

1821
public class FullExampleHandler
1922
{
20-
[Tracing()]
21-
public string[] Handle()
23+
[Tracing(Namespace = "ns", CaptureMode = TracingCaptureMode.ResponseAndError)]
24+
public Task<string> Handle(string text, ILambdaContext context)
25+
{
26+
Tracing.AddAnnotation("annotation", "value");
27+
BusinessLogic1().GetAwaiter().GetResult();
28+
29+
return Task.FromResult(text.ToUpper());
30+
}
31+
32+
[Tracing(SegmentName = "First Call")]
33+
private async Task BusinessLogic1()
34+
{
35+
await BusinessLogic2();
36+
}
37+
38+
[Tracing(CaptureMode = TracingCaptureMode.Disabled)]
39+
private async Task BusinessLogic2()
40+
{
41+
Tracing.AddMetadata("metadata", "value");
42+
43+
Tracing.WithSubsegment("localNamespace", "GetSomething", (subsegment) => {
44+
GetSomething();
45+
});
46+
47+
await Task.FromResult(0);
48+
}
49+
50+
private void GetSomething()
2251
{
23-
return new[] { "A", "B" };
52+
Tracing.AddAnnotation("getsomething", "value");
2453
}
2554
}

libraries/tests/AWS.Lambda.Powertools.Tracing.Tests/Handlers/HandlerTests.cs

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
using System;
2+
using System.Linq;
23
using System.Threading.Tasks;
4+
using Amazon.Lambda.TestUtilities;
5+
using Amazon.XRay.Recorder.Core;
6+
using AWS.Lambda.Powertools.Tracing.Internal;
7+
using AWS.Lambda.Powertools.Tracing.Tests.Handlers;
38
using Xunit;
49

5-
namespace AWS.Lambda.Powertools.Tracing.Tests.Handlers;
10+
namespace AWS.Lambda.Powertools.Tracing.Tests;
611

712
[Collection("Sequential")]
8-
public sealed class HandlerTests
13+
public sealed class HandlerTests : IDisposable
914
{
15+
public HandlerTests()
16+
{
17+
Environment.SetEnvironmentVariable("LAMBDA_TASK_ROOT", "AWS");
18+
}
19+
1020
[Fact]
1121
public async Task Stack_Trace_Included_When_Decorator_Present()
1222
{
@@ -19,7 +29,6 @@ public async Task Stack_Trace_Included_When_Decorator_Present()
1929
// Assert
2030
var tracedException = await Assert.ThrowsAsync<NullReferenceException>(Handle);
2131
Assert.StartsWith("at AWS.Lambda.Powertools.Tracing.Tests.Handlers.ExceptionFunctionHandler.ThisThrows()", tracedException.StackTrace?.TrimStart());
22-
2332
}
2433

2534
[Fact]
@@ -33,4 +42,70 @@ public async Task When_Decorator_Present_In_Generic_Method_Should_Not_Throw_When
3342

3443
// Assert
3544
}
45+
46+
[Fact]
47+
public async Task Full_Example()
48+
{
49+
// Arrange
50+
Environment.SetEnvironmentVariable("LAMBDA_TASK_ROOT", "AWS");
51+
Environment.SetEnvironmentVariable("POWERTOOLS_SERVICE_NAME", "POWERTOOLS");
52+
53+
var handler = new FullExampleHandler();
54+
var context = new TestLambdaContext
55+
{
56+
FunctionName = "FullExampleLambda",
57+
FunctionVersion = "1",
58+
MemoryLimitInMB = 215,
59+
AwsRequestId = Guid.NewGuid().ToString("D")
60+
};
61+
62+
// Act
63+
await handler.Handle("Hello World", context);
64+
var handleSegment = AWSXRayRecorder.Instance.TraceContext.GetEntity();
65+
66+
// Assert
67+
Assert.True(handleSegment.IsAnnotationsAdded);
68+
Assert.True(handleSegment.IsSubsegmentsAdded);
69+
70+
Assert.Equal("POWERTOOLS", handleSegment.Annotations["Service"]);
71+
Assert.Equal(true, handleSegment.Annotations["ColdStart"]);
72+
Assert.Equal("value", handleSegment.Annotations["annotation"]);
73+
Assert.Equal("## Handle", handleSegment.Name);
74+
75+
var firstCallSubsegment = handleSegment.Subsegments[0];
76+
77+
Assert.Equal("First Call", firstCallSubsegment.Name);
78+
Assert.False(firstCallSubsegment.IsInProgress);
79+
Assert.False(firstCallSubsegment.IsAnnotationsAdded);
80+
// Assert.True(firstCallSubsegment.IsMetadataAdded);
81+
Assert.True(firstCallSubsegment.IsSubsegmentsAdded);
82+
83+
var businessLogicSubsegment = firstCallSubsegment.Subsegments[0];
84+
85+
Assert.Equal("## BusinessLogic2", businessLogicSubsegment.Name);
86+
Assert.True(businessLogicSubsegment.IsMetadataAdded);
87+
Assert.False(businessLogicSubsegment.IsInProgress);
88+
Assert.Single(businessLogicSubsegment.Metadata);
89+
var metadata = businessLogicSubsegment.Metadata["POWERTOOLS"];
90+
Assert.Contains("metadata", metadata.Keys.Cast<string>());
91+
Assert.Contains("value", metadata.Values.Cast<string>());
92+
Assert.True(businessLogicSubsegment.IsSubsegmentsAdded);
93+
94+
var getSomethingSubsegment = businessLogicSubsegment.Subsegments[0];
95+
96+
Assert.Equal("## GetSomething", getSomethingSubsegment.Name);
97+
Assert.Equal("localNamespace", getSomethingSubsegment.Namespace);
98+
Assert.True(getSomethingSubsegment.IsAnnotationsAdded);
99+
Assert.False(getSomethingSubsegment.IsSubsegmentsAdded);
100+
Assert.False(getSomethingSubsegment.IsInProgress);
101+
Assert.Equal("value", getSomethingSubsegment.Annotations["getsomething"]);
102+
}
103+
104+
public void Dispose()
105+
{
106+
Environment.SetEnvironmentVariable("LAMBDA_TASK_ROOT", "");
107+
Environment.SetEnvironmentVariable("POWERTOOLS_SERVICE_NAME", "");
108+
Environment.SetEnvironmentVariable("POWERTOOLS_TRACE_DISABLED", "");
109+
TracingAspect.ResetForTest();
110+
}
36111
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
using System;
1717

18-
namespace AWS.Lambda.Powertools.Tracing.Tests.Handlers;
18+
namespace AWS.Lambda.Powertools.Tracing.Tests;
1919

20-
public class Handlers
20+
public class HandlerFunctions
2121
{
2222
[Tracing()]
2323
public string[] Handle()

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ namespace AWS.Lambda.Powertools.Tracing.Tests
2727
[Collection("Sequential")]
2828
public class TracingAttributeColdStartTest : IDisposable
2929
{
30-
private readonly Handlers.Handlers _handler;
30+
private readonly HandlerFunctions _handler;
3131

3232
public TracingAttributeColdStartTest()
3333
{
34-
_handler = new Handlers.Handlers();
34+
_handler = new HandlerFunctions();
3535
}
3636

3737
[Fact]
@@ -122,11 +122,11 @@ public void Dispose()
122122
[Collection("Sequential")]
123123
public class TracingAttributeDisableTest : IDisposable
124124
{
125-
private readonly Handlers.Handlers _handler;
125+
private readonly HandlerFunctions _handler;
126126

127127
public TracingAttributeDisableTest()
128128
{
129-
_handler = new Handlers.Handlers();
129+
_handler = new HandlerFunctions();
130130
}
131131

132132
[Fact]
@@ -178,11 +178,11 @@ private static void ClearEnvironment()
178178
[Collection("Sequential")]
179179
public class TracingAttributeLambdaEnvironmentTest
180180
{
181-
private readonly Handlers.Handlers _handler;
181+
private readonly HandlerFunctions _handler;
182182

183183
public TracingAttributeLambdaEnvironmentTest()
184184
{
185-
_handler = new Handlers.Handlers();
185+
_handler = new HandlerFunctions();
186186
}
187187

188188
[Fact]
@@ -212,11 +212,11 @@ public void Tracing_WhenOutsideOfLambdaEnv_DisablesTracing()
212212
[Collection("Sequential")]
213213
public class TracingAttributeTest : IDisposable
214214
{
215-
private readonly Handlers.Handlers _handler;
215+
private readonly HandlerFunctions _handler;
216216

217217
public TracingAttributeTest()
218218
{
219-
_handler = new Handlers.Handlers();
219+
_handler = new HandlerFunctions();
220220
}
221221

222222
#region OnEntry Tests

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace AWS.Lambda.Powertools.Tracing.Tests;
1010

11+
// This has to be the last tests to run otherwise it will keep state and fail other random tests
1112
[Collection("Sequential")]
1213
public class XRayRecorderTests
1314
{

0 commit comments

Comments
 (0)