2020-12-23 11:35:49 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
2017-05-12 14:49:44 +02:00
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
2021-03-03 13:43:27 +11:00
|
|
|
using System.Threading.Tasks;
|
2020-09-17 11:35:29 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
2021-03-03 13:43:27 +11:00
|
|
|
using Moq;
|
2017-05-12 14:49:44 +02:00
|
|
|
using NUnit.Framework;
|
2022-06-21 08:09:38 +02:00
|
|
|
using Umbraco.Cms.Core;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Hosting;
|
|
|
|
|
using Umbraco.Cms.Core.IO;
|
2022-04-26 10:22:37 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.Scoping;
|
Examine 2.0 integration (#10241)
* Init commit for examine 2.0 work, most old umb examine tests working, probably a lot that doesn't
* Gets Umbraco Examine tests passing and makes some sense out of them, fixes some underlying issues.
* Large refactor, remove TaskHelper, rename Notifications to be consistent, Gets all examine/lucene indexes building and startup ordered in the correct way, removes old files, creates new IUmbracoIndexingHandler for abstracting out all index operations for umbraco data, abstracts out IIndexRebuilder, Fixes Stack overflow with LiveModelsProvider and loading assemblies, ports some changes from v8 for startup handling with cold boots, refactors out LastSyncedFileManager
* fix up issues with rebuilding and management dashboard.
* removes old files, removes NetworkHelper, fixes LastSyncedFileManager implementation to ensure the machine name is used, fix up logging with cold boot state.
* Makes MainDom safer to use and makes PublishedSnapshotService lazily register with MainDom
* lazily acquire application id (fix unit tests)
* Fixes resource casing and missing test file
* Ensures caches when requiring internal services for PublishedSnapshotService, UseNuCache is a separate call, shouldn't be buried in AddWebComponents, was also causing issues in integration tests since nucache was being used for the Id2Key service.
* For UmbracoTestServerTestBase enable nucache services
* Fixing tests
* Fix another test
* Fixes tests, use TestHostingEnvironment, make Tests.Common use net5, remove old Lucene.Net.Contrib ref.
* Fixes up some review notes
* Fixes issue with doubly registering PublishedSnapshotService meanig there could be 2x instances of it
* Checks for parseexception when executing the query
* Use application root instead of duplicating functionality.
* Added Examine project to netcore only solution file
* Fixed casing issue with LazyLoad, that is not lowercase.
* uses cancellationToken instead of bool flag, fixes always reading lastId from the LastSyncedFileManager, fixes RecurringHostedServiceBase so that there isn't an overlapping thread for the same task type
* Fix tests
* remove legacy test project from solution file
* Fix test
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-05-18 18:31:38 +10:00
|
|
|
using Umbraco.Cms.Tests.Common;
|
2021-02-10 14:45:44 +01:00
|
|
|
using Umbraco.Cms.Tests.Common.Testing;
|
2021-02-11 08:30:27 +01:00
|
|
|
using Umbraco.Cms.Tests.Integration.Testing;
|
2021-02-09 11:26:22 +01:00
|
|
|
using Umbraco.Extensions;
|
2017-05-12 14:49:44 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Scoping;
|
2022-04-26 10:22:37 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[TestFixture]
|
|
|
|
|
[UmbracoTest(Database = UmbracoTestOptions.Database.NewEmptyPerTest)]
|
|
|
|
|
public class ScopeFileSystemsTests : UmbracoIntegrationTest
|
2017-05-12 14:49:44 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
[SetUp]
|
|
|
|
|
public void SetUp() => ClearFiles(IOHelper);
|
|
|
|
|
|
|
|
|
|
[TearDown]
|
|
|
|
|
public void Teardown() => ClearFiles(IOHelper);
|
|
|
|
|
|
|
|
|
|
private MediaFileManager MediaFileManager => GetRequiredService<MediaFileManager>();
|
|
|
|
|
|
|
|
|
|
private IHostingEnvironment HostingEnvironment => GetRequiredService<IHostingEnvironment>();
|
|
|
|
|
|
|
|
|
|
private void ClearFiles(IIOHelper ioHelper)
|
2017-05-12 14:49:44 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
TestHelper.DeleteDirectory(ioHelper.MapPath("media"));
|
|
|
|
|
TestHelper.DeleteDirectory(ioHelper.MapPath("FileSysTests"));
|
|
|
|
|
TestHelper.DeleteDirectory(ioHelper.MapPath(Constants.SystemDirectories.TempData.EnsureEndsWith('/') +
|
|
|
|
|
"ShadowFs"));
|
|
|
|
|
}
|
2020-12-23 11:35:49 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void MediaFileManager_does_not_write_to_physical_file_system_when_scoped_if_scope_does_not_complete()
|
|
|
|
|
{
|
|
|
|
|
var rootPath = HostingEnvironment.MapPathWebRoot(GlobalSettings.UmbracoMediaPhysicalRootPath);
|
|
|
|
|
var rootUrl = HostingEnvironment.ToAbsolute(GlobalSettings.UmbracoMediaPath);
|
|
|
|
|
var physMediaFileSystem = new PhysicalFileSystem(IOHelper, HostingEnvironment, GetRequiredService<ILogger<PhysicalFileSystem>>(), rootPath, rootUrl);
|
|
|
|
|
var mediaFileManager = MediaFileManager;
|
2017-05-12 14:49:44 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
|
2017-05-12 14:49:44 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
using (ScopeProvider.CreateScope(scopeFileSystems: true))
|
2017-05-12 14:49:44 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes("foo")))
|
|
|
|
|
{
|
|
|
|
|
MediaFileManager.FileSystem.AddFile("f1.txt", ms);
|
|
|
|
|
}
|
2017-05-12 14:49:44 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.IsTrue(mediaFileManager.FileSystem.FileExists("f1.txt"));
|
|
|
|
|
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(mediaFileManager.FileSystem.FileExists("f1.txt"));
|
|
|
|
|
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
|
2017-05-12 14:49:44 +02:00
|
|
|
}
|
2020-10-28 14:54:16 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// After scope is disposed ensure shadow wrapper didn't commit to physical
|
|
|
|
|
Assert.IsFalse(mediaFileManager.FileSystem.FileExists("f1.txt"));
|
|
|
|
|
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
|
|
|
|
|
}
|
2017-05-12 14:49:44 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void MediaFileManager_writes_to_physical_file_system_when_scoped_and_scope_is_completed()
|
|
|
|
|
{
|
|
|
|
|
var rootPath = HostingEnvironment.MapPathWebRoot(GlobalSettings.UmbracoMediaPhysicalRootPath);
|
|
|
|
|
var rootUrl = HostingEnvironment.ToAbsolute(GlobalSettings.UmbracoMediaPath);
|
|
|
|
|
var physMediaFileSystem = new PhysicalFileSystem(IOHelper, HostingEnvironment, GetRequiredService<ILogger<PhysicalFileSystem>>(), rootPath, rootUrl);
|
|
|
|
|
var mediaFileManager = MediaFileManager;
|
|
|
|
|
|
|
|
|
|
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
|
2017-05-12 14:49:44 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
using (var scope = ScopeProvider.CreateScope(scopeFileSystems: true))
|
|
|
|
|
{
|
|
|
|
|
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes("foo")))
|
2017-05-12 14:49:44 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
mediaFileManager.FileSystem.AddFile("f1.txt", ms);
|
|
|
|
|
}
|
2020-12-23 11:35:49 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.IsTrue(mediaFileManager.FileSystem.FileExists("f1.txt"));
|
|
|
|
|
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
|
2020-10-28 14:54:16 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
scope.Complete();
|
2017-05-12 14:49:44 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.IsTrue(mediaFileManager.FileSystem.FileExists("f1.txt"));
|
2020-10-26 10:47:14 +00:00
|
|
|
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// After scope is disposed ensure shadow wrapper writes to physical file system
|
|
|
|
|
Assert.IsTrue(mediaFileManager.FileSystem.FileExists("f1.txt"));
|
|
|
|
|
Assert.IsTrue(physMediaFileSystem.FileExists("f1.txt"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void MultiThread()
|
|
|
|
|
{
|
|
|
|
|
var rootPath = HostingEnvironment.MapPathWebRoot(GlobalSettings.UmbracoMediaPhysicalRootPath);
|
|
|
|
|
var rootUrl = HostingEnvironment.ToAbsolute(GlobalSettings.UmbracoMediaPath);
|
|
|
|
|
var physMediaFileSystem = new PhysicalFileSystem(IOHelper, HostingEnvironment, GetRequiredService<ILogger<PhysicalFileSystem>>(), rootPath, rootUrl);
|
|
|
|
|
var mediaFileManager = MediaFileManager;
|
|
|
|
|
var taskHelper = new TaskHelper(Mock.Of<ILogger<TaskHelper>>());
|
|
|
|
|
|
|
|
|
|
var scopeProvider = ScopeProvider;
|
|
|
|
|
using (var scope = scopeProvider.CreateScope(scopeFileSystems: true))
|
2020-10-26 10:47:14 +00:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes("foo")))
|
|
|
|
|
{
|
|
|
|
|
mediaFileManager.FileSystem.AddFile("f1.txt", ms);
|
|
|
|
|
}
|
2020-10-26 10:47:14 +00:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.IsTrue(mediaFileManager.FileSystem.FileExists("f1.txt"));
|
2020-10-26 10:47:14 +00:00
|
|
|
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
|
|
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// execute on another disconnected thread (execution context will not flow)
|
|
|
|
|
var t = taskHelper.ExecuteBackgroundTask(() =>
|
2017-05-12 14:49:44 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.IsFalse(mediaFileManager.FileSystem.FileExists("f1.txt"));
|
|
|
|
|
|
2020-10-26 10:47:14 +00:00
|
|
|
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes("foo")))
|
2020-12-23 11:35:49 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
mediaFileManager.FileSystem.AddFile("f2.txt", ms);
|
2020-12-23 11:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.IsTrue(mediaFileManager.FileSystem.FileExists("f2.txt"));
|
|
|
|
|
Assert.IsTrue(physMediaFileSystem.FileExists("f2.txt"));
|
2020-10-26 10:47:14 +00:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
return Task.CompletedTask;
|
|
|
|
|
});
|
2020-10-26 10:47:14 +00:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Task.WaitAll(t);
|
2020-10-26 10:47:14 +00:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.IsTrue(mediaFileManager.FileSystem.FileExists("f2.txt"));
|
|
|
|
|
Assert.IsTrue(physMediaFileSystem.FileExists("f2.txt"));
|
2017-05-12 14:49:44 +02:00
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
2017-05-12 14:49:44 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void SingleShadow()
|
|
|
|
|
{
|
|
|
|
|
var taskHelper = new TaskHelper(Mock.Of<ILogger<TaskHelper>>());
|
|
|
|
|
var scopeProvider = ScopeProvider;
|
|
|
|
|
var isThrown = false;
|
|
|
|
|
using (var scope = scopeProvider.CreateScope(scopeFileSystems: true))
|
2017-05-12 14:49:44 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
// This is testing when another thread concurrently tries to create a scoped file system
|
|
|
|
|
// because at the moment we don't support concurrent scoped filesystems.
|
|
|
|
|
var t = taskHelper.ExecuteBackgroundTask(() =>
|
2017-05-12 14:49:44 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
// ok to create a 'normal' other scope
|
|
|
|
|
using (var other = scopeProvider.CreateScope())
|
2020-12-23 11:35:49 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
other.Complete();
|
2020-12-23 11:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// not ok to create a 'scoped filesystems' other scope
|
|
|
|
|
// we will get a "Already shadowing." exception.
|
|
|
|
|
Assert.Throws<InvalidOperationException>(() =>
|
2017-05-12 14:49:44 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
using var other = scopeProvider.CreateScope(scopeFileSystems: true);
|
2021-03-03 13:43:27 +11:00
|
|
|
});
|
|
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
isThrown = true;
|
2017-05-12 14:49:44 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
return Task.CompletedTask;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Task.WaitAll(t);
|
2017-05-12 14:49:44 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.IsTrue(isThrown);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void SingleShadowEvenDetached()
|
|
|
|
|
{
|
|
|
|
|
var taskHelper = new TaskHelper(Mock.Of<ILogger<TaskHelper>>());
|
|
|
|
|
var scopeProvider = (ScopeProvider)ScopeProvider;
|
|
|
|
|
using (var scope = scopeProvider.CreateScope(scopeFileSystems: true))
|
2017-05-12 14:49:44 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
// This is testing when another thread concurrently tries to create a scoped file system
|
|
|
|
|
// because at the moment we don't support concurrent scoped filesystems.
|
|
|
|
|
var t = taskHelper.ExecuteBackgroundTask(() =>
|
2017-05-12 14:49:44 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
// not ok to create a 'scoped filesystems' other scope
|
|
|
|
|
// because at the moment we don't support concurrent scoped filesystems
|
|
|
|
|
// even a detached one
|
|
|
|
|
// we will get a "Already shadowing." exception.
|
|
|
|
|
Assert.Throws<InvalidOperationException>(() =>
|
2017-05-12 14:49:44 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
using var other = scopeProvider.CreateDetachedScope(scopeFileSystems: true);
|
2021-03-03 13:43:27 +11:00
|
|
|
});
|
|
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
return Task.CompletedTask;
|
|
|
|
|
});
|
2021-03-03 13:43:27 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Task.WaitAll(t);
|
2017-05-12 14:49:44 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var detached = scopeProvider.CreateDetachedScope(scopeFileSystems: true);
|
2017-05-12 14:49:44 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.IsNull(scopeProvider.AmbientScope);
|
2017-05-12 14:49:44 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.Throws<InvalidOperationException>(() =>
|
|
|
|
|
{
|
|
|
|
|
// even if there is no ambient scope, there's a single shadow
|
|
|
|
|
using var other = scopeProvider.CreateScope(scopeFileSystems: true);
|
|
|
|
|
});
|
2017-05-12 14:49:44 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
scopeProvider.AttachScope(detached);
|
|
|
|
|
detached.Dispose();
|
2017-05-12 14:49:44 +02:00
|
|
|
}
|
|
|
|
|
}
|