Skip to content

Commit cb01ee0

Browse files
committed
test: Use Parallel.ForEachAsync
1 parent 0a8373f commit cb01ee0

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

src/OpenApi/src/Services/OpenApiDocumentService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Collections.Concurrent;
45
using System.Collections.Frozen;
56
using System.ComponentModel;
67
using System.ComponentModel.DataAnnotations;
@@ -46,7 +47,7 @@ internal sealed class OpenApiDocumentService(
4647
/// are unique within the lifetime of an application and serve as helpful associators between
4748
/// operations, API descriptions, and their respective transformer contexts.
4849
/// </summary>
49-
private readonly Dictionary<string, OpenApiOperationTransformerContext> _operationTransformerContextCache = new();
50+
private readonly ConcurrentDictionary<string, OpenApiOperationTransformerContext> _operationTransformerContextCache = new();
5051
private static readonly ApiResponseType _defaultApiResponseType = new() { StatusCode = StatusCodes.Status200OK };
5152

5253
private static readonly FrozenSet<string> _disallowedHeaderParameters = new[] { HeaderNames.Accept, HeaderNames.Authorization, HeaderNames.ContentType }.ToFrozenSet(StringComparer.OrdinalIgnoreCase);

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/OpenApiDocumentConcurrentRequestTests.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,14 @@ public async Task MapOpenApi_HandlesConcurrentRequests()
1313
{
1414
// Arrange
1515
var client = fixture.CreateClient();
16-
var requests = new List<Task<HttpResponseMessage>>
17-
{
18-
client.GetAsync("/openapi/v1.json"),
19-
client.GetAsync("/openapi/v1.json"),
20-
client.GetAsync("/openapi/v1.json")
21-
};
2216

2317
// Act
24-
var results = await Task.WhenAll(requests);
25-
26-
// Assert
27-
foreach (var result in results)
18+
await Parallel.ForAsync(0, 150, async (_, ctx) =>
2819
{
29-
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
30-
}
20+
var response = await client.GetAsync("/openapi/v1.json", ctx);
21+
22+
// Assert
23+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
24+
});
3125
}
3226
}

0 commit comments

Comments
 (0)