Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional Validation for PauseThresholdWriter and ResumeThresholdWriter #77359

Merged
merged 6 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ private void AdvanceReader(BufferSegment? consumedSegment, int consumedIndex, Bu
_lastExaminedIndex = examinedSegment.RunningIndex + examinedIndex;

Debug.Assert(_unconsumedBytes >= 0, "Length has gone negative");
Debug.Assert(ResumeWriterThreshold >= 1, "ResumeWriterThreshold is less than 1");

if (oldLength >= ResumeWriterThreshold &&
_unconsumedBytes < ResumeWriterThreshold)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ public PipeOptions(
{
resumeWriterThreshold = DefaultResumeWriterThreshold;
}
else if (resumeWriterThreshold < 0 || resumeWriterThreshold > pauseWriterThreshold)
else if (resumeWriterThreshold == 0)
{
// A resumeWriterThreshold of 0 makes no sense because the writer could never resume if paused.
// By setting it to 1, the writer will resume only after all data is consumed.
resumeWriterThreshold = 1;
}

// Only validate that the resumeWriterThreshold is not too large if the writer could actually pause.
if (resumeWriterThreshold < 0 || (pauseWriterThreshold > 0 && resumeWriterThreshold > pauseWriterThreshold))
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.resumeWriterThreshold);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override void Schedule(Action<object> action, object state)
public void CompletingReaderFromWriterCallbackWorks()
{
var callbackRan = false;
var pipe = new Pipe(new PipeOptions(_pool, pauseWriterThreshold: 5, readerScheduler: PipeScheduler.Inline, writerScheduler: PipeScheduler.Inline, useSynchronizationContext: false));
var pipe = new Pipe(new PipeOptions(_pool, pauseWriterThreshold: 5, resumeWriterThreshold: 5, readerScheduler: PipeScheduler.Inline, writerScheduler: PipeScheduler.Inline, useSynchronizationContext: false));

pipe.Writer.OnReaderCompleted((exception, state) => { pipe.Writer.Complete(); }, null);

Expand All @@ -61,7 +61,7 @@ public void CompletingReaderFromWriterCallbackWorks()
public void CompletingWriterFromReaderCallbackWorks()
{
var callbackRan = false;
var pipe = new Pipe(new PipeOptions(_pool, pauseWriterThreshold: 5, readerScheduler: PipeScheduler.Inline, writerScheduler: PipeScheduler.Inline, useSynchronizationContext: false));
var pipe = new Pipe(new PipeOptions(_pool, pauseWriterThreshold: 5, resumeWriterThreshold: 5, readerScheduler: PipeScheduler.Inline, writerScheduler: PipeScheduler.Inline, useSynchronizationContext: false));

pipe.Reader.OnWriterCompleted((exception, state) => { pipe.Reader.Complete(); }, null);

Expand Down Expand Up @@ -201,7 +201,7 @@ public void OnReaderCompletedRanBeforeFlushContinuation()
{
var callbackRan = false;
var continuationRan = false;
var pipe = new Pipe(new PipeOptions(_pool, pauseWriterThreshold: 5, readerScheduler: PipeScheduler.Inline, writerScheduler: PipeScheduler.Inline, useSynchronizationContext: false));
var pipe = new Pipe(new PipeOptions(_pool, pauseWriterThreshold: 5, resumeWriterThreshold: 5, readerScheduler: PipeScheduler.Inline, writerScheduler: PipeScheduler.Inline, useSynchronizationContext: false));

pipe.Writer.OnReaderCompleted(
(exception, state) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void InvalidArgs_Throws()
AssertExtensions.Throws<ArgumentOutOfRangeException>("pauseWriterThreshold", () => new PipeOptions(pauseWriterThreshold: -2));
AssertExtensions.Throws<ArgumentOutOfRangeException>("resumeWriterThreshold", () => new PipeOptions(resumeWriterThreshold: -2));
AssertExtensions.Throws<ArgumentOutOfRangeException>("resumeWriterThreshold", () => new PipeOptions(pauseWriterThreshold: 50, resumeWriterThreshold: 100));
AssertExtensions.Throws<ArgumentOutOfRangeException>("resumeWriterThreshold", () => new PipeOptions(pauseWriterThreshold: 1, resumeWriterThreshold: -1));
}

[Theory]
Expand Down