Skip to content

Commit e717472

Browse files
authored
Fix nullable warnings in HttpStress (#35862)
1 parent ee2355c commit e717472

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

src/libraries/System.Net.Http/tests/StressTests/HttpStress/ClientOperations.cs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public sealed class RequestContext
2929

3030
public RequestContext(Configuration config, HttpClient httpClient, Random random, CancellationToken globalToken, int taskNum)
3131
{
32+
Debug.Assert(httpClient.BaseAddress != null);
33+
3234
_random = random;
3335
_client = httpClient;
3436
_globalToken = globalToken;
@@ -47,7 +49,7 @@ public RequestContext(Configuration config, HttpClient httpClient, Random random
4749
public int MaxRequestHeaderCount => _config.MaxRequestHeaderCount;
4850
public int MaxRequestHeaderTotalSize => _config.MaxRequestHeaderTotalSize;
4951
public int MaxContentLength => _config.MaxContentLength;
50-
public Uri BaseAddress => _client.BaseAddress;
52+
public Uri BaseAddress => _client.BaseAddress!;
5153

5254
// HttpClient.SendAsync() wrapper that wires randomized cancellation
5355
public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, HttpCompletionOption httpCompletion = HttpCompletionOption.ResponseContentRead, CancellationToken? token = null)
@@ -190,7 +192,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
190192
using HttpResponseMessage m = await ctx.SendAsync(req);
191193

192194
ValidateStatusCode(m);
193-
ValidateServerContent(await m.Content.ReadAsStringAsync(), expectedLength);
195+
ValidateServerContent(await m.Content!.ReadAsStringAsync(), expectedLength);
194196
}),
195197

196198
("GET Partial",
@@ -202,7 +204,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
202204

203205
ValidateStatusCode(m);
204206

205-
using (Stream s = await m.Content.ReadAsStreamAsync())
207+
using (Stream s = await m.Content!.ReadAsStreamAsync())
206208
{
207209
s.ReadByte(); // read single byte from response and throw the rest away
208210
}
@@ -219,22 +221,21 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
219221

220222
ValidateStatusCode(res);
221223

222-
await res.Content.ReadAsStringAsync();
224+
await res.Content!.ReadAsStringAsync();
223225

224226
bool isValidChecksum = ValidateServerChecksum(res.Headers, expectedChecksum);
225-
string GetFailureDetails() => isValidChecksum ? "server checksum matches client checksum" : "server checksum mismatch";
227+
string failureDetails = isValidChecksum ? "server checksum matches client checksum" : "server checksum mismatch";
226228

227229
// Validate that request headers are being echoed
228230
foreach (KeyValuePair<string, IEnumerable<string>> reqHeader in req.Headers)
229231
{
230-
if (!res.Headers.TryGetValues(reqHeader.Key, out IEnumerable<string> values))
232+
if (!res.Headers.TryGetValues(reqHeader.Key, out IEnumerable<string>? values))
231233
{
232-
throw new Exception($"Expected response header name {reqHeader.Key} missing. {GetFailureDetails()}");
234+
throw new Exception($"Expected response header name {reqHeader.Key} missing. {failureDetails}");
233235
}
234236
else if (!reqHeader.Value.SequenceEqual(values))
235237
{
236-
string FmtValues(IEnumerable<string> values) => string.Join(", ", values.Select(x => $"\"{x}\""));
237-
throw new Exception($"Unexpected values for header {reqHeader.Key}. Expected {FmtValues(reqHeader.Value)} but got {FmtValues(values)}. {GetFailureDetails()}");
238+
throw new Exception($"Unexpected values for header {reqHeader.Key}. Expected {FmtValues(reqHeader.Value)} but got {FmtValues(values)}. {failureDetails}");
238239
}
239240
}
240241

@@ -243,14 +244,13 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
243244
{
244245
foreach (KeyValuePair<string, IEnumerable<string>> reqHeader in req.Headers)
245246
{
246-
if (!res.TrailingHeaders.TryGetValues(reqHeader.Key + "-trailer", out IEnumerable<string> values))
247+
if (!res.TrailingHeaders.TryGetValues(reqHeader.Key + "-trailer", out IEnumerable<string>? values))
247248
{
248-
throw new Exception($"Expected trailing header name {reqHeader.Key}-trailer missing. {GetFailureDetails()}");
249+
throw new Exception($"Expected trailing header name {reqHeader.Key}-trailer missing. {failureDetails}");
249250
}
250251
else if (!reqHeader.Value.SequenceEqual(values))
251252
{
252-
string FmtValues(IEnumerable<string> values) => string.Join(", ", values.Select(x => $"\"{x}\""));
253-
throw new Exception($"Unexpected values for trailing header {reqHeader.Key}-trailer. Expected {FmtValues(reqHeader.Value)} but got {FmtValues(values)}. {GetFailureDetails()}");
253+
throw new Exception($"Unexpected values for trailing header {reqHeader.Key}-trailer. Expected {FmtValues(reqHeader.Value)} but got {FmtValues(values)}. {failureDetails}");
254254
}
255255
}
256256
}
@@ -260,6 +260,8 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
260260
// Should not reach this block unless there's a bug in checksum validation logic. Do throw now
261261
throw new Exception("server checksum mismatch");
262262
}
263+
264+
static string FmtValues(IEnumerable<string> values) => string.Join(", ", values.Select(x => $"\"{x}\""));
263265
}),
264266

265267
("GET Parameters",
@@ -271,7 +273,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
271273
using HttpResponseMessage m = await ctx.SendAsync(req);
272274

273275
ValidateStatusCode(m);
274-
ValidateContent(expectedResponse, await m.Content.ReadAsStringAsync(), $"Uri: {uri}");
276+
ValidateContent(expectedResponse, await m.Content!.ReadAsStringAsync(), $"Uri: {uri}");
275277
}),
276278

277279
("GET Aborted",
@@ -330,7 +332,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
330332

331333
ValidateStatusCode(m);
332334
string checksumMessage = ValidateServerChecksum(m.Headers, checksum) ? "server checksum matches client checksum" : "server checksum mismatch";
333-
ValidateContent(content, await m.Content.ReadAsStringAsync(), checksumMessage);
335+
ValidateContent(content, await m.Content!.ReadAsStringAsync(), checksumMessage);
334336
}),
335337

336338
("POST Multipart Data",
@@ -344,7 +346,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
344346

345347
ValidateStatusCode(m);
346348
string checksumMessage = ValidateServerChecksum(m.Headers, checksum) ? "server checksum matches client checksum" : "server checksum mismatch";
347-
ValidateContent(formData.expected, await m.Content.ReadAsStringAsync(), checksumMessage);
349+
ValidateContent(formData.expected, await m.Content!.ReadAsStringAsync(), checksumMessage);
348350
}),
349351

350352
("POST Duplex",
@@ -357,7 +359,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
357359
using HttpResponseMessage m = await ctx.SendAsync(req, HttpCompletionOption.ResponseHeadersRead);
358360

359361
ValidateStatusCode(m);
360-
string response = await m.Content.ReadAsStringAsync();
362+
string response = await m.Content!.ReadAsStringAsync();
361363

362364
string checksumMessage = ValidateServerChecksum(m.TrailingHeaders, checksum, required: false) ? "server checksum matches client checksum" : "server checksum mismatch";
363365
ValidateContent(content, await m.Content.ReadAsStringAsync(), checksumMessage);
@@ -374,7 +376,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
374376
using HttpResponseMessage m = await ctx.SendAsync(req, HttpCompletionOption.ResponseHeadersRead);
375377

376378
ValidateStatusCode(m);
377-
string response = await m.Content.ReadAsStringAsync();
379+
string response = await m.Content!.ReadAsStringAsync();
378380

379381
// trailing headers not supported for all servers, so do not require checksums
380382
bool isValidChecksum = ValidateServerChecksum(m.TrailingHeaders, checksum, required: false);
@@ -414,7 +416,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
414416

415417
ValidateStatusCode(m);
416418
string checksumMessage = ValidateServerChecksum(m.Headers, checksum) ? "server checksum matches client checksum" : "server checksum mismatch";
417-
ValidateContent(content, await m.Content.ReadAsStringAsync(), checksumMessage);
419+
ValidateContent(content, await m.Content!.ReadAsStringAsync(), checksumMessage);
418420
}),
419421

420422
("HEAD",
@@ -426,7 +428,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
426428

427429
ValidateStatusCode(m);
428430

429-
if (m.Content.Headers.ContentLength != expectedLength)
431+
if (m.Content!.Headers.ContentLength != expectedLength)
430432
{
431433
throw new Exception($"Expected {expectedLength}, got {m.Content.Headers.ContentLength}");
432434
}
@@ -444,7 +446,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
444446

445447
ValidateStatusCode(m);
446448

447-
string r = await m.Content.ReadAsStringAsync();
449+
string r = await m.Content!.ReadAsStringAsync();
448450
if (r != "") throw new Exception($"Got unexpected response: {r}");
449451
}),
450452

@@ -458,7 +460,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
458460

459461
ValidateStatusCode(m);
460462

461-
string r = await m.Content.ReadAsStringAsync();
463+
string r = await m.Content!.ReadAsStringAsync();
462464
if (r != "") throw new Exception($"Got unexpected response: {r}");
463465
}),
464466

@@ -470,7 +472,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
470472
using HttpResponseMessage m = await ctx.SendAsync(req);
471473

472474
ValidateStatusCode(m);
473-
ValidateServerContent(await m.Content.ReadAsStringAsync(), expectedLength);
475+
ValidateServerContent(await m.Content!.ReadAsStringAsync(), expectedLength);
474476
}),
475477
};
476478

@@ -573,7 +575,7 @@ private static (string, MultipartContent) GetMultipartContent(RequestContext cli
573575

574576
private static bool ValidateServerChecksum(HttpResponseHeaders headers, ulong expectedChecksum, bool required = true)
575577
{
576-
if (headers.TryGetValues("crc32", out IEnumerable<string> values) &&
578+
if (headers.TryGetValues("crc32", out IEnumerable<string>? values) &&
577579
uint.TryParse(values.First(), out uint serverChecksum))
578580
{
579581
return serverChecksum == expectedChecksum;
@@ -595,7 +597,7 @@ private sealed class StringDuplexContent : HttpContent
595597

596598
public StringDuplexContent(string value) => _data = Encoding.UTF8.GetBytes(value);
597599

598-
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context) =>
600+
protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context) =>
599601
stream.WriteAsync(_data, 0, _data.Length);
600602

601603
protected override bool TryComputeLength(out long length)
@@ -612,7 +614,7 @@ private sealed class ByteAtATimeNoLengthContent : HttpContent
612614

613615
public ByteAtATimeNoLengthContent(byte[] buffer) => _buffer = buffer;
614616

615-
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
617+
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext? context)
616618
{
617619
for (int i = 0; i < _buffer.Length; i++)
618620
{

src/libraries/System.Net.Http/tests/StressTests/HttpStress/StressClient.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Collections.Generic;
88
using System.Collections.Concurrent;
99
using System.Diagnostics;
10+
using System.Diagnostics.CodeAnalysis;
1011
using System.Linq;
1112
using System.Net.Http;
1213
using System.Net.Security;
@@ -452,7 +453,7 @@ public void PrintFailureTypes()
452453

453454
private class StructuralEqualityComparer<T> : IEqualityComparer<T> where T : IStructuralEquatable
454455
{
455-
public bool Equals(T left, T right) => left.Equals(right, StructuralComparisons.StructuralEqualityComparer);
456+
public bool Equals([AllowNull] T left, [AllowNull] T right) => left != null && left.Equals(right, StructuralComparisons.StructuralEqualityComparer);
456457
public int GetHashCode(T value) => value.GetHashCode(StructuralComparisons.StructuralEqualityComparer);
457458
}
458459
}

0 commit comments

Comments
 (0)