| 1 | namespace Zipelo.StreamProtocolTests; |
| 2 | |
| 3 | // 작은 메모리 입력과 취소 가능한 한 개의 장벽만 사용하는 I/O 대역임. |
| 4 | internal sealed class ControlledStream(byte[]? input = null) : Stream |
| 5 | { |
| 6 | private readonly MemoryStream inner = input is null ? new() : new(input, false); |
| 7 | public TaskCompletionSource Entered { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously); |
| 8 | public TaskCompletionSource Release { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously); |
| 9 | public int ReadCalls { get; private set; } |
| 10 | public int WriteCalls { get; private set; } |
| 11 | public int FlushCalls { get; private set; } |
| 12 | public long BytesRead { get; private set; } |
| 13 | public int LargestReadRequest { get; private set; } |
| 14 | public int ReadFragment { get; init; } = int.MaxValue; |
| 15 | public int BlockRead { get; set; } = -1; |
| 16 | public int BlockWrite { get; set; } = -1; |
| 17 | public int BlockFlush { get; set; } = -1; |
| 18 | public int FailRead { get; set; } = -1; |
| 19 | public int FailWrite { get; set; } = -1; |
| 20 | public long ReadCeiling { get; set; } = long.MaxValue; |
| 21 | public IOException Failure { get; } = new("테스트 I/O 실패"); |
| 22 | public byte[] Written => inner.ToArray(); |
| 23 | |
| 24 | private async Task Wait(CancellationToken token) |
| 25 | { |
| 26 | Entered.TrySetResult(); |
| 27 | await Release.Task.WaitAsync(token); |
| 28 | } |
| 29 | |
| 30 | public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken token = default) |
| 31 | { |
| 32 | ReadCalls++; |
| 33 | LargestReadRequest = Math.Max(LargestReadRequest, buffer.Length); |
| 34 | if (ReadCalls == FailRead) { throw Failure; } |
| 35 | if (ReadCalls == BlockRead) { await Wait(token); } |
| 36 | token.ThrowIfCancellationRequested(); |
| 37 | if (buffer.Length != 0 && inner.Position >= ReadCeiling) |
| 38 | { |
| 39 | throw new InvalidOperationException("거절해야 할 본문에 접근함"); |
| 40 | } |
| 41 | var count = await inner.ReadAsync(buffer[..Math.Min(buffer.Length, ReadFragment)], token); |
| 42 | BytesRead += count; |
| 43 | return count; |
| 44 | } |
| 45 | |
| 46 | public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken token = default) |
| 47 | { |
| 48 | WriteCalls++; |
| 49 | if (WriteCalls == FailWrite) { throw Failure; } |
| 50 | if (WriteCalls == BlockWrite) { await Wait(token); } |
| 51 | await inner.WriteAsync(buffer, token); |
| 52 | } |
| 53 | |
| 54 | public override async Task FlushAsync(CancellationToken token) |
| 55 | { |
| 56 | FlushCalls++; |
| 57 | if (FlushCalls == BlockFlush) { await Wait(token); } |
| 58 | token.ThrowIfCancellationRequested(); |
| 59 | } |
| 60 | |
| 61 | protected override void Dispose(bool disposing) |
| 62 | { |
| 63 | if (disposing) { inner.Dispose(); } |
| 64 | base.Dispose(disposing); |
| 65 | } |
| 66 | |
| 67 | public override bool CanRead => inner.CanRead; |
| 68 | public override bool CanWrite => inner.CanWrite; |
| 69 | public override bool CanSeek => false; |
| 70 | public override long Length => inner.Length; |
| 71 | public override long Position { get => inner.Position; set => throw new NotSupportedException(); } |
| 72 | public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException(); |
| 73 | public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException(); |
| 74 | public override void Flush() => throw new NotSupportedException(); |
| 75 | public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); |
| 76 | public override void SetLength(long value) => throw new NotSupportedException(); |
| 77 | } |