Skip to content

Commit

Permalink
missing WaitAsync cancellationToken (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Sep 26, 2024
1 parent 97565a8 commit 98f1554
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 25 deletions.
27 changes: 21 additions & 6 deletions contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ static partial class Polyfill
return Task.FromCanceled(cancellationToken);
}

return target.FlushAsync();
return target.FlushAsync()
.WaitAsync(cancellationToken);
}

#endif
Expand Down Expand Up @@ -283,7 +284,8 @@ static partial class Polyfill
await target.WriteAsync(chunk, cancel).ConfigureAwait(false);
}
#else
await target.WriteAsync(builder.ToString());
await target.WriteAsync(builder.ToString())
.WaitAsync(cancellationToken);
#endif
}
}
Expand Down Expand Up @@ -315,7 +317,9 @@ static partial class Polyfill
segment = new(buffer.ToArray());
}

return new(target.WriteAsync(segment.Array!, segment.Offset, segment.Count));
var task = target.WriteAsync(segment.Array!, segment.Offset, segment.Count)
.WaitAsync(cancellationToken);
return new(task);
}

/// <summary>
Expand All @@ -341,7 +345,9 @@ static partial class Polyfill
segment = new(buffer.ToArray());
}

return new(target.WriteLineAsync(segment.Array!, segment.Offset, segment.Count));
var task = target.WriteLineAsync(segment.Array!, segment.Offset, segment.Count)
.WaitAsync(cancellationToken);
return new(task);
}

#endif
Expand Down Expand Up @@ -394,7 +400,7 @@ static partial class Polyfill
#endif
}
```
<sup><a href='/src/Polyfill/Polyfill_TextWriter.cs#L1-L207' title='Snippet source file'>snippet source</a> | <a href='#snippet-Polyfill_TextWriter.cs' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Polyfill/Polyfill_TextWriter.cs#L1-L213' title='Snippet source file'>snippet source</a> | <a href='#snippet-Polyfill_TextWriter.cs' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down Expand Up @@ -429,9 +435,18 @@ partial class PolyfillTests
var read = await reader.ReadToEndAsync(Cancel.None);
Assert.AreEqual("value", read);
}

[Test]
public async Task StreamReaderReadLineAsync()
{
using var stream = new MemoryStream("line1\nline2"u8.ToArray());
using var reader = new StreamReader(stream);
var read = await reader.ReadLineAsync(CancellationToken.None);
Assert.AreEqual("line1", read);
}
}
```
<sup><a href='/src/Tests/PolyfillTests_StreamReader.cs#L1-L23' title='Snippet source file'>snippet source</a> | <a href='#snippet-PolyfillTests_StreamReader.cs' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/PolyfillTests_StreamReader.cs#L1-L32' title='Snippet source file'>snippet source</a> | <a href='#snippet-PolyfillTests_StreamReader.cs' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<Version>6.9.0</Version>
<Version>6.10.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>Polyfill</PackageTags>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
Expand Down
16 changes: 10 additions & 6 deletions src/Polyfill/Polyfill_HttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@ public static async Task<byte[]> GetByteArrayAsync(
using var response = await target.GetAsync(
requestUri,
HttpCompletionOption.ResponseHeadersRead,
cancellationToken
).ConfigureAwait(false);
cancellationToken)
.ConfigureAwait(false);

response.EnsureSuccessStatusCode();

return await response.Content.ReadAsByteArrayAsync(cancellationToken).ConfigureAwait(false);
return await response.Content
.ReadAsByteArrayAsync(cancellationToken)
.ConfigureAwait(false);
}
// Older versions of HttpClient methods don't propagate the cancellation token inside the exception
catch (OperationCanceledException exception) when (
Expand Down Expand Up @@ -143,12 +145,14 @@ public static async Task<string> GetStringAsync(
using var response = await target.GetAsync(
requestUri,
HttpCompletionOption.ResponseHeadersRead,
cancellationToken
).ConfigureAwait(false);
cancellationToken)
.ConfigureAwait(false);

response.EnsureSuccessStatusCode();

return await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
return await response.Content
.ReadAsStringAsync(cancellationToken)
.ConfigureAwait(false);
}
// Older versions of HttpClient methods don't propagate the cancellation token inside the exception
catch (OperationCanceledException exception) when (
Expand Down
9 changes: 6 additions & 3 deletions src/Polyfill/Polyfill_HttpContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public static Task<Stream> ReadAsStreamAsync(
CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return target.ReadAsStreamAsync();
return target.ReadAsStreamAsync()
.WaitAsync(cancellationToken);
}

/// <summary>
Expand All @@ -49,7 +50,8 @@ public static Task<byte[]> ReadAsByteArrayAsync(
CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return target.ReadAsByteArrayAsync();
return target.ReadAsByteArrayAsync()
.WaitAsync(cancellationToken);
}

/// <summary>
Expand All @@ -69,7 +71,8 @@ public static Task<string> ReadAsStringAsync(
CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return target.ReadAsStringAsync();
return target.ReadAsStringAsync()
.WaitAsync(cancellationToken);
}
}
#endif
6 changes: 4 additions & 2 deletions src/Polyfill/Polyfill_Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public static ValueTask<int> ReadAsync(
segment = new(buffer.ToArray());
}

return new(target.ReadAsync(segment.Array!, segment.Offset, segment.Count, cancellationToken));
var task = target.ReadAsync(segment.Array!, segment.Offset, segment.Count, cancellationToken);
return new(task);
}

/// <summary>
Expand All @@ -63,7 +64,8 @@ public static ValueTask WriteAsync(
segment = new(buffer.ToArray());
}

return new(target.WriteAsync(segment.Array!, segment.Offset, segment.Count, cancellationToken));
var task = target.WriteAsync(segment.Array!, segment.Offset, segment.Count, cancellationToken);
return new(task);
}

#endif
Expand Down
10 changes: 7 additions & 3 deletions src/Polyfill/Polyfill_TextReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public static ValueTask<int> ReadAsync(
segment = new(buffer.ToArray());
}

return new(target.ReadAsync(segment.Array!, segment.Offset, segment.Count));
var task = target.ReadAsync(segment.Array!, segment.Offset, segment.Count)
.WaitAsync(cancellationToken);
return new(task);
}
#endif

Expand All @@ -62,7 +64,8 @@ public static Task<string> ReadToEndAsync(
{
cancellationToken.ThrowIfCancellationRequested();

return target.ReadToEndAsync();
return target.ReadToEndAsync()
.WaitAsync(cancellationToken);
}

/// <summary>
Expand All @@ -81,7 +84,8 @@ public static Task<string> ReadLineAsync(
{
cancellationToken.ThrowIfCancellationRequested();

return target.ReadLineAsync();
return target.ReadLineAsync()
.WaitAsync(cancellationToken);
}
#endif
}
14 changes: 10 additions & 4 deletions src/Polyfill/Polyfill_TextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public static Task FlushAsync(this TextWriter target, CancellationToken cancella
return Task.FromCanceled(cancellationToken);
}

return target.FlushAsync();
return target.FlushAsync()
.WaitAsync(cancellationToken);
}

#endif
Expand Down Expand Up @@ -95,7 +96,8 @@ async Task WriteAsyncCore(StringBuilder builder, CancellationToken cancel)
await target.WriteAsync(chunk, cancel).ConfigureAwait(false);
}
#else
await target.WriteAsync(builder.ToString());
await target.WriteAsync(builder.ToString())
.WaitAsync(cancellationToken);
#endif
}
}
Expand Down Expand Up @@ -127,7 +129,9 @@ public static ValueTask WriteAsync(
segment = new(buffer.ToArray());
}

return new(target.WriteAsync(segment.Array!, segment.Offset, segment.Count));
var task = target.WriteAsync(segment.Array!, segment.Offset, segment.Count)
.WaitAsync(cancellationToken);
return new(task);
}

/// <summary>
Expand All @@ -153,7 +157,9 @@ public static ValueTask WriteLineAsync(
segment = new(buffer.ToArray());
}

return new(target.WriteLineAsync(segment.Array!, segment.Offset, segment.Count));
var task = target.WriteLineAsync(segment.Array!, segment.Offset, segment.Count)
.WaitAsync(cancellationToken);
return new(task);
}

#endif
Expand Down
9 changes: 9 additions & 0 deletions src/Tests/PolyfillTests_StreamReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ public async Task StreamReaderReadToEndAsync()
var read = await reader.ReadToEndAsync(Cancel.None);
Assert.AreEqual("value", read);
}

[Test]
public async Task StreamReaderReadLineAsync()
{
using var stream = new MemoryStream("line1\nline2"u8.ToArray());
using var reader = new StreamReader(stream);
var read = await reader.ReadLineAsync(CancellationToken.None);
Assert.AreEqual("line1", read);
}
}

0 comments on commit 98f1554

Please sign in to comment.