Skip to content

Commit 6e4d92c

Browse files
committed
Fix unit test
1 parent ea445ab commit 6e4d92c

4 files changed

Lines changed: 152 additions & 5 deletions

File tree

Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/UserCodeLoader.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ internal class UserCodeLoader
3535
{
3636
private const string UserInvokeException = "An exception occurred while invoking customer handler.";
3737
private const string LambdaLoggingActionFieldName = "_loggingAction";
38+
private const string LambdaLoggingWithLevelActionFieldName = "_loggingWithLevelAction";
39+
private const string LambdaLoggingWithLevelAndExceptionActionFieldName = "_loggingWithLevelAndExceptionAction";
3840

3941
internal const string LambdaCoreAssemblyName = "Amazon.Lambda.Core";
4042

@@ -186,6 +188,82 @@ internal static void SetCustomerLoggerLogAction(Assembly coreAssembly, Action<st
186188
}
187189
}
188190

191+
internal static void SetCustomerLoggerLogAction(Assembly coreAssembly, Action<string, string, object[]> loggingWithLevelAction, InternalLogger internalLogger)
192+
{
193+
if (coreAssembly == null)
194+
{
195+
throw new ArgumentNullException(nameof(coreAssembly));
196+
}
197+
198+
if (loggingWithLevelAction == null)
199+
{
200+
throw new ArgumentNullException(nameof(loggingWithLevelAction));
201+
}
202+
203+
internalLogger.LogDebug($"UCL : Retrieving type '{Types.LambdaLoggerTypeName}'");
204+
var lambdaILoggerType = coreAssembly.GetType(Types.LambdaLoggerTypeName);
205+
if (lambdaILoggerType == null)
206+
{
207+
throw LambdaExceptions.ValidationException(Errors.UserCodeLoader.Internal.UnableToLocateType, Types.LambdaLoggerTypeName);
208+
}
209+
210+
internalLogger.LogDebug($"UCL : Retrieving field '{LambdaLoggingWithLevelActionFieldName}'");
211+
var loggingActionField = lambdaILoggerType.GetTypeInfo().GetField(LambdaLoggingWithLevelActionFieldName, BindingFlags.NonPublic | BindingFlags.Static);
212+
if (loggingActionField == null)
213+
{
214+
throw LambdaExceptions.ValidationException(Errors.UserCodeLoader.Internal.UnableToRetrieveField, LambdaLoggingWithLevelActionFieldName, Types.LambdaLoggerTypeName);
215+
}
216+
217+
internalLogger.LogDebug($"UCL : Setting field '{LambdaLoggingWithLevelActionFieldName}'");
218+
try
219+
{
220+
loggingActionField.SetValue(null, loggingWithLevelAction);
221+
}
222+
catch (Exception e)
223+
{
224+
throw LambdaExceptions.ValidationException(e, Errors.UserCodeLoader.Internal.UnableToSetField,
225+
Types.LambdaLoggerTypeName, LambdaLoggingWithLevelActionFieldName);
226+
}
227+
}
228+
229+
internal static void SetCustomerLoggerLogAction(Assembly coreAssembly, Action<string, Exception, string, object[]> loggingWithAndExceptionLevelAction, InternalLogger internalLogger)
230+
{
231+
if (coreAssembly == null)
232+
{
233+
throw new ArgumentNullException(nameof(coreAssembly));
234+
}
235+
236+
if (loggingWithAndExceptionLevelAction == null)
237+
{
238+
throw new ArgumentNullException(nameof(loggingWithAndExceptionLevelAction));
239+
}
240+
241+
internalLogger.LogDebug($"UCL : Retrieving type '{Types.LambdaLoggerTypeName}'");
242+
var lambdaILoggerType = coreAssembly.GetType(Types.LambdaLoggerTypeName);
243+
if (lambdaILoggerType == null)
244+
{
245+
throw LambdaExceptions.ValidationException(Errors.UserCodeLoader.Internal.UnableToLocateType, Types.LambdaLoggerTypeName);
246+
}
247+
248+
internalLogger.LogDebug($"UCL : Retrieving field '{LambdaLoggingWithLevelAndExceptionActionFieldName}'");
249+
var loggingActionField = lambdaILoggerType.GetTypeInfo().GetField(LambdaLoggingWithLevelAndExceptionActionFieldName, BindingFlags.NonPublic | BindingFlags.Static);
250+
if (loggingActionField == null)
251+
{
252+
throw LambdaExceptions.ValidationException(Errors.UserCodeLoader.Internal.UnableToRetrieveField, LambdaLoggingWithLevelAndExceptionActionFieldName, Types.LambdaLoggerTypeName);
253+
}
254+
255+
internalLogger.LogDebug($"UCL : Setting field '{LambdaLoggingWithLevelAndExceptionActionFieldName}'");
256+
try
257+
{
258+
loggingActionField.SetValue(null, loggingWithAndExceptionLevelAction);
259+
}
260+
catch (Exception e)
261+
{
262+
throw LambdaExceptions.ValidationException(e, Errors.UserCodeLoader.Internal.UnableToSetField,
263+
Types.LambdaLoggerTypeName, LambdaLoggingWithLevelAndExceptionActionFieldName);
264+
}
265+
}
266+
189267
/// <summary>
190268
/// Constructs customer-specified serializer, specified either on the method,
191269
/// the assembly, or not specified at all.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Text;
8+
using Xunit;
9+
using System.Linq;
10+
using Amazon.Lambda.TestUtilities;
11+
using Amazon.Lambda.Core;
12+
13+
namespace Amazon.Lambda.Tests
14+
{
15+
public class TestUtilitiesLoggingTest
16+
{
17+
[Fact]
18+
public void LogWithLevelAndMessage()
19+
{
20+
ILambdaLogger logger = new TestLambdaLogger();
21+
logger.Log(LogLevel.Warning, "This is a warning");
22+
23+
Assert.Contains("Warning: This is a warning", ((TestLambdaLogger)logger).Buffer.ToString());
24+
}
25+
26+
[Fact]
27+
public void LogWithLevelAndMessageAndParameters()
28+
{
29+
ILambdaLogger logger = new TestLambdaLogger();
30+
logger.Log(LogLevel.Warning, "This is {name}", "garp");
31+
32+
Assert.Contains("Warning: This is {name}", ((TestLambdaLogger)logger).Buffer.ToString());
33+
Assert.Contains("\tgarp", ((TestLambdaLogger)logger).Buffer.ToString());
34+
}
35+
36+
[Fact]
37+
public void LogWithLevelAndMessageAndParametersAndExecption()
38+
{
39+
ILambdaLogger logger = new TestLambdaLogger();
40+
41+
var exception = new ArgumentException("Bad Name");
42+
logger.Log(LogLevel.Warning, exception, "This is {name}", "garp");
43+
44+
45+
Assert.Contains("Warning: This is {name}", ((TestLambdaLogger)logger).Buffer.ToString());
46+
Assert.Contains("\tgarp", ((TestLambdaLogger)logger).Buffer.ToString());
47+
Assert.Contains("ArgumentException", ((TestLambdaLogger)logger).Buffer.ToString());
48+
}
49+
}
50+
}

Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/Amazon.Lambda.RuntimeSupport.UnitTests/HandlerTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
@@ -393,9 +393,10 @@ private async Task<ExecutionInfo> ExecHandlerAsync(string handler, string dataIn
393393
Client = testRuntimeApiClient
394394
};
395395

396-
var loggerAction = actionWriter.ToLoggingAction();
397396
var assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(UserCodeLoader.LambdaCoreAssemblyName));
398-
UserCodeLoader.SetCustomerLoggerLogAction(assembly, loggerAction, _internalLogger);
397+
UserCodeLoader.SetCustomerLoggerLogAction(assembly, actionWriter.ToLoggingAction(), _internalLogger);
398+
UserCodeLoader.SetCustomerLoggerLogAction(assembly, actionWriter.ToLoggingWithLevelAction(), _internalLogger);
399+
UserCodeLoader.SetCustomerLoggerLogAction(assembly, actionWriter.ToLoggingWithLevelAndExceptionAction(), _internalLogger);
399400

400401
if (assertLoggedByInitialize != null)
401402
{

Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/Amazon.Lambda.RuntimeSupport.UnitTests/TestHelpers/StringWriterExtensions.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
@@ -67,9 +67,27 @@ public static Action<string> ToLoggingAction(this TextWriter writer)
6767
return writer.WriteLine;
6868
}
6969

70+
public static Action<string, string, object[]> ToLoggingWithLevelAction(this TextWriter writer)
71+
{
72+
return (l, m, p) =>
73+
{
74+
var formattedMessage = $"{l}: {m}";
75+
writer.WriteLine(formattedMessage);
76+
};
77+
}
78+
79+
public static Action<string, Exception, string, object[]> ToLoggingWithLevelAndExceptionAction(this TextWriter writer)
80+
{
81+
return (l, e, m, p) =>
82+
{
83+
var formattedMessage = $"{l}: {m}\n{e}";
84+
writer.WriteLine(formattedMessage);
85+
};
86+
}
87+
7088
public static Action<string> ToLoggingAction(this ITestOutputHelper writer)
7189
{
7290
return writer.WriteLine;
7391
}
7492
}
75-
}
93+
}

0 commit comments

Comments
 (0)