-
Notifications
You must be signed in to change notification settings - Fork 502
Expand file tree
/
Copy pathStreamingE2EWithMoq.cs
More file actions
495 lines (418 loc) · 21.2 KB
/
Copy pathStreamingE2EWithMoq.cs
File metadata and controls
495 lines (418 loc) · 21.2 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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Amazon.Lambda.RuntimeSupport.Client.ResponseStreaming;
using Amazon.Lambda.RuntimeSupport.UnitTests.TestHelpers;
using Xunit;
namespace Amazon.Lambda.RuntimeSupport.UnitTests
{
[CollectionDefinition("RuntimeSupportStateCheck")]
public class RuntimeSupportStateCheckCollection { }
/// <summary>
/// End-to-end integration tests for the true-streaming architecture.
/// These tests exercise the full pipeline: LambdaBootstrap → ResponseStreamFactory →
/// ResponseStream → captured HTTP output stream.
/// </summary>
[Collection("RuntimeSupportStateCheck")]
public class StreamingE2EWithMoq : IDisposable
{
public void Dispose()
{
ResponseStreamFactory.CleanupInvocation(isMultiConcurrency: false);
ResponseStreamFactory.CleanupInvocation(isMultiConcurrency: true);
}
private static Dictionary<string, IEnumerable<string>> MakeHeaders(string requestId = "test-request-id")
=> new Dictionary<string, IEnumerable<string>>
{
{ RuntimeApiHeaders.HeaderAwsRequestId, new List<string> { requestId } },
{ RuntimeApiHeaders.HeaderInvokedFunctionArn, new List<string> { "arn:aws:lambda:us-east-1:123456789012:function:test" } },
{ RuntimeApiHeaders.HeaderAwsTenantId, new List<string> { "tenant-id" } },
{ RuntimeApiHeaders.HeaderTraceId, new List<string> { "trace-id" } },
{ RuntimeApiHeaders.HeaderDeadlineMs, new List<string> { "9999999999999" } },
};
/// <summary>
/// A capturing RuntimeApiClient that records the raw bytes written to the HTTP output stream
/// by SerializeToStreamAsync.
/// </summary>
private class CapturingStreamingRuntimeApiClient : RuntimeApiClient, IRuntimeApiClient
{
private readonly IEnvironmentVariables _envVars;
private readonly Dictionary<string, IEnumerable<string>> _headers;
public bool StartStreamingCalled { get; private set; }
public bool SendResponseCalled { get; private set; }
public bool ReportInvocationErrorCalled { get; private set; }
public byte[] CapturedHttpBytes { get; private set; }
public ResponseStream LastResponseStream { get; private set; }
public Stream LastBufferedOutputStream { get; private set; }
public Action OnStreamingReady { get; set; }
public MemoryStream CapturedOutputStream { get; private set; }
public new Amazon.Lambda.RuntimeSupport.Helpers.IConsoleLoggerWriter ConsoleLogger { get; } = new Helpers.LogLevelLoggerWriter(new SystemEnvironmentVariables());
public CapturingStreamingRuntimeApiClient(
IEnvironmentVariables envVars,
Dictionary<string, IEnumerable<string>> headers)
: base(envVars, new NoOpInternalRuntimeApiClient())
{
_envVars = envVars;
_headers = headers;
}
public new async Task<InvocationRequest> GetNextInvocationAsync(CancellationToken cancellationToken = default)
{
_headers[RuntimeApiHeaders.HeaderTraceId] = new List<string> { Guid.NewGuid().ToString() };
var inputStream = new MemoryStream(new byte[0]);
return new InvocationRequest
{
InputStream = inputStream,
LambdaContext = new LambdaContext(
new RuntimeApiHeaders(_headers),
new LambdaEnvironment(_envVars),
new TestDateTimeHelper(),
new Helpers.LogLevelLoggerWriter(_envVars))
};
}
internal override async Task<IDisposable> StartStreamingResponseAsync(
string awsRequestId, ResponseStream responseStream, CancellationToken cancellationToken = default)
{
StartStreamingCalled = true;
LastResponseStream = responseStream;
// Use a real MemoryStream as the HTTP output stream so we capture actual bytes
var captureStream = new MemoryStream();
CapturedOutputStream = captureStream;
await responseStream.SetHttpOutputStreamAsync(captureStream, cancellationToken);
// Wait for the handler to finish writing (mirrors real RawStreamingHttpClient behavior)
OnStreamingReady?.Invoke();
await responseStream.WaitForCompletionAsync(cancellationToken);
CapturedHttpBytes = captureStream.ToArray();
return new NoOpDisposable();
}
public new async Task SendResponseAsync(string awsRequestId, Stream outputStream, CancellationToken cancellationToken = default)
{
SendResponseCalled = true;
if (outputStream != null)
{
var ms = new MemoryStream();
await outputStream.CopyToAsync(ms);
ms.Position = 0;
LastBufferedOutputStream = ms;
}
}
public new Task ReportInvocationErrorAsync(string awsRequestId, Exception exception, CancellationToken cancellationToken = default)
{
ReportInvocationErrorCalled = true;
return Task.CompletedTask;
}
public new Task ReportInitializationErrorAsync(Exception exception, string errorType = null, CancellationToken cancellationToken = default)
=> Task.CompletedTask;
public new Task ReportInitializationErrorAsync(string errorType, CancellationToken cancellationToken = default)
=> Task.CompletedTask;
public new Task RestoreNextInvocationAsync(CancellationToken cancellationToken = default) => Task.CompletedTask;
public new Task ReportRestoreErrorAsync(Exception exception, string errorType = null, CancellationToken cancellationToken = default) => Task.CompletedTask;
}
private static CapturingStreamingRuntimeApiClient CreateClient(string requestId = "test-request-id")
=> new CapturingStreamingRuntimeApiClient(new TestEnvironmentVariables(), MakeHeaders(requestId));
/// <summary>
/// End-to-end: all data is transmitted correctly (content round-trip).
/// </summary>
[Fact]
public async Task Streaming_AllDataTransmitted_ContentRoundTrip()
{
var client = CreateClient();
var payload = Encoding.UTF8.GetBytes("integration test payload");
LambdaBootstrapHandler handler = async (invocation) =>
{
var stream = ResponseStreamFactory.CreateStream(Array.Empty<byte>());
await stream.WriteAsync(payload);
return new InvocationResponse(Stream.Null, false);
};
using var bootstrap = new LambdaBootstrap(handler, null, null, new TestEnvironmentVariables());
bootstrap.Client = client;
await bootstrap.InvokeOnceAsync();
var output = client.CapturedHttpBytes;
Assert.NotNull(output);
var outputStr = Encoding.UTF8.GetString(output);
Assert.Contains("integration test payload", outputStr);
}
/// <summary>
/// End-to-end: stream is finalized (final chunk written, BytesWritten matches).
/// </summary>
[Fact]
public async Task Streaming_StreamFinalized_BytesWrittenMatchesPayload()
{
var client = CreateClient();
var data = Encoding.UTF8.GetBytes("finalization check");
LambdaBootstrapHandler handler = async (invocation) =>
{
var stream = ResponseStreamFactory.CreateStream(Array.Empty<byte>());
await stream.WriteAsync(data);
return new InvocationResponse(Stream.Null, false);
};
using var bootstrap = new LambdaBootstrap(handler, null, null, new TestEnvironmentVariables());
bootstrap.Client = client;
await bootstrap.InvokeOnceAsync();
Assert.NotNull(client.LastResponseStream);
Assert.Equal(data.Length, client.LastResponseStream.BytesWritten);
}
/// <summary>
/// End-to-end: handler does NOT call CreateStream — response goes via buffered path.
/// Verifies SendResponseAsync is called and streaming headers are absent.
/// </summary>
[Fact]
public async Task Buffered_HandlerDoesNotCallCreateStream_UsesSendResponsePath()
{
var client = CreateClient();
var responseBody = Encoding.UTF8.GetBytes("buffered response body");
LambdaBootstrapHandler handler = async (invocation) =>
{
await Task.Yield();
return new InvocationResponse(new MemoryStream(responseBody));
};
using var bootstrap = new LambdaBootstrap(handler, null, null, new TestEnvironmentVariables());
bootstrap.Client = client;
await bootstrap.InvokeOnceAsync();
Assert.False(client.StartStreamingCalled, "StartStreamingResponseAsync should NOT be called for buffered mode");
Assert.True(client.SendResponseCalled, "SendResponseAsync should be called for buffered mode");
Assert.Null(client.CapturedHttpBytes);
}
/// <summary>
/// End-to-end: buffered response body is transmitted correctly.
/// </summary>
[Fact]
public async Task Buffered_ResponseBodyTransmittedCorrectly()
{
var client = CreateClient();
var responseBody = Encoding.UTF8.GetBytes("hello buffered world");
LambdaBootstrapHandler handler = async (invocation) =>
{
await Task.Yield();
return new InvocationResponse(new MemoryStream(responseBody));
};
using var bootstrap = new LambdaBootstrap(handler, null, null, new TestEnvironmentVariables());
bootstrap.Client = client;
await bootstrap.InvokeOnceAsync();
Assert.True(client.SendResponseCalled);
Assert.NotNull(client.LastBufferedOutputStream);
var received = new MemoryStream();
await client.LastBufferedOutputStream.CopyToAsync(received);
Assert.Equal(responseBody, received.ToArray());
}
/// <summary>
/// Multi-concurrency: concurrent invocations use AsyncLocal for state isolation.
/// Each invocation independently uses streaming or buffered mode without interference.
/// </summary>
[Fact]
public async Task MultiConcurrency_ConcurrentInvocations_StateIsolated()
{
const int concurrency = 3;
var results = new ConcurrentDictionary<string, string>();
var barrier = new SemaphoreSlim(0, concurrency);
var allStarted = new SemaphoreSlim(0, concurrency);
// Simulate concurrent invocations using AsyncLocal directly
var tasks = new List<Task>();
for (int i = 0; i < concurrency; i++)
{
var requestId = $"req-{i}";
var payload = $"payload-{i}";
tasks.Add(Task.Run(async () =>
{
var mockClient = new MockMultiConcurrencyStreamingClient();
ResponseStreamFactory.InitializeInvocation(
requestId,
isMultiConcurrency: true,
mockClient,
CancellationToken.None);
var stream = ResponseStreamFactory.CreateStream(Array.Empty<byte>());
allStarted.Release();
// Wait until all tasks have started (to ensure true concurrency)
await barrier.WaitAsync();
await stream.WriteAsync(Encoding.UTF8.GetBytes(payload));
stream.MarkCompleted();
// Verify this invocation's stream is still accessible
var retrieved = ResponseStreamFactory.GetStreamIfCreated(isMultiConcurrency: true);
results[requestId] = retrieved != null ? payload : "MISSING";
ResponseStreamFactory.CleanupInvocation(isMultiConcurrency: true);
}));
}
// Wait for all tasks to start, then release the barrier
for (int i = 0; i < concurrency; i++)
await allStarted.WaitAsync();
barrier.Release(concurrency);
await Task.WhenAll(tasks);
// Each invocation should have seen its own stream
Assert.Equal(concurrency, results.Count);
for (int i = 0; i < concurrency; i++)
Assert.Equal($"payload-{i}", results[$"req-{i}"]);
}
/// <summary>
/// Multi-concurrency: streaming and buffered invocations can run concurrently without interference.
/// </summary>
[Fact]
public async Task MultiConcurrency_StreamingAndBufferedMixedConcurrently_NoInterference()
{
var streamingResults = new ConcurrentBag<bool>();
var bufferedResults = new ConcurrentBag<bool>();
var barrier = new SemaphoreSlim(0, 4);
var allStarted = new SemaphoreSlim(0, 4);
var tasks = new List<Task>();
// 2 streaming invocations
for (int i = 0; i < 2; i++)
{
var requestId = $"stream-{i}";
tasks.Add(Task.Run(async () =>
{
var mockClient = new MockMultiConcurrencyStreamingClient();
ResponseStreamFactory.InitializeInvocation(
requestId,
isMultiConcurrency: true, mockClient, CancellationToken.None);
var stream = ResponseStreamFactory.CreateStream(Array.Empty<byte>());
allStarted.Release();
await barrier.WaitAsync();
await stream.WriteAsync(Encoding.UTF8.GetBytes("streaming data"));
stream.MarkCompleted();
var retrieved = ResponseStreamFactory.GetStreamIfCreated(isMultiConcurrency: true);
streamingResults.Add(retrieved != null);
ResponseStreamFactory.CleanupInvocation(isMultiConcurrency: true);
}));
}
// 2 buffered invocations (no CreateStream)
for (int i = 0; i < 2; i++)
{
var requestId = $"buffered-{i}";
tasks.Add(Task.Run(async () =>
{
var mockClient = new MockMultiConcurrencyStreamingClient();
ResponseStreamFactory.InitializeInvocation(
requestId,
isMultiConcurrency: true, mockClient, CancellationToken.None);
allStarted.Release();
await barrier.WaitAsync();
// No CreateStream — buffered mode
var retrieved = ResponseStreamFactory.GetStreamIfCreated(isMultiConcurrency: true);
bufferedResults.Add(retrieved == null); // should be null (no stream created)
ResponseStreamFactory.CleanupInvocation(isMultiConcurrency: true);
}));
}
for (int i = 0; i < 4; i++)
await allStarted.WaitAsync();
barrier.Release(4);
await Task.WhenAll(tasks);
Assert.Equal(2, streamingResults.Count);
Assert.All(streamingResults, r => Assert.True(r, "Streaming invocation should have a stream"));
Assert.Equal(2, bufferedResults.Count);
Assert.All(bufferedResults, r => Assert.True(r, "Buffered invocation should have no stream"));
}
/// <summary>
/// Minimal mock RuntimeApiClient for multi-concurrency tests.
/// Accepts StartStreamingResponseAsync calls without real HTTP.
/// </summary>
private class MockMultiConcurrencyStreamingClient : RuntimeApiClient
{
public MockMultiConcurrencyStreamingClient()
: base(new TestEnvironmentVariables(), new NoOpInternalRuntimeApiClient()) { }
internal override async Task<IDisposable> StartStreamingResponseAsync(
string awsRequestId, ResponseStream responseStream, CancellationToken cancellationToken = default)
{
// Provide the HTTP output stream so writes don't block
await responseStream.SetHttpOutputStreamAsync(new MemoryStream());
await responseStream.WaitForCompletionAsync();
return new NoOpDisposable();
}
}
/// <summary>
/// Backward compatibility: existing handler signatures (event + ILambdaContext) work without modification.
/// </summary>
[Fact]
public async Task BackwardCompat_ExistingHandlerSignature_WorksUnchanged()
{
var client = CreateClient();
bool handlerCalled = false;
// Simulate a classic handler that returns a buffered response
LambdaBootstrapHandler handler = async (invocation) =>
{
handlerCalled = true;
await Task.Yield();
return new InvocationResponse(new MemoryStream(Encoding.UTF8.GetBytes("classic response")));
};
using var bootstrap = new LambdaBootstrap(handler, null, null, new TestEnvironmentVariables());
bootstrap.Client = client;
await bootstrap.InvokeOnceAsync();
Assert.True(handlerCalled);
Assert.True(client.SendResponseCalled);
Assert.False(client.StartStreamingCalled);
}
/// <summary>
/// Backward compatibility: no regression in buffered response behavior — response body is correct.
/// </summary>
[Fact]
public async Task BackwardCompat_BufferedResponse_NoRegression()
{
var client = CreateClient();
var expected = Encoding.UTF8.GetBytes("no regression here");
LambdaBootstrapHandler handler = async (invocation) =>
{
await Task.Yield();
return new InvocationResponse(new MemoryStream(expected));
};
using var bootstrap = new LambdaBootstrap(handler, null, null, new TestEnvironmentVariables());
bootstrap.Client = client;
await bootstrap.InvokeOnceAsync();
Assert.True(client.SendResponseCalled);
Assert.NotNull(client.LastBufferedOutputStream);
var received = new MemoryStream();
await client.LastBufferedOutputStream.CopyToAsync(received);
Assert.Equal(expected, received.ToArray());
}
/// <summary>
/// Backward compatibility: handler that returns null OutputStream still works.
/// </summary>
[Fact]
public async Task BackwardCompat_NullOutputStream_HandledGracefully()
{
var client = CreateClient();
LambdaBootstrapHandler handler = async (invocation) =>
{
await Task.Yield();
return new InvocationResponse(Stream.Null, false);
};
using var bootstrap = new LambdaBootstrap(handler, null, null, new TestEnvironmentVariables());
bootstrap.Client = client;
// Should not throw
await bootstrap.InvokeOnceAsync();
Assert.True(client.SendResponseCalled);
}
/// <summary>
/// Backward compatibility: handler that throws before CreateStream uses standard error path.
/// </summary>
[Fact]
public async Task BackwardCompat_HandlerThrows_StandardErrorReportingUsed()
{
var client = CreateClient();
LambdaBootstrapHandler handler = async (invocation) =>
{
await Task.Yield();
throw new Exception("classic handler error");
};
using var bootstrap = new LambdaBootstrap(handler, null, null, new TestEnvironmentVariables());
bootstrap.Client = client;
await bootstrap.InvokeOnceAsync();
Assert.True(client.ReportInvocationErrorCalled);
Assert.False(client.StartStreamingCalled);
}
}
}