Skip to content

Commit

Permalink
[tests] Move deletion of existing temporary directory to the backgrou…
Browse files Browse the repository at this point in the history
…nd. (#12511)

Improve Cache.CreateTemporaryDirectory to move the deletion of the previous
root temporary directory to a background thread. In some cases this directory
can have a lot of files and directories, and deleting it synchronously can
cause a significant startup delay when running tests locally.
  • Loading branch information
rolfbjarne authored Aug 23, 2021
1 parent 1175a13 commit 4f5e2c5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
22 changes: 20 additions & 2 deletions tests/mtouch/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;

namespace Xamarin
{
Expand All @@ -18,8 +19,25 @@ public static class Cache // Not really a cache (since the root directory is cle
static Cache ()
{
root = Path.Combine (Path.GetDirectoryName (System.Reflection.Assembly.GetExecutingAssembly ().Location), "tmp-test-dir");
if (Directory.Exists (root))
Directory.Delete (root, true);
if (Directory.Exists (root)) {
var movedRoot = root + DateTime.UtcNow.Ticks.ToString () + "-deletion-in-progress";
// The temporary directory can be big, and it can take a while to clean it out.
// So move it to a different name (which should be fast), and then do the deletion on a different thread.
// This should speed up startup in some cases.
if (!Directory.Exists (movedRoot)) {
try {
Directory.Move (root, movedRoot);
ThreadPool.QueueUserWorkItem ((v) => {
Directory.Delete (movedRoot, true);
});
} catch (Exception e) {
// Just delete the root if we can't move the temporary directory.
Directory.Delete (root, true);
}
} else {
Directory.Delete (root, true);
}
}
Directory.CreateDirectory (root);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/xharness/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
xharness.csproj.inc
tmp-test-dir
tmp-test-dir*

/xharness.exe.config
3 changes: 2 additions & 1 deletion tests/xharness/TestProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.DotNet.XHarness.Common.Execution;
using Microsoft.DotNet.XHarness.Common.Logging;
using Microsoft.DotNet.XHarness.iOS.Shared.Utilities;
using Xamarin;
using Xharness.Jenkins.TestTasks;

namespace Xharness {
Expand Down Expand Up @@ -87,7 +88,7 @@ public Task CreateCopyAsync (ILog log, IProcessManager processManager, ITestTask

async Task CreateCopyAsync (ILog log, IProcessManager processManager, ITestTask test, string rootDirectory, Dictionary<string, TestProject> allProjectReferences)
{
var directory = DirectoryUtilities.CreateTemporaryDirectory (test?.TestName ?? System.IO.Path.GetFileNameWithoutExtension (Path));
var directory = Cache.CreateTemporaryDirectory (test?.TestName ?? System.IO.Path.GetFileNameWithoutExtension (Path));
Directory.CreateDirectory (directory);
var original_path = Path;
Path = System.IO.Path.Combine (directory, System.IO.Path.GetFileName (Path));
Expand Down
3 changes: 3 additions & 0 deletions tests/xharness/xharness.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@
<Compile Include="..\..\tools\common\StringUtils.cs">
<Link>StringUtils.cs</Link>
</Compile>
<Compile Include="..\mtouch\Cache.cs">
<Link>Cache.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\tools\common\SdkVersions.cs">
Expand Down

5 comments on commit 4f5e2c5

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

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

❌ [CI Build] Tests failed on Build ❌

Tests failed on Build.

API diff

✅ API Diff from stable

View API diff

API & Generator diff

API Diff (from PR only) (no change)
Generator Diff (only version changes)

Packages generated

View packages

Test results

1 tests failed, 248 tests passed.

Failed tests

  • Documentation/All: Failed

Pipeline on Agent XAMBOT-1036.BigSur'
[tests] Move deletion of existing temporary directory to the background. (#12511)

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

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

⚠️ Tests were not ran (VSTS: device tests iOS). ⚠️

Results were skipped for this run due to provisioning problems Azure Devops. Please contact the bot administrator.

Pipeline on Agent
[tests] Move deletion of existing temporary directory to the background. (#12511)

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

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

⚠️ Tests were not ran (VSTS: device tests tvOS). ⚠️

Results were skipped for this run due to provisioning problems Azure Devops. Please contact the bot administrator.

Pipeline on Agent
[tests] Move deletion of existing temporary directory to the background. (#12511)

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

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

❌ Tests failed on macOS M1 - Mac Big Sur (11.5) ❌

Tests failed on M1 - Mac Big Sur (11.5).

Failed tests are:

  • xammac_tests

Pipeline on Agent
[tests] Move deletion of existing temporary directory to the background. (#12511)

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

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

✅ Tests passed on macOS Mac Mojave (10.14) ✅

Tests passed

All tests on macOS X Mac Mojave (10.14) passed.

Pipeline on Agent
[tests] Move deletion of existing temporary directory to the background. (#12511)

Please sign in to comment.