@@ -114,24 +114,32 @@ public async Task LargeUpload(long? maxRequestBufferSize, bool sendContentLength
114
114
115
115
if ( expectPause )
116
116
{
117
- // Block until the send task has gone a while without writing bytes, which likely means
118
- // the server input buffer is full.
119
- while ( ( DateTime . Now - lastBytesWritten ) < bytesWrittenTimeout )
120
- {
121
- await Task . Delay ( bytesWrittenPollingInterval ) ;
122
- }
123
-
124
- // Verify the number of bytes written before the client was paused.
125
- //
126
117
// The minimum is (maxRequestBufferSize - maxSendSize + 1), since if bytesWritten is
127
118
// (maxRequestBufferSize - maxSendSize) or smaller, the client should be able to
128
119
// complete another send.
129
- //
120
+ var minimumExpectedBytesWritten = maxRequestBufferSize . Value - maxSendSize + 1 ;
121
+
130
122
// The maximum is harder to determine, since there can be OS-level buffers in both the client
131
123
// and server, which allow the client to send more than maxRequestBufferSize before getting
132
124
// paused. We assume the combined buffers are smaller than the difference between
133
125
// data.Length and maxRequestBufferSize.
134
- Assert . InRange ( bytesWritten , maxRequestBufferSize . Value - maxSendSize + 1 , data . Length - 1 ) ;
126
+ var maximumExpectedBytesWritten = data . Length - 1 ;
127
+
128
+ // Block until the send task has gone a while without writing bytes AND
129
+ // the bytes written exceeds the minimum expected. This indicates the server buffer
130
+ // is full.
131
+ //
132
+ // If the send task is paused before the expected number of bytes have been
133
+ // written, keep waiting since the pause may have been caused by something else
134
+ // like a slow machine.
135
+ while ( ( DateTime . Now - lastBytesWritten ) < bytesWrittenTimeout ||
136
+ bytesWritten < minimumExpectedBytesWritten )
137
+ {
138
+ await Task . Delay ( bytesWrittenPollingInterval ) ;
139
+ }
140
+
141
+ // Verify the number of bytes written before the client was paused.
142
+ Assert . InRange ( bytesWritten , minimumExpectedBytesWritten , maximumExpectedBytesWritten ) ;
135
143
136
144
// Tell server to start reading request body
137
145
startReadingRequestBody . Set ( ) ;
0 commit comments