| 1 | using System.Buffers.Binary; |
| 2 | using Zipelo.Runtime.Streaming; |
| 3 | |
| 4 | namespace Zipelo.StreamProtocolTests; |
| 5 | |
| 6 | internal static class ChannelTests |
| 7 | { |
| 8 | private static readonly TimeSpan Guard = TimeSpan.FromSeconds(4); |
| 9 | |
| 10 | public static async Task RoundTrip(int[] sizes, int fragment = int.MaxValue) |
| 11 | { |
| 12 | var frames = Wire.JobFrames(sizes); |
| 13 | using var output = new ControlledStream(); |
| 14 | using (var writer = new WorkerStreamChannel(output, WorkerStreamDirection.Write, Wire.Job)) |
| 15 | { |
| 16 | foreach (var frame in frames) { await Wire.Write(writer, frame); } |
| 17 | Check.Equal(WorkerStreamPhase.Terminal, writer.Phase); |
| 18 | Check.Equal((long)sizes.Sum(), writer.TotalDataBytes); |
| 19 | Check.Equal(frames.Length, writer.FramesAccepted); |
| 20 | Check.Equal((long)output.Written.Length, writer.TotalWireBytes); |
| 21 | } |
| 22 | Check.That(output.CanWrite, "채널이 호출자 스트림을 닫음"); |
| 23 | Check.Bytes(Wire.Join(frames.Select(Wire.Encode).ToArray()), output.Written); |
| 24 | using var input = new ControlledStream(output.Written) { ReadFragment = fragment }; |
| 25 | using var reader = new WorkerStreamChannel(input, WorkerStreamDirection.Read, Wire.Job); |
| 26 | foreach (var expected in frames) |
| 27 | { |
| 28 | var actual = await reader.ReadAsync() ?? throw new InvalidOperationException("조기 EOF임"); |
| 29 | Check.Equal(expected.Header, actual.Header); |
| 30 | Check.Bytes(expected.Payload.Span, actual.Payload.Span); |
| 31 | } |
| 32 | Check.Equal(WorkerStreamPhase.Terminal, reader.Phase); |
| 33 | Check.That(await reader.ReadAsync() is null, "종료 후 EOF 확인에 실패함"); |
| 34 | Check.Equal(WorkerStreamPhase.InputCompleted, reader.Phase); |
| 35 | Check.Equal<WorkerStreamTerminalStatus?>(WorkerStreamTerminalStatus.Completed, reader.TerminalStatus); |
| 36 | var calls = input.ReadCalls; |
| 37 | Check.That(await reader.ReadAsync() is null, "완료된 수신을 재개함"); |
| 38 | Check.Equal(calls, input.ReadCalls); |
| 39 | Check.Equal((long)sizes.Sum(), reader.TotalDataBytes); |
| 40 | Check.Equal((long)output.Written.Length, reader.TotalWireBytes); |
| 41 | Check.That(input.LargestReadRequest <= 65536, "청크 상한보다 큰 읽기를 요청함"); |
| 42 | } |
| 43 | |
| 44 | public static async Task GoldenHeader() |
| 45 | { |
| 46 | Check.Equal(64, WorkerStreamChannel.HeaderBytes); |
| 47 | Check.Equal(65536, WorkerStreamChannel.MaxChunkBytes); |
| 48 | Check.Equal((ushort)2, WorkerStreamChannel.Version); |
| 49 | var expected = Convert.FromHexString( |
| 50 | "5A49503202000100000000000000000033221100554477668899AABBCCDDEEFF" + new string('0', 64)); |
| 51 | using var stream = new ControlledStream(); |
| 52 | using var writer = new WorkerStreamChannel(stream, WorkerStreamDirection.Write, Wire.Job); |
| 53 | await Wire.Write(writer, Wire.Begin); |
| 54 | Check.Bytes(expected, stream.Written); |
| 55 | Check.Bytes(expected, Wire.Encode(Wire.Begin)); |
| 56 | } |
| 57 | |
| 58 | private static async Task Reject(byte[] bytes, int accepted, WorkerStreamError error, long? ceiling = null) |
| 59 | { |
| 60 | using var stream = new ControlledStream(bytes) { ReadCeiling = ceiling ?? long.MaxValue }; |
| 61 | using var reader = new WorkerStreamChannel(stream, WorkerStreamDirection.Read, Wire.Job); |
| 62 | for (var index = 0; index < accepted; index++) |
| 63 | { |
| 64 | Check.That(await reader.ReadAsync() is not null, "정상 접두 프레임이 거절됨"); |
| 65 | } |
| 66 | var allocatedBefore = GC.GetAllocatedBytesForCurrentThread(); |
| 67 | var rejected = reader.ReadAsync().AsTask(); |
| 68 | Check.That(rejected.IsCompleted, "메모리 입력 검사가 예기치 않게 대기함"); |
| 69 | await Check.Error(error, async () => { await rejected; }); |
| 70 | var allocated = GC.GetAllocatedBytesForCurrentThread() - allocatedBefore; |
| 71 | Check.That(allocated >= 0 && allocated < 1024 * 1024, "작은 오류 프레임에 1 MiB 이상 할당함"); |
| 72 | Check.Equal(WorkerStreamPhase.Faulted, reader.Phase); |
| 73 | Check.Equal<WorkerStreamTerminalStatus?>(null, reader.TerminalStatus); |
| 74 | await Check.Error(WorkerStreamError.Faulted, async () => { await reader.ReadAsync(); }); |
| 75 | if (ceiling is not null) { Check.Equal(ceiling.Value, stream.BytesRead); } |
| 76 | Check.That(stream.LargestReadRequest <= 65536, "큰 본문 읽기를 요청함"); |
| 77 | } |
| 78 | |
| 79 | public static async Task TruncatedHeaders() |
| 80 | { |
| 81 | var bytes = Wire.Encode(Wire.Begin); |
| 82 | for (var cut = 0; cut < 64; cut++) { await Reject(bytes[..cut], 0, WorkerStreamError.Truncated); } |
| 83 | } |
| 84 | |
| 85 | public static async Task TruncatedBodies() |
| 86 | { |
| 87 | var item = Wire.Encode(Wire.Frame(WorkerStreamFrameKind.BeginItem, 1, Wire.Length(8), Wire.Item)); |
| 88 | for (var cut = 0; cut < 8; cut++) |
| 89 | { |
| 90 | await Reject(Wire.Join(Wire.Encode(Wire.Begin), item[..(64 + cut)]), 1, WorkerStreamError.Truncated); |
| 91 | } |
| 92 | var frames = Wire.JobFrames(8); |
| 93 | var data = Wire.Encode(frames[2]); |
| 94 | for (var cut = 0; cut < 8; cut++) |
| 95 | { |
| 96 | await Reject(Wire.Join(Wire.Encode(frames[0]), Wire.Encode(frames[1]), data[..(64 + cut)]), |
| 97 | 2, WorkerStreamError.Truncated); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | public static async Task FixedFields() |
| 102 | { |
| 103 | foreach (var offset in new[] { 0, 3, 4, 5, 12, 13, 14, 15, 56, 57, 58, 59, 60, 61, 62, 63 }) |
| 104 | { |
| 105 | var bytes = Wire.Encode(Wire.Begin); |
| 106 | bytes[offset] ^= 0x80; |
| 107 | await Reject(bytes, 0, WorkerStreamError.InvalidHeader, 64); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | public static async Task KindsAndLengths() |
| 112 | { |
| 113 | foreach (var kind in new ushort[] { 0, 6, ushort.MaxValue }) |
| 114 | { |
| 115 | var bytes = Wire.Encode(Wire.Begin); |
| 116 | BinaryPrimitives.WriteUInt16LittleEndian(bytes.AsSpan(6), kind); |
| 117 | await Reject(bytes, 0, WorkerStreamError.InvalidLength, 64); |
| 118 | } |
| 119 | foreach (var (kind, length) in new (ushort, uint)[] |
| 120 | { |
| 121 | (1, 1), (2, 0), (2, 7), (2, 9), (3, 0), (3, 65537), |
| 122 | (3, int.MaxValue), (3, uint.MaxValue), (4, 0), (4, 9), (5, 0), (5, 2) |
| 123 | }) |
| 124 | { |
| 125 | var bytes = Wire.Encode(Wire.Begin); |
| 126 | BinaryPrimitives.WriteUInt16LittleEndian(bytes.AsSpan(6), kind); |
| 127 | BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(8), length); |
| 128 | await Reject(bytes, 0, WorkerStreamError.InvalidLength, 64); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | public static async Task HugeDeclarations() |
| 133 | { |
| 134 | foreach (var length in new ulong[] { 16777217, int.MaxValue, (ulong)long.MaxValue, ulong.MaxValue }) |
| 135 | { |
| 136 | var beginItem = Wire.Frame(WorkerStreamFrameKind.BeginItem, 1, Wire.Length(length), Wire.Item); |
| 137 | await Reject(Wire.Join(Wire.Encode(Wire.Begin), Wire.Encode(beginItem)), 1, |
| 138 | WorkerStreamError.BudgetExceeded, 136); |
| 139 | } |
| 140 | var limitedItem = Wire.Frame(WorkerStreamFrameKind.BeginItem, 1, Wire.Length(0), Wire.Item); |
| 141 | using var stream = new ControlledStream(Wire.Join(Wire.Encode(Wire.Begin), Wire.Encode(limitedItem))) |
| 142 | { |
| 143 | ReadCeiling = 128 |
| 144 | }; |
| 145 | using var reader = new WorkerStreamChannel(stream, WorkerStreamDirection.Read, Wire.Job, |
| 146 | new() { MaxWireBytes = 129 }); |
| 147 | await reader.ReadAsync(); |
| 148 | await Check.Error(WorkerStreamError.BudgetExceeded, async () => { await reader.ReadAsync(); }); |
| 149 | Check.Equal(128L, stream.BytesRead); |
| 150 | Check.Equal(WorkerStreamPhase.Faulted, reader.Phase); |
| 151 | } |
| 152 | |
| 153 | public static async Task EarlyEof() |
| 154 | { |
| 155 | var frames = Wire.JobFrames(1); |
| 156 | for (var count = 0; count < frames.Length; count++) |
| 157 | { |
| 158 | await Reject(Wire.Join(frames.Take(count).Select(Wire.Encode).ToArray()), |
| 159 | count, WorkerStreamError.Truncated); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | public static async Task TrailingData() |
| 164 | { |
| 165 | var frames = Wire.JobFrames(); |
| 166 | var complete = Wire.Join(frames.Select(Wire.Encode).ToArray()); |
| 167 | foreach (var suffix in new[] { new byte[] { 0 }, Wire.Encode(Wire.End(2)) }) |
| 168 | { |
| 169 | await Reject(Wire.Join(complete, suffix), 2, WorkerStreamError.TrailingData); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | public static async Task FailedTerminals() |
| 174 | { |
| 175 | foreach (var status in new byte[] { 2, 3 }) |
| 176 | { |
| 177 | var frames = new[] { Wire.Begin, |
| 178 | Wire.Frame(WorkerStreamFrameKind.BeginItem, 1, Wire.Length(10), Wire.Item), Wire.End(2, status) }; |
| 179 | using var stream = new ControlledStream(Wire.Join(frames.Select(Wire.Encode).ToArray())); |
| 180 | using var reader = new WorkerStreamChannel(stream, WorkerStreamDirection.Read, Wire.Job); |
| 181 | foreach (var unused in frames) { Check.That(await reader.ReadAsync() is not null, "조기 종료임"); } |
| 182 | Check.That(await reader.ReadAsync() is null, "EOF가 아님"); |
| 183 | Check.Equal(WorkerStreamPhase.InputCompleted, reader.Phase); |
| 184 | Check.Equal<WorkerStreamTerminalStatus?>((WorkerStreamTerminalStatus)status, reader.TerminalStatus); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | public static async Task PreCancelled() |
| 189 | { |
| 190 | foreach (var direction in new[] { WorkerStreamDirection.Read, WorkerStreamDirection.Write }) |
| 191 | { |
| 192 | using var stream = new ControlledStream(); |
| 193 | using var channel = new WorkerStreamChannel(stream, direction, Wire.Job); |
| 194 | using var cancellation = new CancellationTokenSource(); |
| 195 | cancellation.Cancel(); |
| 196 | await Check.ThrowsAsync<OperationCanceledException>(() => direction == WorkerStreamDirection.Read |
| 197 | ? Read(channel, cancellation.Token) : Wire.Write(channel, Wire.Begin, cancellation.Token)); |
| 198 | Check.Equal(0, stream.ReadCalls + stream.WriteCalls); |
| 199 | Check.Equal(WorkerStreamPhase.Faulted, channel.Phase); |
| 200 | await Check.Error(WorkerStreamError.Faulted, () => direction == WorkerStreamDirection.Read |
| 201 | ? Read(channel, default) : Wire.Write(channel, Wire.Begin)); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | private static async Task Read(WorkerStreamChannel channel, CancellationToken token) => |
| 206 | _ = await channel.ReadAsync(token); |
| 207 | |
| 208 | public static async Task BlockedRead(bool body, bool deadline, bool resume = false) |
| 209 | { |
| 210 | var frames = Wire.JobFrames(8); |
| 211 | using var stream = new ControlledStream(Wire.Join(frames.Select(Wire.Encode).ToArray())); |
| 212 | using var channel = new WorkerStreamChannel(stream, WorkerStreamDirection.Read, Wire.Job, |
| 213 | new() { MaxDuration = deadline ? TimeSpan.FromMilliseconds(180) : TimeSpan.FromSeconds(10) }); |
| 214 | await channel.ReadAsync(); |
| 215 | await channel.ReadAsync(); |
| 216 | stream.BlockRead = stream.ReadCalls + (body ? 3 : 1); |
| 217 | using var cancellation = new CancellationTokenSource(); |
| 218 | var pending = channel.ReadAsync(cancellation.Token).AsTask(); |
| 219 | try |
| 220 | { |
| 221 | await stream.Entered.Task.WaitAsync(Guard); |
| 222 | Check.That(!pending.IsCompleted, "생산자를 기다리지 않고 읽기를 완료함"); |
| 223 | Check.Equal(2, channel.FramesAccepted); |
| 224 | if (resume) |
| 225 | { |
| 226 | stream.Release.TrySetResult(); |
| 227 | var frame = await pending.WaitAsync(Guard) ?? throw new InvalidOperationException(); |
| 228 | Check.Bytes(frames[2].Payload.Span, frame.Payload.Span); |
| 229 | return; |
| 230 | } |
| 231 | if (!deadline) { cancellation.Cancel(); } |
| 232 | if (deadline) { await Check.Error(WorkerStreamError.BudgetExceeded, async () => { await pending.WaitAsync(Guard); }); } |
| 233 | else { await Check.ThrowsAsync<OperationCanceledException>(async () => { await pending.WaitAsync(Guard); }); } |
| 234 | Check.Equal(WorkerStreamPhase.Faulted, channel.Phase); |
| 235 | Check.Equal<WorkerStreamTerminalStatus?>(null, channel.TerminalStatus); |
| 236 | if (!deadline) { await Check.Error(WorkerStreamError.Faulted, () => Read(channel, default)); } |
| 237 | } |
| 238 | finally |
| 239 | { |
| 240 | cancellation.Cancel(); |
| 241 | try { await pending.WaitAsync(Guard); } |
| 242 | catch (OperationCanceledException) { } |
| 243 | catch (WorkerStreamException) { } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | public static async Task BlockedWrite(string stage, bool deadline, bool resume = false) |
| 248 | { |
| 249 | using var stream = new ControlledStream(); |
| 250 | using var channel = new WorkerStreamChannel(stream, WorkerStreamDirection.Write, Wire.Job, |
| 251 | new() { MaxDuration = deadline ? TimeSpan.FromMilliseconds(180) : TimeSpan.FromSeconds(10) }); |
| 252 | await Wire.Write(channel, Wire.Begin); |
| 253 | await Wire.Write(channel, Wire.Frame(WorkerStreamFrameKind.BeginItem, 1, Wire.Length(3), Wire.Item)); |
| 254 | if (stage == "header") { stream.BlockWrite = stream.WriteCalls + 1; } |
| 255 | if (stage == "body") { stream.BlockWrite = stream.WriteCalls + 2; } |
| 256 | if (stage == "flush") { stream.BlockFlush = stream.FlushCalls + 1; } |
| 257 | byte[] payload = [1, 2, 3]; |
| 258 | var frame = Wire.Frame(WorkerStreamFrameKind.Data, 2, payload, Wire.Item); |
| 259 | using var cancellation = new CancellationTokenSource(); |
| 260 | var pending = Wire.Write(channel, frame, cancellation.Token); |
| 261 | try |
| 262 | { |
| 263 | await stream.Entered.Task.WaitAsync(Guard); |
| 264 | Check.That(!pending.IsCompleted, "소비자나 Flush를 기다리지 않고 완료함"); |
| 265 | if (resume) |
| 266 | { |
| 267 | payload.AsSpan().Fill(99); |
| 268 | stream.Release.TrySetResult(); |
| 269 | await pending.WaitAsync(Guard); |
| 270 | Check.Bytes(new byte[] { 1, 2, 3 }, stream.Written.AsSpan(200, 3)); |
| 271 | Check.Equal(3L, channel.TotalDataBytes); |
| 272 | return; |
| 273 | } |
| 274 | if (!deadline) { cancellation.Cancel(); } |
| 275 | if (deadline) { await Check.Error(WorkerStreamError.BudgetExceeded, () => pending.WaitAsync(Guard)); } |
| 276 | else { await Check.ThrowsAsync<OperationCanceledException>(() => pending.WaitAsync(Guard)); } |
| 277 | Check.Equal(WorkerStreamPhase.Faulted, channel.Phase); |
| 278 | Check.Equal<WorkerStreamTerminalStatus?>(null, channel.TerminalStatus); |
| 279 | if (!deadline) { await Check.Error(WorkerStreamError.Faulted, () => Wire.Write(channel, Wire.Begin)); } |
| 280 | } |
| 281 | finally |
| 282 | { |
| 283 | cancellation.Cancel(); |
| 284 | try { await pending.WaitAsync(Guard); } |
| 285 | catch (OperationCanceledException) { } |
| 286 | catch (WorkerStreamException) { } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | public static async Task MissingEof() |
| 291 | { |
| 292 | using var stream = new ControlledStream(Wire.Join(Wire.Encode(Wire.Begin), Wire.Encode(Wire.End(1)))); |
| 293 | using var channel = new WorkerStreamChannel(stream, WorkerStreamDirection.Read, Wire.Job, |
| 294 | new() { MaxDuration = TimeSpan.FromMilliseconds(180) }); |
| 295 | await channel.ReadAsync(); |
| 296 | await channel.ReadAsync(); |
| 297 | stream.BlockRead = stream.ReadCalls + 1; |
| 298 | await Check.Error(WorkerStreamError.BudgetExceeded, async () => { await channel.ReadAsync().AsTask().WaitAsync(Guard); }); |
| 299 | Check.Equal(WorkerStreamPhase.Faulted, channel.Phase); |
| 300 | Check.Equal<WorkerStreamTerminalStatus?>(null, channel.TerminalStatus); |
| 301 | } |
| 302 | |
| 303 | public static async Task FinalStatePriority() |
| 304 | { |
| 305 | foreach (var status in new byte[] { 1, 2, 3 }) |
| 306 | { |
| 307 | using var stream = new ControlledStream(Wire.Join(Wire.Encode(Wire.Begin), Wire.Encode(Wire.End(1, status)))); |
| 308 | using var reader = new WorkerStreamChannel(stream, WorkerStreamDirection.Read, Wire.Job, |
| 309 | new() { MaxDuration = TimeSpan.FromMilliseconds(250) }); |
| 310 | await reader.ReadAsync(); |
| 311 | await reader.ReadAsync(); |
| 312 | Check.That(await reader.ReadAsync() is null, "종료 EOF가 아님"); |
| 313 | var calls = stream.ReadCalls; |
| 314 | using var cancelled = new CancellationTokenSource(); |
| 315 | cancelled.Cancel(); |
| 316 | Check.That(await reader.ReadAsync(cancelled.Token) is null, "확정 EOF를 취소가 바꿈"); |
| 317 | await Task.Delay(400); |
| 318 | Check.That(await reader.ReadAsync() is null, "확정 EOF를 기한이 바꿈"); |
| 319 | Check.Equal(WorkerStreamPhase.InputCompleted, reader.Phase); |
| 320 | Check.Equal<WorkerStreamTerminalStatus?>((WorkerStreamTerminalStatus)status, reader.TerminalStatus); |
| 321 | Check.Equal(calls, stream.ReadCalls); |
| 322 | } |
| 323 | |
| 324 | var bytes = Wire.Encode(Wire.Begin); |
| 325 | bytes[0] = 0; |
| 326 | using var input = new ControlledStream(bytes); |
| 327 | using var faulted = new WorkerStreamChannel(input, WorkerStreamDirection.Read, Wire.Job, |
| 328 | new() { MaxDuration = TimeSpan.FromMilliseconds(250) }); |
| 329 | await Check.Error(WorkerStreamError.InvalidHeader, () => Read(faulted, default)); |
| 330 | var reads = input.ReadCalls; |
| 331 | using var cancellation = new CancellationTokenSource(); |
| 332 | cancellation.Cancel(); |
| 333 | await Check.Error(WorkerStreamError.Faulted, () => Read(faulted, cancellation.Token)); |
| 334 | await Task.Delay(400); |
| 335 | await Check.Error(WorkerStreamError.Faulted, () => Read(faulted, default)); |
| 336 | Check.Equal(WorkerStreamPhase.Faulted, faulted.Phase); |
| 337 | Check.Equal<WorkerStreamTerminalStatus?>(null, faulted.TerminalStatus); |
| 338 | Check.Equal(reads, input.ReadCalls); |
| 339 | } |
| 340 | |
| 341 | public static async Task BetweenCallsDeadline() |
| 342 | { |
| 343 | using var stream = new ControlledStream(); |
| 344 | using var channel = new WorkerStreamChannel(stream, WorkerStreamDirection.Write, Wire.Job, |
| 345 | new() { MaxDuration = TimeSpan.FromMilliseconds(40) }); |
| 346 | await Task.Delay(100); |
| 347 | await Check.Error(WorkerStreamError.BudgetExceeded, () => Wire.Write(channel, Wire.Begin)); |
| 348 | Check.Equal(0, stream.WriteCalls); |
| 349 | Check.Equal(WorkerStreamPhase.Faulted, channel.Phase); |
| 350 | } |
| 351 | |
| 352 | public static async Task IoFailure(bool read) |
| 353 | { |
| 354 | using var stream = new ControlledStream(read ? Wire.Encode(Wire.Begin) : null) |
| 355 | { |
| 356 | FailRead = read ? 2 : -1, FailWrite = read ? -1 : 2 |
| 357 | }; |
| 358 | using var channel = new WorkerStreamChannel(stream, |
| 359 | read ? WorkerStreamDirection.Read : WorkerStreamDirection.Write, Wire.Job); |
| 360 | var exception = await Check.ThrowsAsync<IOException>(() => read |
| 361 | ? Read(channel, default) : Wire.Write(channel, Wire.Begin)); |
| 362 | Check.That(ReferenceEquals(stream.Failure, exception), "원래 I/O 오류를 바꿈"); |
| 363 | Check.Equal(WorkerStreamPhase.Faulted, channel.Phase); |
| 364 | await Check.Error(WorkerStreamError.Faulted, () => read |
| 365 | ? Read(channel, default) : Wire.Write(channel, Wire.Begin)); |
| 366 | } |
| 367 | |
| 368 | public static async Task ConcurrentCalls() |
| 369 | { |
| 370 | using var stream = new ControlledStream(Wire.Encode(Wire.Begin)) { BlockRead = 1 }; |
| 371 | using var channel = new WorkerStreamChannel(stream, WorkerStreamDirection.Read, Wire.Job); |
| 372 | using var cancellation = new CancellationTokenSource(); |
| 373 | var pending = channel.ReadAsync(cancellation.Token).AsTask(); |
| 374 | try |
| 375 | { |
| 376 | await stream.Entered.Task.WaitAsync(Guard); |
| 377 | await Check.ThrowsAsync<InvalidOperationException>(() => Read(channel, default)); |
| 378 | Check.Throws<InvalidOperationException>(channel.Dispose); |
| 379 | Check.Equal(WorkerStreamPhase.Ready, channel.Phase); |
| 380 | stream.Release.TrySetResult(); |
| 381 | Check.Equal(Wire.Begin.Header, (await pending.WaitAsync(Guard))!.Header); |
| 382 | } |
| 383 | finally |
| 384 | { |
| 385 | cancellation.Cancel(); |
| 386 | try { await pending.WaitAsync(Guard); } |
| 387 | catch (OperationCanceledException) { } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | public static async Task DirectionAndLifetime() |
| 392 | { |
| 393 | using var stream = new ControlledStream(); |
| 394 | using var channel = new WorkerStreamChannel(stream, WorkerStreamDirection.Write, Wire.Job); |
| 395 | await Check.ThrowsAsync<InvalidOperationException>(() => Read(channel, default)); |
| 396 | Check.Equal(WorkerStreamPhase.Ready, channel.Phase); |
| 397 | await Wire.Write(channel, Wire.Begin); |
| 398 | channel.Dispose(); |
| 399 | channel.Dispose(); |
| 400 | Check.That(stream.CanWrite, "호출자 스트림을 닫음"); |
| 401 | await Check.ThrowsAsync<ObjectDisposedException>(() => Wire.Write(channel, Wire.End(1))); |
| 402 | } |
| 403 | |
| 404 | public static async Task InvalidWrite() |
| 405 | { |
| 406 | foreach (var length in new[] { -1, 1, 65537, int.MaxValue }) |
| 407 | { |
| 408 | using var stream = new ControlledStream(); |
| 409 | using var writer = new WorkerStreamChannel(stream, WorkerStreamDirection.Write, Wire.Job); |
| 410 | var frame = Wire.Begin with { Header = Wire.Begin.Header with { PayloadLength = length } }; |
| 411 | await Check.Error(WorkerStreamError.InvalidLength, () => Wire.Write(writer, frame)); |
| 412 | Check.Equal(0, stream.WriteCalls); |
| 413 | Check.Equal(WorkerStreamPhase.Faulted, writer.Phase); |
| 414 | } |
| 415 | using var output = new ControlledStream(); |
| 416 | using var channel = new WorkerStreamChannel(output, WorkerStreamDirection.Write, Wire.Job); |
| 417 | await Check.Error(WorkerStreamError.InvalidLength, |
| 418 | () => channel.WriteAsync(Wire.Begin.Header, new byte[] { 1 }).AsTask()); |
| 419 | Check.Equal(0, output.WriteCalls); |
| 420 | } |
| 421 | } |