@@ -1048,27 +1048,52 @@ await connection.ReceiveEnd(
1048
1048
}
1049
1049
1050
1050
[ Fact ]
1051
- [ QuarantinedTest ( "https://github.com/dotnet/aspnetcore/issues/23628" ) ]
1052
1051
public async Task ContentLengthReadAsyncSingleBytesAtATime ( )
1053
1052
{
1054
1053
var testContext = new TestServiceContext ( LoggerFactory ) ;
1055
- var tcs = new TaskCompletionSource ( ) ;
1056
- var tcs2 = new TaskCompletionSource ( ) ;
1054
+ var tcs = new TaskCompletionSource ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
1055
+ var tcs2 = new TaskCompletionSource ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
1056
+
1057
+ static async Task < ReadResult > ReadAtLeastAsync ( PipeReader reader , int numBytes )
1058
+ {
1059
+ var result = await reader . ReadAsync ( ) ;
1060
+
1061
+ while ( ! result . IsCompleted && result . Buffer . Length < numBytes )
1062
+ {
1063
+ reader . AdvanceTo ( result . Buffer . Start , result . Buffer . End ) ;
1064
+ result = await reader . ReadAsync ( ) ;
1065
+ }
1066
+
1067
+ if ( result . Buffer . Length < numBytes )
1068
+ {
1069
+ throw new IOException ( "Unexpected end of content." ) ;
1070
+ }
1071
+
1072
+ return result ;
1073
+ }
1074
+
1057
1075
await using ( var server = new TestServer ( async httpContext =>
1058
1076
{
1059
- var readResult = await httpContext . Request . BodyReader . ReadAsync ( ) ;
1077
+ // Buffer 3 bytes.
1078
+ var readResult = await ReadAtLeastAsync ( httpContext . Request . BodyReader , numBytes : 3 ) ;
1060
1079
Assert . Equal ( 3 , readResult . Buffer . Length ) ;
1061
1080
tcs . SetResult ( ) ;
1062
1081
1063
1082
httpContext . Request . BodyReader . AdvanceTo ( readResult . Buffer . Start , readResult . Buffer . End ) ;
1064
1083
1084
+ // Buffer 1 more byte.
1065
1085
readResult = await httpContext . Request . BodyReader . ReadAsync ( ) ;
1066
1086
httpContext . Request . BodyReader . AdvanceTo ( readResult . Buffer . Start , readResult . Buffer . End ) ;
1067
1087
tcs2 . SetResult ( ) ;
1068
1088
1089
+ // Buffer 1 last byte.
1069
1090
readResult = await httpContext . Request . BodyReader . ReadAsync ( ) ;
1070
1091
Assert . Equal ( 5 , readResult . Buffer . Length ) ;
1071
1092
1093
+ // Do one more read to ensure completion is always observed.
1094
+ httpContext . Request . BodyReader . AdvanceTo ( readResult . Buffer . Start , readResult . Buffer . End ) ;
1095
+ readResult = await httpContext . Request . BodyReader . ReadAsync ( ) ;
1096
+ Assert . True ( readResult . IsCompleted ) ;
1072
1097
} , testContext ) )
1073
1098
{
1074
1099
using ( var connection = server . CreateConnection ( ) )
@@ -1766,7 +1791,7 @@ await connection.Send(
1766
1791
[ Fact ]
1767
1792
public async Task ContentLengthRequestCallCancelPendingReadWorks ( )
1768
1793
{
1769
- var tcs = new TaskCompletionSource ( ) ;
1794
+ var tcs = new TaskCompletionSource ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
1770
1795
var testContext = new TestServiceContext ( LoggerFactory ) ;
1771
1796
1772
1797
await using ( var server = new TestServer ( async httpContext =>
@@ -1863,7 +1888,7 @@ public async Task ContentLengthRequestCallCompleteDoesNotCauseException()
1863
1888
{
1864
1889
var testContext = new TestServiceContext ( LoggerFactory ) ;
1865
1890
1866
- var tcs = new TaskCompletionSource ( ) ;
1891
+ var tcs = new TaskCompletionSource ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
1867
1892
await using ( var server = new TestServer ( async httpContext =>
1868
1893
{
1869
1894
var request = httpContext . Request ;
0 commit comments