Skip to content

Commit d666ab2

Browse files
committed
add test with lambdacontext, refactor
1 parent 564044b commit d666ab2

File tree

5 files changed

+88
-26
lines changed

5 files changed

+88
-26
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<ItemGroup>
1111
<!-- Package versions are Centrally managed in Directory.Packages.props file -->
1212
<!-- More info https://learn.microsoft.com/en-us/nuget/consume-packages/central-package-management -->
13+
<PackageReference Include="Amazon.Lambda.Core" />
14+
<PackageReference Include="Amazon.Lambda.TestUtilities" />
1315
<PackageReference Include="Microsoft.NET.Test.Sdk" />
1416
<PackageReference Include="xunit" />
1517
<PackageReference Include="xunit.runner.visualstudio">

libraries/tests/AWS.Lambda.Powertools.Metrics.Tests/EMFValidationTests.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -341,18 +341,7 @@ private List<int> AllIndexesOf(string str, string value)
341341
indexes.Add(index);
342342
}
343343
}
344-
345-
private class CustomConsoleWriter : StringWriter
346-
{
347-
public readonly List<string> OutputValues = new();
348-
349-
public override void WriteLine(string value)
350-
{
351-
OutputValues.Add(value);
352-
base.WriteLine(value);
353-
}
354-
}
355-
344+
356345
#endregion
357346

358347

libraries/tests/AWS.Lambda.Powertools.Metrics.Tests/Handlers/FunctionHandler.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
using System.Globalization;
2020
using System.Linq;
2121
using System.Threading.Tasks;
22+
using Amazon.Lambda.Core;
23+
using Amazon.Lambda.TestUtilities;
2224

2325
namespace AWS.Lambda.Powertools.Metrics.Tests.Handlers;
2426

@@ -159,4 +161,16 @@ await Parallel.ForEachAsync(Enumerable.Range(0, Environment.ProcessorCount * 2),
159161

160162
return input.ToUpper(CultureInfo.InvariantCulture);
161163
}
164+
165+
[Metrics(Namespace = "ns", Service = "svc", CaptureColdStart = true)]
166+
public void HandleWithLambdaContext(ILambdaContext context)
167+
{
168+
169+
}
170+
171+
[Metrics(Namespace = "ns", Service = "svc")]
172+
public void HandleWithLambdaContextAndMetrics(TestLambdaContext context)
173+
{
174+
Metrics.AddMetric("MyMetric", 1);
175+
}
162176
}

libraries/tests/AWS.Lambda.Powertools.Metrics.Tests/Handlers/FunctionHandlerTests.cs

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,33 @@
1515

1616
using System;
1717
using System.Threading.Tasks;
18+
using Amazon.Lambda.TestUtilities;
19+
using AWS.Lambda.Powertools.Common;
1820
using Xunit;
1921

2022
namespace AWS.Lambda.Powertools.Metrics.Tests.Handlers;
2123

2224
[Collection("Sequential")]
2325
public class FunctionHandlerTests : IDisposable
2426
{
27+
private readonly FunctionHandler _handler;
28+
private readonly CustomConsoleWriter _consoleOut;
29+
30+
public FunctionHandlerTests()
31+
{
32+
_handler = new FunctionHandler();
33+
_consoleOut = new CustomConsoleWriter();
34+
SystemWrapper.Instance.SetOut(_consoleOut);
35+
}
36+
2537
[Fact]
2638
public async Task When_Metrics_Add_Metadata_Same_Key_Should_Ignore_Metadata()
2739
{
2840
// Arrange
29-
Metrics.ResetForTest();
30-
var handler = new FunctionHandler();
41+
3142

3243
// Act
33-
var exception = await Record.ExceptionAsync( () => handler.HandleSameKey("whatever"));
44+
var exception = await Record.ExceptionAsync( () => _handler.HandleSameKey("whatever"));
3445

3546
// Assert
3647
Assert.Null(exception);
@@ -39,32 +50,48 @@ public async Task When_Metrics_Add_Metadata_Same_Key_Should_Ignore_Metadata()
3950
[Fact]
4051
public async Task When_Metrics_Add_Metadata_Second_Invocation_Should_Not_Throw_Exception()
4152
{
42-
// Arrange
43-
Metrics.ResetForTest();
44-
var handler = new FunctionHandler();
45-
4653
// Act
47-
var exception = await Record.ExceptionAsync( () => handler.HandleTestSecondCall("whatever"));
54+
var exception = await Record.ExceptionAsync( () => _handler.HandleTestSecondCall("whatever"));
4855
Assert.Null(exception);
4956

50-
exception = await Record.ExceptionAsync( () => handler.HandleTestSecondCall("whatever"));
57+
exception = await Record.ExceptionAsync( () => _handler.HandleTestSecondCall("whatever"));
5158
Assert.Null(exception);
5259
}
5360

5461
[Fact]
5562
public async Task When_Metrics_Add_Metadata_FromMultipleThread_Should_Not_Throw_Exception()
5663
{
57-
// Arrange
58-
Metrics.ResetForTest();
59-
var handler = new FunctionHandler();
60-
6164
// Act
62-
var exception = await Record.ExceptionAsync(() => handler.HandleMultipleThreads("whatever"));
65+
var exception = await Record.ExceptionAsync(() => _handler.HandleMultipleThreads("whatever"));
6366
Assert.Null(exception);
6467
}
68+
69+
[Fact]
70+
public void When_LambdaContext_Should_Add_FunctioName_Dimension_CaptureColdStart()
71+
{
72+
// Arrange
73+
var context = new TestLambdaContext
74+
{
75+
FunctionName = "My Function with context"
76+
};
77+
78+
// Act
79+
_handler.HandleWithLambdaContext(context);
80+
var metricsOutput = _consoleOut.ToString();
81+
82+
// Assert
83+
Assert.Contains(
84+
"\"FunctionName\":\"My Function with context\"",
85+
metricsOutput);
86+
87+
Assert.Contains(
88+
"\"Metrics\":[{\"Name\":\"ColdStart\",\"Unit\":\"Count\"}],\"Dimensions\":[[\"FunctionName\"],[\"Service\"]]}]}",
89+
metricsOutput);
90+
}
6591

6692
public void Dispose()
6793
{
94+
Metrics.ResetForTest();
6895
MetricsAspect.ResetForTest();
6996
}
7097
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
using System.Collections.Generic;
17+
using System.IO;
18+
19+
namespace AWS.Lambda.Powertools.Metrics.Tests;
20+
21+
public class CustomConsoleWriter : StringWriter
22+
{
23+
public readonly List<string> OutputValues = new();
24+
25+
public override void WriteLine(string value)
26+
{
27+
OutputValues.Add(value);
28+
base.WriteLine(value);
29+
}
30+
}

0 commit comments

Comments
 (0)