Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Timeout - Task Cancelled Exception #184

Merged
merged 2 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(TaskCanceledException ex)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTTP Client sometimes throws an OperationCanceledException as well. Because TaskCanceledException derives from OperationCanceledException we can catch OperationCanceledException and slightly adjust our log message to handle both exceptions.

Suggested change
catch(TaskCanceledException ex)
catch(OperationCanceledException ex)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the recommendation. I pushed a new commit.

{
WriteOutput($"Task Cancelled Exception during download of license url {info.LicenseUrl} exception {ex.Message}", logLevel: LogLevel.Verbose);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
WriteOutput($"Task Cancelled Exception during download of license url {info.LicenseUrl} exception {ex.Message}", logLevel: LogLevel.Verbose);
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