|
| 1 | +/* |
| 2 | + * Copyright 2019 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; |
| 17 | +using System.Threading.Tasks; |
| 18 | +using Xunit; |
| 19 | + |
| 20 | +namespace Amazon.Lambda.RuntimeSupport.UnitTests |
| 21 | +{ |
| 22 | + public class ResponseStreamTests |
| 23 | + { |
| 24 | + private const long MaxResponseSize = 20 * 1024 * 1024; // 20 MiB |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public void Constructor_InitializesStateCorrectly() |
| 28 | + { |
| 29 | + var stream = new ResponseStream(MaxResponseSize); |
| 30 | + |
| 31 | + Assert.Equal(0, stream.BytesWritten); |
| 32 | + Assert.False(stream.IsCompleted); |
| 33 | + Assert.False(stream.HasError); |
| 34 | + Assert.Empty(stream.Chunks); |
| 35 | + Assert.Null(stream.ReportedError); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public async Task WriteAsync_ByteArray_BuffersDataCorrectly() |
| 40 | + { |
| 41 | + var stream = new ResponseStream(MaxResponseSize); |
| 42 | + var data = new byte[] { 1, 2, 3, 4, 5 }; |
| 43 | + |
| 44 | + await stream.WriteAsync(data); |
| 45 | + |
| 46 | + Assert.Equal(5, stream.BytesWritten); |
| 47 | + Assert.Single(stream.Chunks); |
| 48 | + Assert.Equal(data, stream.Chunks[0]); |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public async Task WriteAsync_WithOffset_BuffersCorrectSlice() |
| 53 | + { |
| 54 | + var stream = new ResponseStream(MaxResponseSize); |
| 55 | + var data = new byte[] { 0, 1, 2, 3, 0 }; |
| 56 | + |
| 57 | + await stream.WriteAsync(data, 1, 3); |
| 58 | + |
| 59 | + Assert.Equal(3, stream.BytesWritten); |
| 60 | + Assert.Equal(new byte[] { 1, 2, 3 }, stream.Chunks[0]); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public async Task WriteAsync_ReadOnlyMemory_BuffersDataCorrectly() |
| 65 | + { |
| 66 | + var stream = new ResponseStream(MaxResponseSize); |
| 67 | + var data = new ReadOnlyMemory<byte>(new byte[] { 10, 20, 30 }); |
| 68 | + |
| 69 | + await stream.WriteAsync(data); |
| 70 | + |
| 71 | + Assert.Equal(3, stream.BytesWritten); |
| 72 | + Assert.Equal(new byte[] { 10, 20, 30 }, stream.Chunks[0]); |
| 73 | + } |
| 74 | + |
| 75 | + [Fact] |
| 76 | + public async Task WriteAsync_MultipleWrites_AccumulatesBytesWritten() |
| 77 | + { |
| 78 | + var stream = new ResponseStream(MaxResponseSize); |
| 79 | + |
| 80 | + await stream.WriteAsync(new byte[100]); |
| 81 | + await stream.WriteAsync(new byte[200]); |
| 82 | + await stream.WriteAsync(new byte[300]); |
| 83 | + |
| 84 | + Assert.Equal(600, stream.BytesWritten); |
| 85 | + Assert.Equal(3, stream.Chunks.Count); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public async Task WriteAsync_CopiesData_AvoidingBufferReuseIssues() |
| 90 | + { |
| 91 | + var stream = new ResponseStream(MaxResponseSize); |
| 92 | + var buffer = new byte[] { 1, 2, 3 }; |
| 93 | + |
| 94 | + await stream.WriteAsync(buffer); |
| 95 | + buffer[0] = 99; // mutate original |
| 96 | + |
| 97 | + Assert.Equal(1, stream.Chunks[0][0]); // chunk should be unaffected |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Property 6: Size Limit Enforcement - Writing beyond 20 MiB throws InvalidOperationException. |
| 102 | + /// Validates: Requirements 3.6, 3.7 |
| 103 | + /// </summary> |
| 104 | + [Theory] |
| 105 | + [InlineData(21 * 1024 * 1024)] // Single write exceeding limit |
| 106 | + public async Task SizeLimit_SingleWriteExceedingLimit_Throws(int writeSize) |
| 107 | + { |
| 108 | + var stream = new ResponseStream(MaxResponseSize); |
| 109 | + var data = new byte[writeSize]; |
| 110 | + |
| 111 | + await Assert.ThrowsAsync<InvalidOperationException>(() => stream.WriteAsync(data)); |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// Property 6: Size Limit Enforcement - Multiple writes exceeding 20 MiB throws. |
| 116 | + /// Validates: Requirements 3.6, 3.7 |
| 117 | + /// </summary> |
| 118 | + [Fact] |
| 119 | + public async Task SizeLimit_MultipleWritesExceedingLimit_Throws() |
| 120 | + { |
| 121 | + var stream = new ResponseStream(MaxResponseSize); |
| 122 | + |
| 123 | + await stream.WriteAsync(new byte[10 * 1024 * 1024]); |
| 124 | + await Assert.ThrowsAsync<InvalidOperationException>( |
| 125 | + () => stream.WriteAsync(new byte[11 * 1024 * 1024])); |
| 126 | + } |
| 127 | + |
| 128 | + [Fact] |
| 129 | + public async Task SizeLimit_ExactlyAtLimit_Succeeds() |
| 130 | + { |
| 131 | + var stream = new ResponseStream(MaxResponseSize); |
| 132 | + var data = new byte[20 * 1024 * 1024]; |
| 133 | + |
| 134 | + await stream.WriteAsync(data); |
| 135 | + |
| 136 | + Assert.Equal(MaxResponseSize, stream.BytesWritten); |
| 137 | + } |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// Property 19: Writes After Completion Rejected - Writes after completion throw InvalidOperationException. |
| 141 | + /// Validates: Requirements 8.8 |
| 142 | + /// </summary> |
| 143 | + [Fact] |
| 144 | + public async Task WriteAsync_AfterMarkCompleted_Throws() |
| 145 | + { |
| 146 | + var stream = new ResponseStream(MaxResponseSize); |
| 147 | + await stream.WriteAsync(new byte[] { 1 }); |
| 148 | + stream.MarkCompleted(); |
| 149 | + |
| 150 | + await Assert.ThrowsAsync<InvalidOperationException>( |
| 151 | + () => stream.WriteAsync(new byte[] { 2 })); |
| 152 | + } |
| 153 | + |
| 154 | + [Fact] |
| 155 | + public async Task WriteAsync_AfterReportError_Throws() |
| 156 | + { |
| 157 | + var stream = new ResponseStream(MaxResponseSize); |
| 158 | + await stream.WriteAsync(new byte[] { 1 }); |
| 159 | + await stream.ReportErrorAsync(new Exception("test")); |
| 160 | + |
| 161 | + await Assert.ThrowsAsync<InvalidOperationException>( |
| 162 | + () => stream.WriteAsync(new byte[] { 2 })); |
| 163 | + } |
| 164 | + |
| 165 | + // --- Error handling tests (2.6) --- |
| 166 | + |
| 167 | + [Fact] |
| 168 | + public async Task ReportErrorAsync_SetsErrorState() |
| 169 | + { |
| 170 | + var stream = new ResponseStream(MaxResponseSize); |
| 171 | + var exception = new InvalidOperationException("something broke"); |
| 172 | + |
| 173 | + await stream.ReportErrorAsync(exception); |
| 174 | + |
| 175 | + Assert.True(stream.HasError); |
| 176 | + Assert.Same(exception, stream.ReportedError); |
| 177 | + } |
| 178 | + |
| 179 | + [Fact] |
| 180 | + public async Task ReportErrorAsync_AfterCompleted_Throws() |
| 181 | + { |
| 182 | + var stream = new ResponseStream(MaxResponseSize); |
| 183 | + stream.MarkCompleted(); |
| 184 | + |
| 185 | + await Assert.ThrowsAsync<InvalidOperationException>( |
| 186 | + () => stream.ReportErrorAsync(new Exception("test"))); |
| 187 | + } |
| 188 | + |
| 189 | + [Fact] |
| 190 | + public async Task ReportErrorAsync_CalledTwice_Throws() |
| 191 | + { |
| 192 | + var stream = new ResponseStream(MaxResponseSize); |
| 193 | + await stream.ReportErrorAsync(new Exception("first")); |
| 194 | + |
| 195 | + await Assert.ThrowsAsync<InvalidOperationException>( |
| 196 | + () => stream.ReportErrorAsync(new Exception("second"))); |
| 197 | + } |
| 198 | + |
| 199 | + [Fact] |
| 200 | + public void MarkCompleted_SetsCompletionState() |
| 201 | + { |
| 202 | + var stream = new ResponseStream(MaxResponseSize); |
| 203 | + |
| 204 | + stream.MarkCompleted(); |
| 205 | + |
| 206 | + Assert.True(stream.IsCompleted); |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments