Skip to content

Commit

Permalink
fix Timeout - Task Cancelled Exception / OperationCancelled Ex. (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchavakis committed Feb 23, 2023
1 parent a0b6fbf commit 9345754
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static bool IgnoreSslCertificateErrorCallback(HttpRequestMessage message,
// Search nuspec in local cache (Fix for linux distro)
private readonly string nugetRoot = Environment.GetEnvironmentVariable("NUGET_PACKAGES") ?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nuget", "packages");

public Methods(PackageOptions packageOptions)
public Methods(PackageOptions packageOptions, HttpClient httpClient = null)
{
if (_httpClient is null)
{
Expand All @@ -67,11 +67,20 @@ public Methods(PackageOptions packageOptions)
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, sslPolicyErrors) => IgnoreSslCertificateErrorCallback(message, cert, chain, sslPolicyErrors);
}

if (httpClient != null)
{
_httpClient = httpClient;
httpClient.BaseAddress = new Uri(nugetUrl);
httpClient.Timeout = TimeSpan.FromSeconds(packageOptions.Timeout);
}
else
{
_httpClient = new HttpClient(httpClientHandler)
{
BaseAddress = new Uri(nugetUrl),
Timeout = TimeSpan.FromSeconds(packageOptions.Timeout)
};
}
}

_serializer = new XmlSerializer(typeof(Package));
Expand Down Expand Up @@ -1055,6 +1064,11 @@ public async Task ExportLicenseTexts(List<LibraryInfo> infos)
// handled in !IsSuccessStatusCode, ignoring to continue export
break;
}
catch (Exception ex) when (ex is TaskCanceledException || ex is OperationCanceledException)
{
WriteOutput($"{ex.GetType().Name} during download of license url {info.LicenseUrl} exception {ex.Message}", logLevel: LogLevel.Verbose);
break;
}
} while (true);
}
}
Expand Down
98 changes: 98 additions & 0 deletions tests/NugetUtility.Tests/MoqMethodTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System.Collections.Generic;

using System.Diagnostics.CodeAnalysis;

using System.IO;

using System.Net.Http;

using System.Threading;

using System.Threading.Tasks;

using Moq;

using Moq.Protected;

using NUnit.Framework;




namespace NugetUtility.Tests

{

[ExcludeFromCodeCoverage]

[TestFixture]

public class MoqMethodTests

{

private string _projectPath = @"../../../";

[TestCase("BenchmarkDotNet", "0.12.1", "https://dummy.com/test", "MIT")]

[Test]

public async Task ExportLicenseTextMoqHttpResponse_Timeout(string packageName, string packageVersion, string licenseUrl, string licenseType)

{

var mockMessageHandler = new Mock<HttpMessageHandler>();

mockMessageHandler.Protected().Setup<Task<HttpResponseMessage>>("SendAsync",ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>()).ThrowsAsync(new TaskCanceledException("Timeout Exception"));




var methods = new Methods(new PackageOptions

{

ProjectDirectory = _projectPath,

Timeout = 2,

ExportLicenseTexts = true,

},new HttpClient(mockMessageHandler.Object));




List<LibraryInfo> infos = new List<LibraryInfo>();

infos.Add(new LibraryInfo()

{

PackageName = packageName,

PackageVersion = packageVersion,

LicenseUrl = licenseUrl,

LicenseType = licenseType,

});

await methods.ExportLicenseTexts(infos);

var directory = methods.GetExportDirectory();

var outpath = Path.Combine(directory, packageName + "_" + packageVersion + ".txt");

var outpathhtml = Path.Combine(directory, packageName + "_" + packageVersion + ".html");

// file not generated

Assert.That(!File.Exists(outpath) || !File.Exists(outpathhtml));

}

}

}
1 change: 1 addition & 0 deletions tests/NugetUtility.Tests/NugetUtility.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.2.0" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="NUnit3TestAdapter" Version="4.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
</ItemGroup>
Expand Down

0 comments on commit 9345754

Please sign in to comment.