From bf5c25652256144f0eb6c1c9bbda520257f46e97 Mon Sep 17 00:00:00 2001 From: Kartheek Penagamuri Date: Thu, 4 Aug 2022 19:40:14 -0700 Subject: [PATCH 01/11] update maxrequestspersource to 64 by default for .NET framework code paths --- .../HttpSource/HttpHandlerResourceV3Provider.cs | 10 ++++++++++ .../HttpSource/HttpSourceResourceProvider.cs | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpHandlerResourceV3Provider.cs b/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpHandlerResourceV3Provider.cs index e1b3105c3f9..f1b5470ce82 100644 --- a/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpHandlerResourceV3Provider.cs +++ b/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpHandlerResourceV3Provider.cs @@ -6,8 +6,11 @@ using System.Linq; using System.Net; using System.Net.Http; +using System.Runtime.Serialization; +using System.Runtime.Serialization.Formatters; using System.Threading; using System.Threading.Tasks; +using NuGet.Common; using NuGet.Configuration; using NuGet.Protocol.Core.Types; @@ -48,6 +51,13 @@ private static HttpHandlerResourceV3 CreateResource(PackageSource packageSource) AutomaticDecompression = (DecompressionMethods.GZip | DecompressionMethods.Deflate) }; +#if IS_DESKTOP + if (RuntimeEnvironmentHelper.IsWindows) + { + clientHandler.MaxConnectionsPerServer = packageSource.MaxHttpRequestsPerSource; + } +#endif + // Setup http client handler client certificates if (packageSource.ClientCertificates != null) { diff --git a/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpSourceResourceProvider.cs b/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpSourceResourceProvider.cs index 506a8f86220..e02964d920a 100644 --- a/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpSourceResourceProvider.cs +++ b/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpSourceResourceProvider.cs @@ -6,6 +6,7 @@ using System.Diagnostics; using System.Threading; using System.Threading.Tasks; +using NuGet.Common; using NuGet.Configuration; using NuGet.Protocol.Core.Types; @@ -48,6 +49,14 @@ public override Task> TryCreate(SourceRepository sou throttle = SemaphoreSlimThrottle.CreateSemaphoreThrottle(source.PackageSource.MaxHttpRequestsPerSource); } +#if IS_DESKTOP + if (RuntimeEnvironmentHelper.IsWindows + && source.PackageSource.MaxHttpRequestsPerSource == 0) + { + source.PackageSource.MaxHttpRequestsPerSource = 64; + } +#endif + curResource = _cache.GetOrAdd( source.PackageSource, packageSource => new HttpSourceResource(HttpSource.Create(source, throttle))); From 73699da2aeebcd39447fbba4c201bd826c3b49c8 Mon Sep 17 00:00:00 2001 From: Kartheek Penagamuri Date: Thu, 4 Aug 2022 20:37:12 -0700 Subject: [PATCH 02/11] format --- .../NuGet.Protocol/HttpSource/HttpHandlerResourceV3Provider.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpHandlerResourceV3Provider.cs b/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpHandlerResourceV3Provider.cs index f1b5470ce82..de97fb4aeb5 100644 --- a/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpHandlerResourceV3Provider.cs +++ b/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpHandlerResourceV3Provider.cs @@ -6,8 +6,6 @@ using System.Linq; using System.Net; using System.Net.Http; -using System.Runtime.Serialization; -using System.Runtime.Serialization.Formatters; using System.Threading; using System.Threading.Tasks; using NuGet.Common; From abb7531b8b4a685c684da1b24d6287978a2953db Mon Sep 17 00:00:00 2001 From: Kartheek Penagamuri Date: Thu, 4 Aug 2022 20:42:04 -0700 Subject: [PATCH 03/11] change code --- .../NuGet.Protocol/HttpSource/HttpSourceResourceProvider.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpSourceResourceProvider.cs b/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpSourceResourceProvider.cs index e02964d920a..4f81bc146e9 100644 --- a/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpSourceResourceProvider.cs +++ b/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpSourceResourceProvider.cs @@ -48,10 +48,8 @@ public override Task> TryCreate(SourceRepository sou { throttle = SemaphoreSlimThrottle.CreateSemaphoreThrottle(source.PackageSource.MaxHttpRequestsPerSource); } - #if IS_DESKTOP - if (RuntimeEnvironmentHelper.IsWindows - && source.PackageSource.MaxHttpRequestsPerSource == 0) + else if (RuntimeEnvironmentHelper.IsWindows) { source.PackageSource.MaxHttpRequestsPerSource = 64; } From f659744ae08a9091951796dd70223af366d9049c Mon Sep 17 00:00:00 2001 From: Kartheek Penagamuri Date: Mon, 8 Aug 2022 12:59:21 -0700 Subject: [PATCH 04/11] fix bug --- .../NuGet.Protocol/HttpSource/HttpHandlerResourceV3Provider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpHandlerResourceV3Provider.cs b/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpHandlerResourceV3Provider.cs index de97fb4aeb5..c5001ab5a07 100644 --- a/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpHandlerResourceV3Provider.cs +++ b/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpHandlerResourceV3Provider.cs @@ -50,7 +50,7 @@ private static HttpHandlerResourceV3 CreateResource(PackageSource packageSource) }; #if IS_DESKTOP - if (RuntimeEnvironmentHelper.IsWindows) + if (RuntimeEnvironmentHelper.IsWindows && packageSource.MaxHttpRequestsPerSource > 0) { clientHandler.MaxConnectionsPerServer = packageSource.MaxHttpRequestsPerSource; } From 27870ca3537482c19556436e62d7741e8f1877ca Mon Sep 17 00:00:00 2001 From: Kartheek Penagamuri Date: Mon, 8 Aug 2022 13:45:43 -0700 Subject: [PATCH 05/11] fix fail tests --- .../NuGet.Protocol/HttpSource/HttpSourceResourceProvider.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpSourceResourceProvider.cs b/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpSourceResourceProvider.cs index 4f81bc146e9..5db2f654c90 100644 --- a/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpSourceResourceProvider.cs +++ b/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpSourceResourceProvider.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Concurrent; using System.Diagnostics; +using System.Net; using System.Threading; using System.Threading.Tasks; using NuGet.Common; @@ -49,7 +50,8 @@ public override Task> TryCreate(SourceRepository sou throttle = SemaphoreSlimThrottle.CreateSemaphoreThrottle(source.PackageSource.MaxHttpRequestsPerSource); } #if IS_DESKTOP - else if (RuntimeEnvironmentHelper.IsWindows) + else if (RuntimeEnvironmentHelper.IsWindows + && ServicePointManager.DefaultConnectionLimit == ServicePointManager.DefaultPersistentConnectionLimit) { source.PackageSource.MaxHttpRequestsPerSource = 64; } From e5ebf44c2ff42233c545e47e1befd2d94d75981c Mon Sep 17 00:00:00 2001 From: Kartheek Penagamuri Date: Mon, 8 Aug 2022 15:20:36 -0700 Subject: [PATCH 06/11] code refactoring --- .../HttpSource/HttpHandlerResourceV3Provider.cs | 3 +-- .../NuGet.Protocol/HttpSource/HttpSourceResourceProvider.cs | 4 +--- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpHandlerResourceV3Provider.cs b/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpHandlerResourceV3Provider.cs index c5001ab5a07..e425e515d2a 100644 --- a/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpHandlerResourceV3Provider.cs +++ b/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpHandlerResourceV3Provider.cs @@ -8,7 +8,6 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; -using NuGet.Common; using NuGet.Configuration; using NuGet.Protocol.Core.Types; @@ -50,7 +49,7 @@ private static HttpHandlerResourceV3 CreateResource(PackageSource packageSource) }; #if IS_DESKTOP - if (RuntimeEnvironmentHelper.IsWindows && packageSource.MaxHttpRequestsPerSource > 0) + if (packageSource.MaxHttpRequestsPerSource > 0) { clientHandler.MaxConnectionsPerServer = packageSource.MaxHttpRequestsPerSource; } diff --git a/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpSourceResourceProvider.cs b/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpSourceResourceProvider.cs index 5db2f654c90..42de366f1ac 100644 --- a/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpSourceResourceProvider.cs +++ b/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpSourceResourceProvider.cs @@ -7,7 +7,6 @@ using System.Net; using System.Threading; using System.Threading.Tasks; -using NuGet.Common; using NuGet.Configuration; using NuGet.Protocol.Core.Types; @@ -50,8 +49,7 @@ public override Task> TryCreate(SourceRepository sou throttle = SemaphoreSlimThrottle.CreateSemaphoreThrottle(source.PackageSource.MaxHttpRequestsPerSource); } #if IS_DESKTOP - else if (RuntimeEnvironmentHelper.IsWindows - && ServicePointManager.DefaultConnectionLimit == ServicePointManager.DefaultPersistentConnectionLimit) + else if (ServicePointManager.DefaultConnectionLimit == ServicePointManager.DefaultPersistentConnectionLimit) { source.PackageSource.MaxHttpRequestsPerSource = 64; } From ed3a703e6c4176e3b0480aeec5e0fff39a2e7e2e Mon Sep 17 00:00:00 2001 From: Kartheek Penagamuri Date: Sat, 3 Sep 2022 18:11:02 -0700 Subject: [PATCH 07/11] added tests --- .../HttpSource/HttpSourceResourceProvider.cs | 5 +- .../HttpHandlerResourceV3ProviderTests.cs | 74 +++++++++++++++++++ .../HttpSourceResourceProviderTests.cs | 48 ++++++++++++ 3 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpHandlerResourceV3ProviderTests.cs create mode 100644 test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpSourceResourceProviderTests.cs diff --git a/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpSourceResourceProvider.cs b/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpSourceResourceProvider.cs index 42de366f1ac..597a22f026d 100644 --- a/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpSourceResourceProvider.cs +++ b/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpSourceResourceProvider.cs @@ -14,6 +14,9 @@ namespace NuGet.Protocol { public class HttpSourceResourceProvider : ResourceProvider { +#if IS_DESKTOP + private const int DefaultMaxHttpRequestsPerSource = 64; +#endif // Only one HttpSource per source should exist. This is to reduce the number of TCP connections. private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); @@ -51,7 +54,7 @@ public override Task> TryCreate(SourceRepository sou #if IS_DESKTOP else if (ServicePointManager.DefaultConnectionLimit == ServicePointManager.DefaultPersistentConnectionLimit) { - source.PackageSource.MaxHttpRequestsPerSource = 64; + source.PackageSource.MaxHttpRequestsPerSource = DefaultMaxHttpRequestsPerSource; } #endif diff --git a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpHandlerResourceV3ProviderTests.cs b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpHandlerResourceV3ProviderTests.cs new file mode 100644 index 00000000000..bc277da50f6 --- /dev/null +++ b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpHandlerResourceV3ProviderTests.cs @@ -0,0 +1,74 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Collections.Generic; +using NuGet.Configuration; +using NuGet.Protocol.Core.Types; +using NuGet.Test.Utility; +using Xunit; + +namespace NuGet.Protocol.Tests +{ + public class HttpHandlerResourceV3ProviderTests + { +#if IS_DESKTOP + [PlatformFact(Platform.Windows)] + public void MaxConnectionsPerServerIsSetToDefaultValue_Success() + { + // Arrange + var packageSource = new PackageSource("https://contoso.com/v3/index.json"); + + var sourceRepository = new SourceRepository(packageSource, new List() { new HttpSourceResourceProvider(), new HttpHandlerResourceV3Provider() }); + + // Act + _ = sourceRepository.GetResource(); + HttpHandlerResource httpHandlerResource = sourceRepository.GetResource(); + + // Assert + Assert.NotNull(httpHandlerResource); + Assert.Equal(64, httpHandlerResource.ClientHandler.MaxConnectionsPerServer); + } + + [PlatformTheory(Platform.Windows)] + [InlineData(128)] + [InlineData(256)] + public void MaxConnectionsPerServerIsSetToMaxHttpRequestsPerSourceValue_Success(int maxHttpRequestsPerSource) + { + // Arrange + var packageSource = new PackageSource("https://contoso.com/v3/index.json") { MaxHttpRequestsPerSource = maxHttpRequestsPerSource }; + + var sourceRepository = new SourceRepository(packageSource, new List() { new HttpSourceResourceProvider(), new HttpHandlerResourceV3Provider() }); + + // Act + _ = sourceRepository.GetResource(); + HttpHandlerResource httpHandlerResource = sourceRepository.GetResource(); + + // Assert + Assert.NotNull(httpHandlerResource); + Assert.Equal(maxHttpRequestsPerSource, httpHandlerResource.ClientHandler.MaxConnectionsPerServer); + } +#endif + +#if IS_CORECLR + [Theory] + [InlineData(64)] + [InlineData(0)] + [InlineData(2)] + public void MaxConnectionsPerServerIsNotEqualToMaxHttpRequestsPerSourceValue_Success(int maxHttpRequestsPerSource) + { + // Arrange + var packageSource = new PackageSource("https://contoso.com/v3/index.json") { MaxHttpRequestsPerSource = maxHttpRequestsPerSource }; + + var sourceRepository = new SourceRepository(packageSource, new[] { new HttpHandlerResourceV3Provider() }); + + // Act + _ = sourceRepository.GetResource(); + HttpHandlerResource httpHandlerResource = sourceRepository.GetResource(); + + // Assert + Assert.NotNull(httpHandlerResource); + Assert.NotEqual(maxHttpRequestsPerSource, httpHandlerResource.ClientHandler.MaxConnectionsPerServer); + } +#endif + } +} diff --git a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpSourceResourceProviderTests.cs b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpSourceResourceProviderTests.cs new file mode 100644 index 00000000000..5534819b7a4 --- /dev/null +++ b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpSourceResourceProviderTests.cs @@ -0,0 +1,48 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using NuGet.Configuration; +using NuGet.Protocol.Core.Types; +using NuGet.Test.Utility; +using Xunit; + +namespace NuGet.Protocol.Tests +{ + public class HttpSourceResourceProviderTests + { + +#if IS_DESKTOP + [PlatformFact(Platform.Windows)] + public void EnsurePackageSourceClientCertificatesForwardedToV3HttpClientHandler() + { + // Arrange + var packageSource = new PackageSource("https://contoso.com/v3/index.json"); + var sourceRepository = new SourceRepository(packageSource, new[] { new HttpSourceResourceProvider() }); + + // Act + HttpSourceResource httpSourceResource = sourceRepository.GetResource(); + + // Assert + Assert.NotNull(httpSourceResource); + Assert.Equal(64, sourceRepository.PackageSource.MaxHttpRequestsPerSource); + } +#endif + +#if IS_CORECLR + [Fact] + public void EnsurePackageSourceClientCertificatesForwardedToV3HttpClientHandler() + { + // Arrange + var packageSource = new PackageSource("https://contoso.com/v3/index.json"); + var sourceRepository = new SourceRepository(packageSource, new[] { new HttpSourceResourceProvider() }); + + // Act + HttpSourceResource httpSourceResource = sourceRepository.GetResource(); + + // Assert + Assert.NotNull(httpSourceResource); + Assert.Equal(0, sourceRepository.PackageSource.MaxHttpRequestsPerSource); + } +#endif + } +} From c1528ca7c5cb2764b2b71171f1aac18a5709902e Mon Sep 17 00:00:00 2001 From: Kartheek Penagamuri Date: Mon, 19 Sep 2022 21:51:48 -0700 Subject: [PATCH 08/11] updated test names --- .../HttpHandlerResourceV3ProviderTests.cs | 8 ++++---- .../HttpSourceResourceProviderTests.cs | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpHandlerResourceV3ProviderTests.cs b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpHandlerResourceV3ProviderTests.cs index bc277da50f6..3d523f25b38 100644 --- a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpHandlerResourceV3ProviderTests.cs +++ b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpHandlerResourceV3ProviderTests.cs @@ -13,7 +13,7 @@ public class HttpHandlerResourceV3ProviderTests { #if IS_DESKTOP [PlatformFact(Platform.Windows)] - public void MaxConnectionsPerServerIsSetToDefaultValue_Success() + public void DefaultMaxHttpRequestsPerSourceForwardedToV3HttpClientHandler_Success() { // Arrange var packageSource = new PackageSource("https://contoso.com/v3/index.json"); @@ -32,7 +32,7 @@ public void MaxConnectionsPerServerIsSetToDefaultValue_Success() [PlatformTheory(Platform.Windows)] [InlineData(128)] [InlineData(256)] - public void MaxConnectionsPerServerIsSetToMaxHttpRequestsPerSourceValue_Success(int maxHttpRequestsPerSource) + public void PackageSourceMaxHttpRequestsPerSourceForwardedToV3HttpClientHandler_Success(int maxHttpRequestsPerSource) { // Arrange var packageSource = new PackageSource("https://contoso.com/v3/index.json") { MaxHttpRequestsPerSource = maxHttpRequestsPerSource }; @@ -52,9 +52,9 @@ public void MaxConnectionsPerServerIsSetToMaxHttpRequestsPerSourceValue_Success( #if IS_CORECLR [Theory] [InlineData(64)] - [InlineData(0)] + [InlineData(128)] [InlineData(2)] - public void MaxConnectionsPerServerIsNotEqualToMaxHttpRequestsPerSourceValue_Success(int maxHttpRequestsPerSource) + public void PackageSourceMaxHttpRequestsPerSourceNotForwardedToV3HttpClientHandler_Success(int maxHttpRequestsPerSource) { // Arrange var packageSource = new PackageSource("https://contoso.com/v3/index.json") { MaxHttpRequestsPerSource = maxHttpRequestsPerSource }; diff --git a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpSourceResourceProviderTests.cs b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpSourceResourceProviderTests.cs index 5534819b7a4..e77e09c00ac 100644 --- a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpSourceResourceProviderTests.cs +++ b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpSourceResourceProviderTests.cs @@ -13,7 +13,7 @@ public class HttpSourceResourceProviderTests #if IS_DESKTOP [PlatformFact(Platform.Windows)] - public void EnsurePackageSourceClientCertificatesForwardedToV3HttpClientHandler() + public void MaxHttpRequestsPerSourceIsToDefaultValue_Success() { // Arrange var packageSource = new PackageSource("https://contoso.com/v3/index.json"); @@ -30,7 +30,7 @@ public void EnsurePackageSourceClientCertificatesForwardedToV3HttpClientHandler( #if IS_CORECLR [Fact] - public void EnsurePackageSourceClientCertificatesForwardedToV3HttpClientHandler() + public void MaxHttpRequestsPerSourceIsSetToDefaultValue_Success() { // Arrange var packageSource = new PackageSource("https://contoso.com/v3/index.json"); From 7561404608dfdfcb8e86f5bcb94d57db9396d327 Mon Sep 17 00:00:00 2001 From: Kartheek Penagamuri Date: Mon, 19 Sep 2022 22:31:06 -0700 Subject: [PATCH 09/11] updated tests --- .../HttpHandlerResourceV3ProviderTests.cs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpHandlerResourceV3ProviderTests.cs b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpHandlerResourceV3ProviderTests.cs index 3d523f25b38..593290992b5 100644 --- a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpHandlerResourceV3ProviderTests.cs +++ b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpHandlerResourceV3ProviderTests.cs @@ -17,11 +17,10 @@ public void DefaultMaxHttpRequestsPerSourceForwardedToV3HttpClientHandler_Succes { // Arrange var packageSource = new PackageSource("https://contoso.com/v3/index.json"); - var sourceRepository = new SourceRepository(packageSource, new List() { new HttpSourceResourceProvider(), new HttpHandlerResourceV3Provider() }); + _ = sourceRepository.GetResource(); // Act - _ = sourceRepository.GetResource(); HttpHandlerResource httpHandlerResource = sourceRepository.GetResource(); // Assert @@ -36,11 +35,10 @@ public void PackageSourceMaxHttpRequestsPerSourceForwardedToV3HttpClientHandler_ { // Arrange var packageSource = new PackageSource("https://contoso.com/v3/index.json") { MaxHttpRequestsPerSource = maxHttpRequestsPerSource }; - var sourceRepository = new SourceRepository(packageSource, new List() { new HttpSourceResourceProvider(), new HttpHandlerResourceV3Provider() }); - - // Act _ = sourceRepository.GetResource(); + + // Act HttpHandlerResource httpHandlerResource = sourceRepository.GetResource(); // Assert @@ -58,11 +56,10 @@ public void PackageSourceMaxHttpRequestsPerSourceNotForwardedToV3HttpClientHandl { // Arrange var packageSource = new PackageSource("https://contoso.com/v3/index.json") { MaxHttpRequestsPerSource = maxHttpRequestsPerSource }; - var sourceRepository = new SourceRepository(packageSource, new[] { new HttpHandlerResourceV3Provider() }); - - // Act _ = sourceRepository.GetResource(); + + // Act HttpHandlerResource httpHandlerResource = sourceRepository.GetResource(); // Assert From 5711e83200a90022abcdade4c7c4a061527da7b0 Mon Sep 17 00:00:00 2001 From: Kartheek Penagamuri Date: Tue, 20 Sep 2022 18:05:37 -0700 Subject: [PATCH 10/11] made tests async --- .../HttpHandlerResourceV3ProviderTests.cs | 34 +++++++++++++------ .../HttpSourceResourceProviderTests.cs | 31 ++++++++++++----- 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpHandlerResourceV3ProviderTests.cs b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpHandlerResourceV3ProviderTests.cs index 593290992b5..f91e8cb41f2 100644 --- a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpHandlerResourceV3ProviderTests.cs +++ b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpHandlerResourceV3ProviderTests.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections.Generic; +using System.Threading.Tasks; using NuGet.Configuration; using NuGet.Protocol.Core.Types; using NuGet.Test.Utility; @@ -13,15 +14,19 @@ public class HttpHandlerResourceV3ProviderTests { #if IS_DESKTOP [PlatformFact(Platform.Windows)] - public void DefaultMaxHttpRequestsPerSourceForwardedToV3HttpClientHandler_Success() + public async Task DefaultMaxHttpRequestsPerSourceIsForwardedToV3HttpClientHandler_SuccessAsync() { // Arrange var packageSource = new PackageSource("https://contoso.com/v3/index.json"); var sourceRepository = new SourceRepository(packageSource, new List() { new HttpSourceResourceProvider(), new HttpHandlerResourceV3Provider() }); - _ = sourceRepository.GetResource(); + + // HttpSourceResourceProvider updates PackageSource.MaxHttpRequestsPerSource value for .NET Framework code paths + // HttpSource constructor accepts a delegate that creates HttpHandlerResource and it stores the delegate in a private variable. + // Hence used discard to ignore the return value and fetched HttpHandlerResource from the source repository to verify behavior. + _ = await sourceRepository.GetResourceAsync(); // Act - HttpHandlerResource httpHandlerResource = sourceRepository.GetResource(); + HttpHandlerResource httpHandlerResource = await sourceRepository.GetResourceAsync(); // Assert Assert.NotNull(httpHandlerResource); @@ -31,36 +36,43 @@ public void DefaultMaxHttpRequestsPerSourceForwardedToV3HttpClientHandler_Succes [PlatformTheory(Platform.Windows)] [InlineData(128)] [InlineData(256)] - public void PackageSourceMaxHttpRequestsPerSourceForwardedToV3HttpClientHandler_Success(int maxHttpRequestsPerSource) + public async Task PackageSourceMaxHttpRequestsPerSourceIsForwardedToV3HttpClientHandler_SuccessAsync(int maxHttpRequestsPerSource) { // Arrange var packageSource = new PackageSource("https://contoso.com/v3/index.json") { MaxHttpRequestsPerSource = maxHttpRequestsPerSource }; var sourceRepository = new SourceRepository(packageSource, new List() { new HttpSourceResourceProvider(), new HttpHandlerResourceV3Provider() }); - _ = sourceRepository.GetResource(); + + // HttpSourceResourceProvider updates PackageSource.MaxHttpRequestsPerSource value for .NET Framework code paths + // HttpSource constructor accepts a delegate that creates HttpHandlerResource and it stores the delegate in a private variable. + // Hence used discard to ignore the return value and fetched HttpHandlerResource from the source repository to verify behavior. + _ = await sourceRepository.GetResourceAsync(); // Act - HttpHandlerResource httpHandlerResource = sourceRepository.GetResource(); + HttpHandlerResource httpHandlerResource = await sourceRepository.GetResourceAsync(); // Assert Assert.NotNull(httpHandlerResource); Assert.Equal(maxHttpRequestsPerSource, httpHandlerResource.ClientHandler.MaxConnectionsPerServer); } -#endif +#elif IS_CORECLR -#if IS_CORECLR [Theory] [InlineData(64)] [InlineData(128)] [InlineData(2)] - public void PackageSourceMaxHttpRequestsPerSourceNotForwardedToV3HttpClientHandler_Success(int maxHttpRequestsPerSource) + public async Task PackageSourceMaxHttpRequestsPerSourceIsNotForwardedToV3HttpClientHandler_SuccessAsync(int maxHttpRequestsPerSource) { // Arrange var packageSource = new PackageSource("https://contoso.com/v3/index.json") { MaxHttpRequestsPerSource = maxHttpRequestsPerSource }; var sourceRepository = new SourceRepository(packageSource, new[] { new HttpHandlerResourceV3Provider() }); - _ = sourceRepository.GetResource(); + + // HttpSourceResourceProvider updates PackageSource.MaxHttpRequestsPerSource value for .NET Framework code paths + // HttpSource constructor accepts a delegate that creates HttpHandlerResource and it stores the delegate in a private variable. + // Hence used discard to ignore the return value and fetched HttpHandlerResource from the source repository to verify behavior. + _ = await sourceRepository.GetResourceAsync(); // Act - HttpHandlerResource httpHandlerResource = sourceRepository.GetResource(); + HttpHandlerResource httpHandlerResource = await sourceRepository.GetResourceAsync(); // Assert Assert.NotNull(httpHandlerResource); diff --git a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpSourceResourceProviderTests.cs b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpSourceResourceProviderTests.cs index e77e09c00ac..80c842d604c 100644 --- a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpSourceResourceProviderTests.cs +++ b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpSourceResourceProviderTests.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System.Threading.Tasks; using NuGet.Configuration; using NuGet.Protocol.Core.Types; using NuGet.Test.Utility; @@ -10,39 +11,53 @@ namespace NuGet.Protocol.Tests { public class HttpSourceResourceProviderTests { - #if IS_DESKTOP [PlatformFact(Platform.Windows)] - public void MaxHttpRequestsPerSourceIsToDefaultValue_Success() + public async Task WhenMaxHttpRequestsPerSourceIsNotConfiguredThenItsValueIsSetToDefault_SuccessAsync() { // Arrange var packageSource = new PackageSource("https://contoso.com/v3/index.json"); var sourceRepository = new SourceRepository(packageSource, new[] { new HttpSourceResourceProvider() }); // Act - HttpSourceResource httpSourceResource = sourceRepository.GetResource(); + HttpSourceResource httpSourceResource = await sourceRepository.GetResourceAsync(); // Assert Assert.NotNull(httpSourceResource); Assert.Equal(64, sourceRepository.PackageSource.MaxHttpRequestsPerSource); } -#endif - -#if IS_CORECLR +#elif IS_CORECLR [Fact] - public void MaxHttpRequestsPerSourceIsSetToDefaultValue_Success() + public async Task WhenMaxHttpRequestsPerSourceIsNotConfiguredThenItsValueWillNotBeUpdated_SuccessAsync() { // Arrange var packageSource = new PackageSource("https://contoso.com/v3/index.json"); var sourceRepository = new SourceRepository(packageSource, new[] { new HttpSourceResourceProvider() }); // Act - HttpSourceResource httpSourceResource = sourceRepository.GetResource(); + HttpSourceResource httpSourceResource = await sourceRepository.GetResourceAsync(); // Assert Assert.NotNull(httpSourceResource); Assert.Equal(0, sourceRepository.PackageSource.MaxHttpRequestsPerSource); } #endif + + [PlatformTheory] + [InlineData(128)] + [InlineData(256)] + public async Task WhenMaxHttpRequestsPerSourceIsConfiguredThenItsValueWillNotBeUpdated_SuccessAsync(int maxHttpRequestsPerSource) + { + // Arrange + var packageSource = new PackageSource("https://contoso.com/v3/index.json") { MaxHttpRequestsPerSource = maxHttpRequestsPerSource }; + var sourceRepository = new SourceRepository(packageSource, new[] { new HttpSourceResourceProvider() }); + + // Act + HttpSourceResource httpSourceResource = await sourceRepository.GetResourceAsync(); + + // Assert + Assert.NotNull(httpSourceResource); + Assert.Equal(maxHttpRequestsPerSource, sourceRepository.PackageSource.MaxHttpRequestsPerSource); + } } } From 102af0b00963ce72efda11fa8a4eb8dd1809e490 Mon Sep 17 00:00:00 2001 From: Kartheek Penagamuri Date: Tue, 20 Sep 2022 18:37:06 -0700 Subject: [PATCH 11/11] updated package source url --- .../HttpHandlerResourceV3ProviderTests.cs | 8 +++++--- .../HttpSourceResourceProviderTests.cs | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpHandlerResourceV3ProviderTests.cs b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpHandlerResourceV3ProviderTests.cs index f91e8cb41f2..417c9a19674 100644 --- a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpHandlerResourceV3ProviderTests.cs +++ b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpHandlerResourceV3ProviderTests.cs @@ -12,12 +12,14 @@ namespace NuGet.Protocol.Tests { public class HttpHandlerResourceV3ProviderTests { + private readonly string _testPackageSourceURL = "https://contoso.test/v3/index.json"; + #if IS_DESKTOP [PlatformFact(Platform.Windows)] public async Task DefaultMaxHttpRequestsPerSourceIsForwardedToV3HttpClientHandler_SuccessAsync() { // Arrange - var packageSource = new PackageSource("https://contoso.com/v3/index.json"); + var packageSource = new PackageSource(_testPackageSourceURL); var sourceRepository = new SourceRepository(packageSource, new List() { new HttpSourceResourceProvider(), new HttpHandlerResourceV3Provider() }); // HttpSourceResourceProvider updates PackageSource.MaxHttpRequestsPerSource value for .NET Framework code paths @@ -39,7 +41,7 @@ public async Task DefaultMaxHttpRequestsPerSourceIsForwardedToV3HttpClientHandle public async Task PackageSourceMaxHttpRequestsPerSourceIsForwardedToV3HttpClientHandler_SuccessAsync(int maxHttpRequestsPerSource) { // Arrange - var packageSource = new PackageSource("https://contoso.com/v3/index.json") { MaxHttpRequestsPerSource = maxHttpRequestsPerSource }; + var packageSource = new PackageSource(_testPackageSourceURL) { MaxHttpRequestsPerSource = maxHttpRequestsPerSource }; var sourceRepository = new SourceRepository(packageSource, new List() { new HttpSourceResourceProvider(), new HttpHandlerResourceV3Provider() }); // HttpSourceResourceProvider updates PackageSource.MaxHttpRequestsPerSource value for .NET Framework code paths @@ -63,7 +65,7 @@ public async Task PackageSourceMaxHttpRequestsPerSourceIsForwardedToV3HttpClient public async Task PackageSourceMaxHttpRequestsPerSourceIsNotForwardedToV3HttpClientHandler_SuccessAsync(int maxHttpRequestsPerSource) { // Arrange - var packageSource = new PackageSource("https://contoso.com/v3/index.json") { MaxHttpRequestsPerSource = maxHttpRequestsPerSource }; + var packageSource = new PackageSource(_testPackageSourceURL) { MaxHttpRequestsPerSource = maxHttpRequestsPerSource }; var sourceRepository = new SourceRepository(packageSource, new[] { new HttpHandlerResourceV3Provider() }); // HttpSourceResourceProvider updates PackageSource.MaxHttpRequestsPerSource value for .NET Framework code paths diff --git a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpSourceResourceProviderTests.cs b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpSourceResourceProviderTests.cs index 80c842d604c..de9959ddb81 100644 --- a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpSourceResourceProviderTests.cs +++ b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/HttpSourceResourceProviderTests.cs @@ -11,12 +11,14 @@ namespace NuGet.Protocol.Tests { public class HttpSourceResourceProviderTests { + private readonly string _testPackageSourceURL = "https://contoso.test/v3/index.json"; + #if IS_DESKTOP [PlatformFact(Platform.Windows)] public async Task WhenMaxHttpRequestsPerSourceIsNotConfiguredThenItsValueIsSetToDefault_SuccessAsync() { // Arrange - var packageSource = new PackageSource("https://contoso.com/v3/index.json"); + var packageSource = new PackageSource(_testPackageSourceURL); var sourceRepository = new SourceRepository(packageSource, new[] { new HttpSourceResourceProvider() }); // Act @@ -31,7 +33,7 @@ public async Task WhenMaxHttpRequestsPerSourceIsNotConfiguredThenItsValueIsSetTo public async Task WhenMaxHttpRequestsPerSourceIsNotConfiguredThenItsValueWillNotBeUpdated_SuccessAsync() { // Arrange - var packageSource = new PackageSource("https://contoso.com/v3/index.json"); + var packageSource = new PackageSource(_testPackageSourceURL); var sourceRepository = new SourceRepository(packageSource, new[] { new HttpSourceResourceProvider() }); // Act @@ -49,7 +51,7 @@ public async Task WhenMaxHttpRequestsPerSourceIsNotConfiguredThenItsValueWillNot public async Task WhenMaxHttpRequestsPerSourceIsConfiguredThenItsValueWillNotBeUpdated_SuccessAsync(int maxHttpRequestsPerSource) { // Arrange - var packageSource = new PackageSource("https://contoso.com/v3/index.json") { MaxHttpRequestsPerSource = maxHttpRequestsPerSource }; + var packageSource = new PackageSource(_testPackageSourceURL) { MaxHttpRequestsPerSource = maxHttpRequestsPerSource }; var sourceRepository = new SourceRepository(packageSource, new[] { new HttpSourceResourceProvider() }); // Act