Skip to content
代码片段 群组 项目
未验证 提交 84b8f804 编辑于 作者: James Newton-King's avatar James Newton-King 提交者: GitHub
浏览文件

[release/7.0-rc1] HTTP/3 flaky test retries and logging (#43423)

* HTTP/3 flaky test retries and logging

* Rewrite retry helper to get ports via binding port to 0
上级 d2c7b659
No related branches found
No related tags found
无相关合并请求
......@@ -421,6 +421,8 @@ public class QuicConnectionContextTests : TestApplicationErrorLoggerLoggedTest
public async Task StreamPool_StreamAbortedOnClientAndServer_NotPooled()
{
// Arrange
using var httpEventSource = new HttpEventSourceListener(LoggerFactory);
await using var connectionListener = await QuicTestHelpers.CreateConnectionListenerFactory(LoggerFactory);
var options = QuicTestHelpers.CreateClientConnectionOptions(connectionListener.EndPoint);
......
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Net;
using System.Net.Sockets;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.Testing;
public static class ServerRetryHelper
{
private const int RetryCount = 10;
/// <summary>
/// Retry a func. Useful when a test needs an explicit port and you want to avoid port conflicts.
/// </summary>
public static async Task BindPortsWithRetry(Func<int, Task> retryFunc, ILogger logger)
{
var ports = GetFreePorts(RetryCount);
var retryCount = 0;
while (true)
{
// Approx dynamic port range on Windows and Linux.
var randomPort = Random.Shared.Next(35000, 60000);
try
{
await retryFunc(randomPort);
await retryFunc(ports[retryCount]);
break;
}
catch (Exception ex)
{
retryCount++;
if (retryCount >= 5)
if (retryCount >= RetryCount)
{
throw;
}
......@@ -38,4 +42,35 @@ public static class ServerRetryHelper
}
}
}
private static int[] GetFreePorts(int count)
{
var sockets = new List<Socket>();
for (var i = 0; i < count; i++)
{
// Find a port that's free by binding port 0.
// Note that this port should be free when the test runs, but:
// - Something else could steal it before the test uses it.
// - UDP port with the same number could be in use.
// For that reason, some retries should be available.
var ipEndPoint = new IPEndPoint(IPAddress.Loopback, 0);
var listenSocket = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
listenSocket.Bind(ipEndPoint);
sockets.Add(listenSocket);
}
// Ports are calculated upfront. Rebinding with port 0 could result the same port
// being returned for each retry.
var ports = sockets.Select(s => (IPEndPoint)s.LocalEndPoint).Select(ep => ep.Port).ToArray();
foreach (var socket in sockets)
{
socket.Dispose();
}
return ports;
}
}
......@@ -228,6 +228,8 @@ public class Http3RequestTests : LoggedTest
public async Task POST_ClientSendsOnlyHeaders_RequestReceivedOnServer(HttpProtocols protocol)
{
// Arrange
using var httpEventSource = new HttpEventSourceListener(LoggerFactory);
var builder = CreateHostBuilder(context =>
{
return Task.CompletedTask;
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册