-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
TestSettings.cs
52 lines (39 loc) · 1.77 KB
/
TestSettings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// 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.Sockets;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace System.Net.NameResolution.Tests
{
internal static class TestSettings
{
// A hostname that will not exist in any DNS caches, forcing some I/O to lookup.
public static string UncachedHost => $"nonexistent-{Guid.NewGuid():N}.contoso.com";
public const string IPv4Host = LocalHost;
// Linux CI servers not configured with IPv6 localhost.
// Windows CI servers not configured with IPv6 name resolution.
public static readonly string IPv6Host = PlatformDetection.IsWindows ? LocalHost : "ipv6.google.com";
public const string LocalHost = "localhost";
public const string LocalIPString = "127.0.0.1";
// Timeout values in milliseconds.
public const int PassingTestTimeout = 30_000;
public static Task<IPAddress> GetLocalIPAddress() =>
ResolveHost(TestSettings.LocalHost, TestSettings.AddressFamily);
public static AddressFamily AddressFamily =>
AddressFamily.InterNetwork;
public static Task WhenAllOrAnyFailedWithTimeout(params Task[] tasks) => tasks.WhenAllOrAnyFailed(PassingTestTimeout);
private static async Task<IPAddress> ResolveHost(string host, AddressFamily family)
{
IPHostEntry hostEntry = await Dns.GetHostEntryAsync(host);
foreach (IPAddress address in hostEntry.AddressList)
{
if (address.AddressFamily == family)
{
return address;
}
}
return null;
}
}
}