-
Notifications
You must be signed in to change notification settings - Fork 499
Expand file tree
/
Copy pathCustomRuntimeFunction.cs
More file actions
476 lines (408 loc) · 20.1 KB
/
CustomRuntimeFunction.cs
File metadata and controls
476 lines (408 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
using Amazon.Lambda.Core;
using Amazon.Lambda.RuntimeSupport;
using Amazon.Lambda.Serialization.SystemTextJson;
using System;
using System.IO;
using System.Net.Http;
using System.Net.Sockets;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CustomRuntimeFunctionTest
{
class CustomRuntimeFunction
{
private const string FailureResult = "FAILURE";
private const string SuccessResult = "SUCCESS";
private const string TestUrl = "https://www.amazon.com";
private static readonly Lazy<string> SixMBString = new Lazy<string>(() => { return new string('X', 1024 * 1024 * 6); });
private static readonly Lazy<string> SevenMBString = new Lazy<string>(() => { return new string('X', 1024 * 1024 * 7); });
private static MemoryStream ResponseStream = new MemoryStream();
private static DefaultLambdaJsonSerializer JsonSerializer = new DefaultLambdaJsonSerializer();
private static LambdaEnvironment LambdaEnvironment = new LambdaEnvironment();
private static async Task Main(string[] args)
{
var handler = Environment.GetEnvironmentVariable("TEST_HANDLER");
LambdaBootstrap bootstrap = null;
HandlerWrapper handlerWrapper = null;
try
{
switch (handler)
{
case nameof(GetTotalAvailableMemoryBytes):
bootstrap = new LambdaBootstrap(GetTotalAvailableMemoryBytes);
break;
case nameof(ExceptionNonAsciiCharacterUnwrappedAsync):
bootstrap = new LambdaBootstrap(ExceptionNonAsciiCharacterUnwrappedAsync);
break;
case nameof(UnintendedDisposeTest):
bootstrap = new LambdaBootstrap(UnintendedDisposeTest);
break;
case nameof(LoggingStressTest):
bootstrap = new LambdaBootstrap(LoggingStressTest);
break;
case nameof(LoggingTest):
bootstrap = new LambdaBootstrap(LoggingTest);
break;
case nameof(ToUpperAsync):
bootstrap = new LambdaBootstrap(ToUpperAsync);
break;
case nameof(PingAsync):
bootstrap = new LambdaBootstrap(PingAsync);
break;
case nameof(HttpsWorksAsync):
bootstrap = new LambdaBootstrap(HttpsWorksAsync);
break;
case nameof(CertificateCallbackWorksAsync):
bootstrap = new LambdaBootstrap(CertificateCallbackWorksAsync);
break;
case nameof(NetworkingProtocolsAsync):
bootstrap = new LambdaBootstrap(NetworkingProtocolsAsync);
break;
case nameof(HandlerEnvVarAsync):
bootstrap = new LambdaBootstrap(HandlerEnvVarAsync);
break;
case nameof(AggregateExceptionUnwrappedAsync):
bootstrap = new LambdaBootstrap(AggregateExceptionUnwrappedAsync);
break;
case nameof(AggregateExceptionUnwrapped):
handlerWrapper = HandlerWrapper.GetHandlerWrapper((Action)AggregateExceptionUnwrapped);
bootstrap = new LambdaBootstrap(handlerWrapper);
break;
case nameof(AggregateExceptionNotUnwrappedAsync):
bootstrap = new LambdaBootstrap(AggregateExceptionNotUnwrappedAsync);
break;
case nameof(AggregateExceptionNotUnwrapped):
handlerWrapper = HandlerWrapper.GetHandlerWrapper((Action)AggregateExceptionNotUnwrapped);
bootstrap = new LambdaBootstrap(handlerWrapper);
break;
case nameof(TooLargeResponseBodyAsync):
bootstrap = new LambdaBootstrap(TooLargeResponseBodyAsync);
break;
case nameof(LambdaEnvironmentAsync):
bootstrap = new LambdaBootstrap(LambdaEnvironmentAsync);
break;
case nameof(LambdaContextBasicAsync):
bootstrap = new LambdaBootstrap(LambdaContextBasicAsync);
break;
case nameof(GetPidDllImportAsync):
bootstrap = new LambdaBootstrap(GetPidDllImportAsync);
break;
case nameof(GetTimezoneNameAsync):
bootstrap = new LambdaBootstrap(GetTimezoneNameAsync);
break;
default:
throw new Exception($"Handler {handler} is not supported.");
}
await bootstrap.RunAsync();
}
finally
{
handlerWrapper?.Dispose();
bootstrap?.Dispose();
}
}
private static Task<InvocationResponse> LoggingStressTest(InvocationRequest invocation)
{
var source = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var token = source.Token;
Task UseWriteAsync()
{
return Task.Run(() =>
{
int i = 0;
while (!token.IsCancellationRequested)
{
Thread.Sleep(0);
Console.Write($"|Write+{i++}|");
}
});
}
Task UseWriteLineAsync()
{
return Task.Run(() =>
{
int i = 0;
while (!token.IsCancellationRequested)
{
Thread.Sleep(0);
Console.WriteLine($"|WriteLine+{i++}|");
}
});
}
Task UseLoggerAsync()
{
return Task.Run(() =>
{
int i = 0;
while (!token.IsCancellationRequested)
{
Thread.Sleep(0);
invocation.LambdaContext.Logger.LogInformation($"|FormattedWriteLine+{i++}|");
}
});
}
var task1 = UseWriteAsync();
var task2 = UseWriteLineAsync();
var task3 = UseLoggerAsync();
Task.WaitAll(task1, task2, task3);
return Task.FromResult(GetInvocationResponse(nameof(LoggingStressTest), "success"));
}
private static Task<InvocationResponse> LoggingTest(InvocationRequest invocation)
{
invocation.LambdaContext.Logger.LogTrace("A trace log");
invocation.LambdaContext.Logger.LogDebug("A debug log");
invocation.LambdaContext.Logger.LogInformation("A information log");
invocation.LambdaContext.Logger.LogWarning("A warning log");
invocation.LambdaContext.Logger.LogError("A error log");
invocation.LambdaContext.Logger.LogCritical("A critical log");
Console.WriteLine("A stdout info message");
Console.Error.WriteLine("A stderror error message");
Amazon.Lambda.Core.LambdaLogger.Log("A fake message level");
return Task.FromResult(GetInvocationResponse(nameof(LoggingTest), true));
}
private static Task<InvocationResponse> UnintendedDisposeTest(InvocationRequest invocation)
{
int errorCode = new NUnitLite.AutoRun().Execute(new string[] { "--noresult" });
Console.WriteLine("This is standard output stream.");
return Task.FromResult(GetInvocationResponse(nameof(UnintendedDisposeTest), true));
}
public class WrapTextWriter : TextWriter
{
TextWriter _textWriter;
public WrapTextWriter(TextWriter textWriter)
{
_textWriter = textWriter;
}
public override Encoding Encoding => UTF8Encoding.UTF8;
protected override void Dispose(bool disposing)
{
if(disposing)
{
_textWriter.Dispose();
}
}
public override async ValueTask DisposeAsync()
{
await _textWriter.DisposeAsync();
}
public override void Close()
{
_textWriter.Close();
}
}
private static Task<InvocationResponse> ToUpperAsync(InvocationRequest invocation)
{
var input = JsonSerializer.Deserialize<string>(invocation.InputStream);
return Task.FromResult(GetInvocationResponse(nameof(ToUpperAsync), input.ToUpper()));
}
private static Task<InvocationResponse> PingAsync(InvocationRequest invocation)
{
var input = JsonSerializer.Deserialize<string>(invocation.InputStream);
if (input == "ping")
{
return Task.FromResult(GetInvocationResponse(nameof(PingAsync), "pong"));
}
else
{
throw new Exception("Expected input: ping");
}
}
private static async Task<InvocationResponse> HttpsWorksAsync(InvocationRequest invocation)
{
var isSuccess = false;
using (var httpClient = new HttpClient())
{
var response = await httpClient.GetAsync(TestUrl);
if (response.IsSuccessStatusCode)
{
isSuccess = true;
}
Console.WriteLine($"Response from HTTP get: {response}");
}
return GetInvocationResponse(nameof(HttpsWorksAsync), isSuccess);
}
private static async Task<InvocationResponse> CertificateCallbackWorksAsync(InvocationRequest invocation)
{
var isSuccess = false;
using (var httpClientHandler = new HttpClientHandler())
{
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
using (var client = new HttpClient(httpClientHandler))
{
var response = await client.GetAsync(TestUrl);
if (response.IsSuccessStatusCode)
{
isSuccess = true;
}
Console.WriteLine($"Response from HTTP get: {response}");
}
}
return GetInvocationResponse(nameof(CertificateCallbackWorksAsync), isSuccess);
}
private static Task<InvocationResponse> NetworkingProtocolsAsync(InvocationRequest invocation)
{
var type = typeof(Socket).GetTypeInfo().Assembly.GetType("System.Net.SocketProtocolSupportPal");
var method = type.GetMethod("IsSupported", BindingFlags.NonPublic | BindingFlags.Static);
var ipv4Supported = method.Invoke(null, new object[] { AddressFamily.InterNetwork });
var ipv6Supported = method.Invoke(null, new object[] { AddressFamily.InterNetworkV6 });
Exception ipv4SocketCreateException = null;
try
{
using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { }
}
catch (Exception e)
{
ipv4SocketCreateException = e;
}
Exception ipv6SocketCreateException = null;
try
{
using (Socket s = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp)) { }
}
catch (Exception e)
{
ipv6SocketCreateException = e;
}
string returnValue = "";
if (!(bool)ipv4Supported)
{
returnValue += "For System.Net.SocketProtocolSupportPal.IsProtocolSupported(AddressFamily.InterNetwork) Expected true, Actual false" + Environment.NewLine;
}
if ((bool)ipv6Supported)
{
returnValue += "For System.Net.SocketProtocolSupportPal.IsProtocolSupported(AddressFamily.InterNetworkV6) Expected false, Actual true" + Environment.NewLine;
}
if (ipv4SocketCreateException != null)
{
returnValue += "Error creating IPV4 Socket: " + ipv4SocketCreateException + Environment.NewLine;
}
if (ipv6SocketCreateException == null)
{
returnValue += "When creating IPV6 Socket expected exception, got none." + Environment.NewLine;
}
if (ipv6SocketCreateException != null && ipv6SocketCreateException.Message != "Address family not supported by protocol")
{
returnValue += "When creating IPV6 Socket expected exception 'Address family not supported by protocol', actual '" + ipv6SocketCreateException.Message + "'" + Environment.NewLine;
}
if (String.IsNullOrEmpty(returnValue))
{
returnValue = "SUCCESS";
}
return Task.FromResult(GetInvocationResponse(nameof(NetworkingProtocolsAsync), returnValue));
}
private static Task<InvocationResponse> HandlerEnvVarAsync(InvocationRequest invocation)
{
return Task.FromResult(GetInvocationResponse(nameof(HandlerEnvVarAsync), LambdaEnvironment.Handler));
}
private static async Task<InvocationResponse> AggregateExceptionUnwrappedAsync(InvocationRequest invocation)
{
// do something async so this function is compiled as async
var dummy = await Task.FromResult("xyz");
throw new Exception("Exception thrown from an async handler.");
}
private static async Task<InvocationResponse> ExceptionNonAsciiCharacterUnwrappedAsync(InvocationRequest invocation)
{
// do something async so this function is compiled as async
var dummy = await Task.FromResult("xyz");
throw new Exception("Unhandled exception with non ASCII character: ♂");
}
private static void AggregateExceptionUnwrapped()
{
throw new Exception("Exception thrown from a synchronous handler.");
}
private static async Task<InvocationResponse> AggregateExceptionNotUnwrappedAsync(InvocationRequest invocation)
{
// do something async so this function is compiled as async
var dummy = await Task.FromResult("xyz");
throw new AggregateException("AggregateException thrown from an async handler.");
}
private static void AggregateExceptionNotUnwrapped()
{
throw new AggregateException("AggregateException thrown from a synchronous handler.");
}
private static Task<InvocationResponse> TooLargeResponseBodyAsync(InvocationRequest invocation)
{
return Task.FromResult(GetInvocationResponse(nameof(TooLargeResponseBodyAsync), SevenMBString.Value));
}
private static Task<InvocationResponse> LambdaEnvironmentAsync(InvocationRequest invocation)
{
AssertNotNull(LambdaEnvironment.FunctionMemorySize, nameof(LambdaEnvironment.FunctionMemorySize));
AssertNotNull(LambdaEnvironment.FunctionName, nameof(LambdaEnvironment.FunctionName));
AssertNotNull(LambdaEnvironment.FunctionVersion, nameof(LambdaEnvironment.FunctionVersion));
AssertNotNull(LambdaEnvironment.Handler, nameof(LambdaEnvironment.Handler));
AssertNotNull(LambdaEnvironment.LogGroupName, nameof(LambdaEnvironment.LogGroupName));
AssertNotNull(LambdaEnvironment.LogStreamName, nameof(LambdaEnvironment.LogStreamName));
AssertNotNull(LambdaEnvironment.RuntimeServerHostAndPort, nameof(LambdaEnvironment.RuntimeServerHostAndPort));
AssertNotNull(LambdaEnvironment.XAmznTraceId, nameof(LambdaEnvironment.XAmznTraceId));
return Task.FromResult(GetInvocationResponse(nameof(LambdaEnvironmentAsync), true));
}
private static Task<InvocationResponse> LambdaContextBasicAsync(InvocationRequest invocation)
{
AssertNotNull(invocation.LambdaContext.AwsRequestId, nameof(invocation.LambdaContext.AwsRequestId));
AssertNotNull(invocation.LambdaContext.ClientContext, nameof(invocation.LambdaContext.ClientContext));
AssertNotNull(invocation.LambdaContext.FunctionName, nameof(invocation.LambdaContext.FunctionName));
AssertNotNull(invocation.LambdaContext.FunctionVersion, nameof(invocation.LambdaContext.FunctionVersion));
AssertNotNull(invocation.LambdaContext.Identity, nameof(invocation.LambdaContext.Identity));
AssertNotNull(invocation.LambdaContext.InvokedFunctionArn, nameof(invocation.LambdaContext.InvokedFunctionArn));
AssertNotNull(invocation.LambdaContext.Logger, nameof(invocation.LambdaContext.Logger));
AssertNotNull(invocation.LambdaContext.LogGroupName, nameof(invocation.LambdaContext.LogGroupName));
AssertNotNull(invocation.LambdaContext.LogStreamName, nameof(invocation.LambdaContext.LogStreamName));
AssertTrue(invocation.LambdaContext.MemoryLimitInMB >= 128,
$"{nameof(invocation.LambdaContext.MemoryLimitInMB)}={invocation.LambdaContext.MemoryLimitInMB} is not >= 128");
AssertTrue(invocation.LambdaContext.RemainingTime > TimeSpan.Zero,
$"{nameof(invocation.LambdaContext.RemainingTime)}={invocation.LambdaContext.RemainingTime} is not >= 0");
return Task.FromResult(GetInvocationResponse(nameof(LambdaContextBasicAsync), true));
}
#region GetPidDllImportAsync
[DllImport("libc", EntryPoint = "getpid", CallingConvention = CallingConvention.Cdecl)]
private static extern int getpid();
private static Task<InvocationResponse> GetPidDllImportAsync(InvocationRequest invocation)
{
var isSuccess = getpid() > 0;
return Task.FromResult(GetInvocationResponse(nameof(GetPidDllImportAsync), isSuccess));
}
#endregion
private static Task<InvocationResponse> GetTimezoneNameAsync(InvocationRequest invocation)
{
return Task.FromResult(GetInvocationResponse(nameof(GetTimezoneNameAsync), TimeZoneInfo.Local.Id));
}
private static async Task<InvocationResponse> GetTotalAvailableMemoryBytes(InvocationRequest invocation)
{
return GetInvocationResponse(nameof(GetTotalAvailableMemoryBytes), GC.GetGCMemoryInfo().TotalAvailableMemoryBytes.ToString());
}
#region Helpers
private static void AssertNotNull(object value, string valueName)
{
if (value == null)
{
throw new Exception($"{valueName} cannot be null.");
}
}
private static void AssertTrue(bool value, string errorMessage)
{
if (!value)
{
throw new Exception(errorMessage);
}
}
private static InvocationResponse GetInvocationResponse(string testName, bool isSuccess)
{
return GetInvocationResponse($"{testName}-{(isSuccess ? SuccessResult : FailureResult)}");
}
private static InvocationResponse GetInvocationResponse(string testName, string result)
{
return GetInvocationResponse($"{testName}-{result}");
}
private static InvocationResponse GetInvocationResponse(string result)
{
ResponseStream.SetLength(0);
JsonSerializer.Serialize(result, ResponseStream);
ResponseStream.Position = 0;
return new InvocationResponse(ResponseStream, false);
}
#endregion
}
}