| 1 | using Zipelo.GzipBoundaryProbe; |
| 2 | |
| 3 | internal static class FinalizationRegression |
| 4 | { |
| 5 | public static void Register(Action<string, Action> run) |
| 6 | { |
| 7 | foreach (var selected in new[] { false, true }) |
| 8 | { |
| 9 | foreach (var scenario in new[] { "success", "body-budget", "body-cancel", "body-both", "dispose-both" }) |
| 10 | { |
| 11 | run($"finalization.dispose.{scenario}.{selected}", () => |
| 12 | { |
| 13 | VerifyDispose(selected, scenario, false); |
| 14 | VerifyDispose(selected, scenario, true); |
| 15 | }); |
| 16 | } |
| 17 | foreach (var delta in new long[] { -1, ProbeBudget.ChunkSize + 1 }) |
| 18 | { |
| 19 | for (var mode = 0; mode < 4; mode++) |
| 20 | { |
| 21 | var capturedMode = mode; |
| 22 | run($"finalization.counter.{delta}.{mode}.{selected}", () => |
| 23 | VerifyCounter(selected, delta, capturedMode)); |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | private static void VerifyDispose(bool selected, string scenario, bool throwOnDispose) |
| 30 | { |
| 31 | var fixture = Fixture.Wrap([0x01, 0x01, 0x00, 0xfe, 0xff, 0x41], [0x41], 0); |
| 32 | using var cts = new CancellationTokenSource(); |
| 33 | var clock = new ManualClock(); |
| 34 | var budget = new ProbeBudget(new(Duration: TimeSpan.FromSeconds(1)), clock); |
| 35 | var pending = new PendingValidation(); |
| 36 | var marker = new IOException("임시 수신기 Dispose 실패"); |
| 37 | var disposeCalls = 0; |
| 38 | var beforeCommitCalls = 0; |
| 39 | var bodyBudget = scenario is "body-budget" or "body-both"; |
| 40 | var bodyCancel = scenario is "body-cancel" or "body-both"; |
| 41 | var observer = new ObservedInflater(() => |
| 42 | { |
| 43 | if (bodyBudget) { clock.Advance(TimeSpan.FromSeconds(1)); } |
| 44 | if (bodyCancel) { cts.Cancel(); } |
| 45 | }); |
| 46 | var error = Capture(() => pending.Execute(fixture.Bytes, budget, selected, cts.Token, |
| 47 | beforeCommit: () => beforeCommitCalls++, inflater: observer, onSinkDispose: () => |
| 48 | { |
| 49 | disposeCalls++; |
| 50 | if (scenario == "dispose-both") |
| 51 | { |
| 52 | clock.Advance(TimeSpan.FromSeconds(1)); |
| 53 | cts.Cancel(); |
| 54 | } |
| 55 | if (throwOnDispose) { throw marker; } |
| 56 | })); |
| 57 | Check(disposeCalls == 1 && observer.Calls == 1); |
| 58 | Check(budget.Processed == fixture.Bytes.Length + 1); |
| 59 | Check(budget.Output == (selected && !bodyBudget && !bodyCancel ? 1 : 0)); |
| 60 | if (throwOnDispose) |
| 61 | { |
| 62 | Check(ReferenceEquals(error, marker) && beforeCommitCalls == 0); |
| 63 | Uncommitted(pending); |
| 64 | } |
| 65 | else if (scenario == "success") |
| 66 | { |
| 67 | Check(error == null && beforeCommitCalls == 1 && pending.CommitCount == 1); |
| 68 | Check(pending.CommittedBoundary != null); |
| 69 | Check(selected ? pending.CommittedHash != null : pending.CommittedHash == null); |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | Check(beforeCommitCalls == (scenario == "dispose-both" ? 1 : 0)); |
| 74 | Check(bodyCancel || scenario == "dispose-both" |
| 75 | ? error is OperationCanceledException |
| 76 | : error is ProbeException { Failure: Failure.Budget }); |
| 77 | Uncommitted(pending); |
| 78 | } |
| 79 | Check(Capture(() => pending.Execute(fixture.Bytes, budget, selected)) is InvalidOperationException); |
| 80 | Check(disposeCalls == 1); |
| 81 | } |
| 82 | |
| 83 | private static void VerifyCounter(bool selected, long delta, int mode) |
| 84 | { |
| 85 | var fixture = Fixture.Wrap([0x01, 0x01, 0x00, 0xfe, 0xff, 0x41], [0x41], 0); |
| 86 | using var cts = new CancellationTokenSource(); |
| 87 | var clock = new ManualClock(); |
| 88 | var budget = new ProbeBudget(new(Duration: TimeSpan.FromSeconds(1)), clock); |
| 89 | var observer = new CounterDeltaInflater(delta, () => |
| 90 | { |
| 91 | if ((mode & 1) != 0) { cts.Cancel(); } |
| 92 | if ((mode & 2) != 0) { clock.Advance(TimeSpan.FromSeconds(1)); } |
| 93 | }); |
| 94 | var pending = new PendingValidation(); |
| 95 | var disposeCalls = 0; |
| 96 | var beforeCommitCalls = 0; |
| 97 | var error = Capture(() => pending.Execute(fixture.Bytes, budget, selected, cts.Token, |
| 98 | beforeCommit: () => beforeCommitCalls++, inflater: observer, |
| 99 | onSinkDispose: () => disposeCalls++)); |
| 100 | Check(error is ProbeException { Failure: Failure.Boundary }); |
| 101 | Check(observer.Calls == 1 && observer.TotalOut == delta && observer.ActualTotalOut == 1); |
| 102 | Check(budget.Processed == fixture.Bytes.Length && budget.Output == 0); |
| 103 | Check(disposeCalls == 1 && beforeCommitCalls == 0); |
| 104 | Uncommitted(pending); |
| 105 | var overlapping = Capture(() => budget.Check(cts.Token)); |
| 106 | Check((mode & 1) != 0 ? overlapping is OperationCanceledException |
| 107 | : (mode & 2) != 0 ? overlapping is ProbeException { Failure: Failure.Budget } |
| 108 | : overlapping == null); |
| 109 | } |
| 110 | |
| 111 | private static Exception? Capture(Action action) |
| 112 | { |
| 113 | try { action(); } |
| 114 | catch (Exception error) { return error; } |
| 115 | return null; |
| 116 | } |
| 117 | |
| 118 | private static void Uncommitted(PendingValidation pending) => |
| 119 | Check(pending.CommitCount == 0 && pending.CommittedBoundary == null && pending.CommittedHash == null); |
| 120 | |
| 121 | private static void Check(bool condition) |
| 122 | { |
| 123 | if (!condition) { throw new Exception("확정 실패 경계 회귀 실패"); } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // 실제 해제 결과는 유지하고 반환 후 관측 카운터만 변조함. |
| 128 | internal sealed class CounterDeltaInflater(long delta, Action afterReturn) : IRawInflater |
| 129 | { |
| 130 | private readonly SharpInflater inner = new(); |
| 131 | public int Calls { get; private set; } |
| 132 | public long ActualTotalOut => inner.TotalOut; |
| 133 | public bool IsFinished => inner.IsFinished; |
| 134 | public bool IsNeedingInput => inner.IsNeedingInput; |
| 135 | public bool IsNeedingDictionary => inner.IsNeedingDictionary; |
| 136 | public long TotalIn => inner.TotalIn; |
| 137 | public long TotalOut => Calls == 0 ? inner.TotalOut : delta; |
| 138 | public int RemainingInput => inner.RemainingInput; |
| 139 | public void SetInput(byte[] bytes, int count) => inner.SetInput(bytes, count); |
| 140 | |
| 141 | public int Inflate(byte[] output) |
| 142 | { |
| 143 | var written = inner.Inflate(output); |
| 144 | Calls++; |
| 145 | afterReturn(); |
| 146 | return written; |
| 147 | } |
| 148 | } |