-
Notifications
You must be signed in to change notification settings - Fork 79
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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) | ||||||
{ | ||||||
|
@@ -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)); | ||||||
|
@@ -1055,6 +1064,11 @@ public async Task ExportLicenseTexts(List<LibraryInfo> infos) | |||||
// handled in !IsSuccessStatusCode, ignoring to continue export | ||||||
break; | ||||||
} | ||||||
catch(TaskCanceledException ex) | ||||||
{ | ||||||
WriteOutput($"Task Cancelled Exception during download of license url {info.LicenseUrl} exception {ex.Message}", logLevel: LogLevel.Verbose); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
break; | ||||||
} | ||||||
} while (true); | ||||||
} | ||||||
} | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
|
||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.