-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fixed and Added unit tests for EnsureResourceAsync hanging issue #4943
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
Merged
mstfbl
merged 25 commits into
dotnet:master
from
mstfbl:fix-CheckValidDownload-and-its-dependencies
Mar 25, 2020
Merged
Changes from 19 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
fedd23c
Update ResourceManagerUtils.cs
mstfbl 85f10af
Added TestDownloadFromLocal
mstfbl 63b3f33
Added TestDownloadError
mstfbl e16b7d6
Revert "Added TestDownloadError"
mstfbl 2caf810
Edit EnsureResourceAsync and its dependencies
mstfbl 6e05a87
Edited TestDownloadFromLocal and re-added TestDownloadError()
mstfbl 69b9827
Disabling TestDownloadFromLocal and TestDownloadError
mstfbl 6e5b246
Edits
mstfbl cd56549
Re-activated TestDownloadError and TestDownloadFromLocal
mstfbl 2c4d22e
Edits, added 5 min timeout, and debugging requested url
mstfbl 2f67666
Removed timeouts, and re-added Resource download tests in separate un…
mstfbl 8bf03c8
Edits
mstfbl fd3c7e6
Removed hardcode "microsoft.com" check for HTTP Status Code
mstfbl bc8b065
Update ResourceManagerUtils.cs
mstfbl 95514c4
Edits for reviews, removing hardcodings of status codes
mstfbl 93b5454
Removing paranthesis from one-liner if statement
mstfbl a54c7e0
Update TestResourceDownload.cs
mstfbl b8d5094
Update TestResourceDownload.cs
mstfbl 38fc48f
Nit fix + test case fixes
mstfbl 666d328
Update ResourceManagerUtils.cs
mstfbl a9e1b5d
Update ResourceManagerUtils.cs
mstfbl d460db7
Update ResourceManagerUtils.cs
mstfbl ed3c6fc
Update ResourceManagerUtils.cs
mstfbl d7b43ed
Added checking for the host of the download absoluteURL euqaling "aka…
mstfbl d9cdc07
Edit TestResourceDownload
mstfbl 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 hidden or 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
This file contains hidden or 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
276 changes: 276 additions & 0 deletions
276
test/Microsoft.ML.Core.Tests/UnitTests/TestResourceDownload.cs
This file contains hidden or 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,276 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.ML.Internal.Utilities; | ||
using Microsoft.ML.RunTests; | ||
using Microsoft.ML.Runtime; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
[assembly: CollectionBehavior(DisableTestParallelization = true)] | ||
|
||
namespace Microsoft.ML.Core.Tests.UnitTests | ||
{ | ||
public class TestResourceDownload : BaseTestBaseline | ||
{ | ||
public TestResourceDownload(ITestOutputHelper helper) | ||
: base(helper) | ||
{ | ||
} | ||
[Fact] | ||
[TestCategory("ResourceDownload")] | ||
public void TestDownloadFromLocal() | ||
{ | ||
var envVarOld = Environment.GetEnvironmentVariable(ResourceManagerUtils.CustomResourcesUrlEnvVariable); | ||
var resourcePathVarOld = Environment.GetEnvironmentVariable(Utils.CustomSearchDirEnvVariable); | ||
Environment.SetEnvironmentVariable(Utils.CustomSearchDirEnvVariable, null); | ||
|
||
var baseDir = GetOutputPath("resources"); | ||
Assert.True(Uri.TryCreate(baseDir, UriKind.Absolute, out var baseDirUri), "Uri.TryCreate failed"); | ||
Environment.SetEnvironmentVariable(ResourceManagerUtils.CustomResourcesUrlEnvVariable, baseDirUri.AbsoluteUri); | ||
var envVar = Environment.GetEnvironmentVariable(ResourceManagerUtils.CustomResourcesUrlEnvVariable); | ||
Assert.True(envVar == baseDirUri.AbsoluteUri); | ||
var path = DeleteOutputPath(Path.Combine("resources", "subdir"), "breast-cancer.txt"); | ||
|
||
var bc = GetDataPath("breast-cancer.txt"); | ||
File.Copy(bc, path); | ||
|
||
var saveToDir = GetOutputPath("copyto"); | ||
DeleteOutputPath("copyto", "breast-cancer.txt"); | ||
var sbOut = new StringBuilder(); | ||
var env = new ConsoleEnvironment(42); | ||
using (var ch = env.Start("Downloading")) | ||
{ | ||
try | ||
{ | ||
var t = ResourceManagerUtils.Instance.EnsureResourceAsync(env, ch, "subdir/breast-cancer.txt", "breast-cancer.txt", saveToDir, 1 * 60 * 1000); | ||
t.Wait(); | ||
|
||
if (t.Result.ErrorMessage != null) | ||
Fail(String.Format("Expected zero length error string. Received error: {0}", t.Result.ErrorMessage)); | ||
if (t.Status != TaskStatus.RanToCompletion) | ||
Fail("Download did not complete succesfully"); | ||
if (!File.Exists(GetOutputPath("copyto", "breast-cancer.txt"))) | ||
{ | ||
Fail($"File '{GetOutputPath("copyto", "breast-cancer.txt")}' does not exist. " + | ||
$"File was downloaded to '{t.Result.FileName}' instead." + | ||
$"MICROSOFTML_RESOURCE_PATH is set to {Environment.GetEnvironmentVariable(Utils.CustomSearchDirEnvVariable)}"); | ||
} | ||
Done(); | ||
} | ||
finally | ||
{ | ||
// Set environment variable back to its old value. | ||
Environment.SetEnvironmentVariable(ResourceManagerUtils.CustomResourcesUrlEnvVariable, envVarOld); | ||
Environment.SetEnvironmentVariable(Utils.CustomSearchDirEnvVariable, resourcePathVarOld); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
[TestCategory("ResourceDownload")] | ||
public void TestDownloadError() | ||
{ | ||
var envVarOld = Environment.GetEnvironmentVariable(ResourceManagerUtils.CustomResourcesUrlEnvVariable); | ||
var timeoutVarOld = Environment.GetEnvironmentVariable(ResourceManagerUtils.TimeoutEnvVariable); | ||
var resourcePathVarOld = Environment.GetEnvironmentVariable(Utils.CustomSearchDirEnvVariable); | ||
Environment.SetEnvironmentVariable(Utils.CustomSearchDirEnvVariable, null); | ||
|
||
// Bad local path. | ||
try | ||
{ | ||
if (!Uri.TryCreate($@"\\ct01\public\{Guid.NewGuid()}\", UriKind.Absolute, out var badUri)) | ||
mstfbl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Fail("Uri could not be created"); | ||
Environment.SetEnvironmentVariable(ResourceManagerUtils.CustomResourcesUrlEnvVariable, badUri.AbsoluteUri); | ||
var envVar = Environment.GetEnvironmentVariable(ResourceManagerUtils.CustomResourcesUrlEnvVariable); | ||
if (envVar != badUri.AbsoluteUri) | ||
Fail("Environment variable not set properly"); | ||
|
||
var saveToDir = GetOutputPath("copyto"); | ||
DeleteOutputPath("copyto", "breast-cancer.txt"); | ||
var sbOut = new StringBuilder(); | ||
var sbErr = new StringBuilder(); | ||
using (var outWriter = new StringWriter(sbOut)) | ||
using (var errWriter = new StringWriter(sbErr)) | ||
{ | ||
var env = new ConsoleEnvironment(42, outWriter: outWriter, errWriter: errWriter); | ||
using (var ch = env.Start("Downloading")) | ||
{ | ||
var t = ResourceManagerUtils.Instance.EnsureResourceAsync(env, ch, "breast-cancer.txt", "breast-cancer.txt", saveToDir, 10 * 1000); | ||
t.Wait(); | ||
|
||
Log("Bad path"); | ||
Log($"out: {sbOut.ToString()}"); | ||
Log($"error: {sbErr.ToString()}"); | ||
if (t.Status != TaskStatus.RanToCompletion) | ||
Fail("Download did not complete succesfully"); | ||
if (File.Exists(Path.Combine(saveToDir, "breast-cancer.txt"))) | ||
Fail($"File '{GetOutputPath("copyto", "breast-cancer.txt")}' should have been deleted."); | ||
} | ||
} | ||
|
||
// Good local path, bad file name. | ||
if (!Uri.TryCreate(GetDataPath("breast-cancer.txt") + "bad_addition", UriKind.Absolute, out var goodUri)) | ||
Fail("Uri could not be created"); | ||
|
||
Environment.SetEnvironmentVariable(ResourceManagerUtils.CustomResourcesUrlEnvVariable, goodUri.AbsoluteUri); | ||
envVar = Environment.GetEnvironmentVariable(ResourceManagerUtils.CustomResourcesUrlEnvVariable); | ||
if (envVar != goodUri.AbsoluteUri) | ||
Fail("Environment variable not set properly"); | ||
|
||
DeleteOutputPath("copyto", "breast-cancer.txt"); | ||
sbOut.Clear(); | ||
sbErr.Clear(); | ||
using (var outWriter = new StringWriter(sbOut)) | ||
using (var errWriter = new StringWriter(sbErr)) | ||
{ | ||
var env = new ConsoleEnvironment(42, outWriter: outWriter, errWriter: errWriter); | ||
|
||
using (var ch = env.Start("Downloading")) | ||
{ | ||
var t = ResourceManagerUtils.Instance.EnsureResourceAsync(env, ch, "breast-cancer1.txt", "breast-cancer.txt", saveToDir, 10 * 1000); | ||
t.Wait(); | ||
|
||
Log("Good path, bad file name"); | ||
Log($"out: {sbOut.ToString()}"); | ||
Log($"error: {sbErr.ToString()}"); | ||
|
||
if (t.Status != TaskStatus.RanToCompletion) | ||
Fail("Download did not complete succesfully"); | ||
if (File.Exists(Path.Combine(saveToDir, "breast-cancer.txt"))) | ||
Fail($"File '{GetOutputPath("copyto", "breast-cancer.txt")}' should have been deleted."); | ||
} | ||
} | ||
|
||
// Bad url. | ||
if (!Uri.TryCreate("https://fake-website/fake-model.model/", UriKind.Absolute, out badUri)) | ||
Fail("Uri could not be created"); | ||
|
||
Environment.SetEnvironmentVariable(ResourceManagerUtils.CustomResourcesUrlEnvVariable, badUri.AbsoluteUri); | ||
envVar = Environment.GetEnvironmentVariable(ResourceManagerUtils.CustomResourcesUrlEnvVariable); | ||
if (envVar != badUri.AbsoluteUri) | ||
Fail("Environment variable not set properly"); | ||
|
||
DeleteOutputPath("copyto", "ResNet_18_Updated.model"); | ||
sbOut.Clear(); | ||
sbErr.Clear(); | ||
using (var outWriter = new StringWriter(sbOut)) | ||
using (var errWriter = new StringWriter(sbErr)) | ||
{ | ||
var env = new ConsoleEnvironment(42, outWriter: outWriter, errWriter: errWriter); | ||
using (var ch = env.Start("Downloading")) | ||
{ | ||
var fileName = "test_bad_url.model"; | ||
var t = ResourceManagerUtils.Instance.EnsureResourceAsync(env, ch, "Image/ResNet_18_Updated.model", fileName, saveToDir, 10 * 1000); | ||
t.Wait(); | ||
|
||
Log("Bad url"); | ||
Log($"out: {sbOut.ToString()}"); | ||
Log($"error: {sbErr.ToString()}"); | ||
|
||
if (t.Status != TaskStatus.RanToCompletion) | ||
Fail("Download did not complete succesfully"); | ||
if (File.Exists(Path.Combine(saveToDir, fileName))) | ||
Fail($"File '{Path.Combine(saveToDir, fileName)}' should have been deleted."); | ||
} | ||
} | ||
|
||
// Good url, bad page. | ||
if (!Uri.TryCreate("https://cnn.com/", UriKind.Absolute, out var cnn)) | ||
Fail("Uri could not be created"); | ||
Environment.SetEnvironmentVariable(ResourceManagerUtils.CustomResourcesUrlEnvVariable, cnn.AbsoluteUri); | ||
envVar = Environment.GetEnvironmentVariable(ResourceManagerUtils.CustomResourcesUrlEnvVariable); | ||
if (envVar != cnn.AbsoluteUri) | ||
Fail("Environment variable not set properly"); | ||
|
||
DeleteOutputPath("copyto", "ResNet_18_Updated.model"); | ||
sbOut.Clear(); | ||
sbErr.Clear(); | ||
using (var outWriter = new StringWriter(sbOut)) | ||
using (var errWriter = new StringWriter(sbErr)) | ||
{ | ||
var env = new ConsoleEnvironment(42, outWriter: outWriter, errWriter: errWriter); | ||
using (var ch = env.Start("Downloading")) | ||
{ | ||
var fileName = "test_cnn_page_does_not_exist.model"; | ||
var t = ResourceManagerUtils.Instance.EnsureResourceAsync(env, ch, "Image/ResNet_18_Updated.model", fileName, saveToDir, 10 * 1000); | ||
t.Wait(); | ||
|
||
Log("Good url, bad page"); | ||
Log($"out: {sbOut.ToString()}"); | ||
Log($"error: {sbErr.ToString()}"); | ||
|
||
if (t.Status != TaskStatus.RanToCompletion) | ||
Fail("Download did not complete succesfully"); | ||
#if !CORECLR | ||
if (!sbErr.ToString().Contains("(404) Not Found")) | ||
Fail($"Error message should contain '(404) Not Found. Instead: {sbErr.ToString()}"); | ||
#endif | ||
if (File.Exists(Path.Combine(saveToDir, fileName))) | ||
Fail($"File '{Path.Combine(saveToDir, fileName)}' should have been deleted."); | ||
} | ||
} | ||
|
||
// Download from local, short time out. | ||
#if CORECLR | ||
var path = Path.Combine(Path.GetDirectoryName(typeof(TestImageAnalyticsTransforms).Assembly.Location), "..", "AutoLoad"); | ||
#else | ||
var path = Path.GetDirectoryName(typeof(TestImageAnalyticsTransforms).Assembly.Location); | ||
#endif | ||
|
||
Assert.True(Uri.TryCreate(path, UriKind.Absolute, out var baseDirUri)); | ||
Environment.SetEnvironmentVariable(ResourceManagerUtils.CustomResourcesUrlEnvVariable, baseDirUri.AbsoluteUri); | ||
envVar = Environment.GetEnvironmentVariable(ResourceManagerUtils.CustomResourcesUrlEnvVariable); | ||
if (envVar != baseDirUri.AbsoluteUri) | ||
Fail("Environment variable not set properly"); | ||
|
||
Environment.SetEnvironmentVariable(ResourceManagerUtils.CustomResourcesUrlEnvVariable, null); | ||
envVar = Environment.GetEnvironmentVariable(ResourceManagerUtils.CustomResourcesUrlEnvVariable); | ||
if (envVar != null) | ||
Fail("Environment variable not set properly"); | ||
|
||
Environment.SetEnvironmentVariable(ResourceManagerUtils.TimeoutEnvVariable, "10"); | ||
envVar = Environment.GetEnvironmentVariable(ResourceManagerUtils.TimeoutEnvVariable); | ||
if (envVar != "10") | ||
Fail("Environment variable not set properly"); | ||
|
||
DeleteOutputPath("copyto", "ResNet_18_Updated.model"); | ||
sbOut.Clear(); | ||
sbErr.Clear(); | ||
using (var outWriter = new StringWriter(sbOut)) | ||
using (var errWriter = new StringWriter(sbErr)) | ||
{ | ||
var env = new ConsoleEnvironment(42, outWriter: outWriter, errWriter: errWriter); | ||
using (var ch = env.Start("Downloading")) | ||
{ | ||
var fileName = "test_short_timeout.model"; | ||
var t = ResourceManagerUtils.Instance.EnsureResourceAsync(env, ch, "Image/AlexNet_Updated.model", fileName, saveToDir, 10 * 1000); | ||
t.Wait(); | ||
|
||
Log("Default url, short time out"); | ||
Log($"out: {sbOut.ToString()}"); | ||
Log($"error: {sbErr.ToString()}"); | ||
|
||
#if !CORECLR | ||
if (!sbErr.ToString().Contains("Download timed out")) | ||
Fail($"Error message should contain the string 'Download timed out'. Instead: {sbErr.ToString()}"); | ||
#endif | ||
if (File.Exists(Path.Combine(saveToDir, fileName))) | ||
Fail($"File '{Path.Combine(saveToDir, fileName)}' should have been deleted."); | ||
} | ||
} | ||
Done(); | ||
} | ||
finally | ||
{ | ||
// Set environment variable back to its old value. | ||
Environment.SetEnvironmentVariable(ResourceManagerUtils.CustomResourcesUrlEnvVariable, envVarOld); | ||
Environment.SetEnvironmentVariable(ResourceManagerUtils.TimeoutEnvVariable, timeoutVarOld); | ||
Environment.SetEnvironmentVariable(Utils.CustomSearchDirEnvVariable, resourcePathVarOld); | ||
} | ||
} | ||
mstfbl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.