V10: fix build warnings in test projects (#12509)
* Run code cleanup * Dotnet format benchmarks project * Fix up Test.Common * Run dotnet format + manual cleanup * Run code cleanup for unit tests * Run dotnet format * Fix up errors * Manual cleanup of Unit test project * Update tests/Umbraco.Tests.Benchmarks/HexStringBenchmarks.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update tests/Umbraco.Tests.Integration/Testing/TestDbMeta.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update tests/Umbraco.Tests.Benchmarks/TypeFinderBenchmarks.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update tests/Umbraco.Tests.Integration/Umbraco.Core/Events/EventAggregatorTests.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Fix according to review * Fix after merge * Fix errors Co-authored-by: Nikolaj Geisle <niko737@edu.ucl.dk> Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> Co-authored-by: Zeegaan <nge@umbraco.dk>
This commit is contained in:
@@ -4,228 +4,219 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Cms.Core;
|
||||
using Umbraco.Cms.Core.Hosting;
|
||||
using Umbraco.Cms.Core.IO;
|
||||
using Umbraco.Cms.Core.Scoping;
|
||||
using Umbraco.Cms.Infrastructure.Scoping;
|
||||
using Umbraco.Cms.Tests.Common;
|
||||
using Umbraco.Cms.Tests.Common.Testing;
|
||||
using Umbraco.Cms.Tests.Integration.Testing;
|
||||
using Umbraco.Extensions;
|
||||
using Constants = Umbraco.Cms.Core.Constants;
|
||||
using FileSystems = Umbraco.Cms.Core.IO.FileSystems;
|
||||
|
||||
using IScopeProvider = Umbraco.Cms.Infrastructure.Scoping.IScopeProvider;
|
||||
using IScope = Umbraco.Cms.Infrastructure.Scoping.IScope;
|
||||
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Scoping;
|
||||
|
||||
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Scoping
|
||||
[TestFixture]
|
||||
[UmbracoTest(Database = UmbracoTestOptions.Database.NewEmptyPerTest)]
|
||||
public class ScopeFileSystemsTests : UmbracoIntegrationTest
|
||||
{
|
||||
[TestFixture]
|
||||
[UmbracoTest(Database = UmbracoTestOptions.Database.NewEmptyPerTest)]
|
||||
public class ScopeFileSystemsTests : UmbracoIntegrationTest
|
||||
[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)
|
||||
{
|
||||
private MediaFileManager MediaFileManager => GetRequiredService<MediaFileManager>();
|
||||
TestHelper.DeleteDirectory(ioHelper.MapPath("media"));
|
||||
TestHelper.DeleteDirectory(ioHelper.MapPath("FileSysTests"));
|
||||
TestHelper.DeleteDirectory(ioHelper.MapPath(Constants.SystemDirectories.TempData.EnsureEndsWith('/') +
|
||||
"ShadowFs"));
|
||||
}
|
||||
|
||||
private IHostingEnvironment HostingEnvironment => GetRequiredService<IHostingEnvironment>();
|
||||
[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;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp() => ClearFiles(IOHelper);
|
||||
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
|
||||
|
||||
[TearDown]
|
||||
public void Teardown()
|
||||
using (ScopeProvider.CreateScope(scopeFileSystems: true))
|
||||
{
|
||||
ClearFiles(IOHelper);
|
||||
}
|
||||
|
||||
private void ClearFiles(IIOHelper ioHelper)
|
||||
{
|
||||
TestHelper.DeleteDirectory(ioHelper.MapPath("media"));
|
||||
TestHelper.DeleteDirectory(ioHelper.MapPath("FileSysTests"));
|
||||
TestHelper.DeleteDirectory(ioHelper.MapPath(Constants.SystemDirectories.TempData.EnsureEndsWith('/') + "ShadowFs"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MediaFileManager_does_not_write_to_physical_file_system_when_scoped_if_scope_does_not_complete()
|
||||
{
|
||||
string rootPath = HostingEnvironment.MapPathWebRoot(GlobalSettings.UmbracoMediaPhysicalRootPath);
|
||||
string rootUrl = HostingEnvironment.ToAbsolute(GlobalSettings.UmbracoMediaPath);
|
||||
var physMediaFileSystem = new PhysicalFileSystem(IOHelper, HostingEnvironment, GetRequiredService<ILogger<PhysicalFileSystem>>(), rootPath, rootUrl);
|
||||
MediaFileManager mediaFileManager = MediaFileManager;
|
||||
|
||||
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
|
||||
|
||||
using (ScopeProvider.CreateScope(scopeFileSystems: true))
|
||||
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes("foo")))
|
||||
{
|
||||
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes("foo")))
|
||||
{
|
||||
MediaFileManager.FileSystem.AddFile("f1.txt", ms);
|
||||
}
|
||||
|
||||
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"));
|
||||
MediaFileManager.FileSystem.AddFile("f1.txt", ms);
|
||||
}
|
||||
|
||||
// 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"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MediaFileManager_writes_to_physical_file_system_when_scoped_and_scope_is_completed()
|
||||
{
|
||||
string rootPath = HostingEnvironment.MapPathWebRoot(GlobalSettings.UmbracoMediaPhysicalRootPath);
|
||||
string rootUrl = HostingEnvironment.ToAbsolute(GlobalSettings.UmbracoMediaPath);
|
||||
var physMediaFileSystem = new PhysicalFileSystem(IOHelper, HostingEnvironment, GetRequiredService<ILogger<PhysicalFileSystem>>(), rootPath, rootUrl);
|
||||
MediaFileManager mediaFileManager = MediaFileManager;
|
||||
|
||||
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
|
||||
|
||||
using (IScope scope = ScopeProvider.CreateScope(scopeFileSystems: true))
|
||||
{
|
||||
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes("foo")))
|
||||
{
|
||||
mediaFileManager.FileSystem.AddFile("f1.txt", ms);
|
||||
}
|
||||
|
||||
Assert.IsTrue(mediaFileManager.FileSystem.FileExists("f1.txt"));
|
||||
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
|
||||
|
||||
scope.Complete();
|
||||
|
||||
Assert.IsTrue(mediaFileManager.FileSystem.FileExists("f1.txt"));
|
||||
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
|
||||
}
|
||||
|
||||
// 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"));
|
||||
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
|
||||
|
||||
Assert.IsTrue(mediaFileManager.FileSystem.FileExists("f1.txt"));
|
||||
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MultiThread()
|
||||
{
|
||||
string rootPath = HostingEnvironment.MapPathWebRoot(GlobalSettings.UmbracoMediaPhysicalRootPath);
|
||||
string rootUrl = HostingEnvironment.ToAbsolute(GlobalSettings.UmbracoMediaPath);
|
||||
var physMediaFileSystem = new PhysicalFileSystem(IOHelper, HostingEnvironment, GetRequiredService<ILogger<PhysicalFileSystem>>(), rootPath, rootUrl);
|
||||
MediaFileManager mediaFileManager = MediaFileManager;
|
||||
var taskHelper = new TaskHelper(Mock.Of<ILogger<TaskHelper>>());
|
||||
// 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"));
|
||||
}
|
||||
|
||||
IScopeProvider scopeProvider = ScopeProvider;
|
||||
using (IScope scope = scopeProvider.CreateScope(scopeFileSystems: true))
|
||||
[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"));
|
||||
|
||||
using (var scope = ScopeProvider.CreateScope(scopeFileSystems: true))
|
||||
{
|
||||
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes("foo")))
|
||||
{
|
||||
mediaFileManager.FileSystem.AddFile("f1.txt", ms);
|
||||
}
|
||||
|
||||
Assert.IsTrue(mediaFileManager.FileSystem.FileExists("f1.txt"));
|
||||
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
|
||||
|
||||
scope.Complete();
|
||||
|
||||
Assert.IsTrue(mediaFileManager.FileSystem.FileExists("f1.txt"));
|
||||
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
|
||||
}
|
||||
|
||||
// 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))
|
||||
{
|
||||
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes("foo")))
|
||||
{
|
||||
mediaFileManager.FileSystem.AddFile("f1.txt", ms);
|
||||
}
|
||||
|
||||
Assert.IsTrue(mediaFileManager.FileSystem.FileExists("f1.txt"));
|
||||
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
|
||||
|
||||
// execute on another disconnected thread (execution context will not flow)
|
||||
var t = taskHelper.ExecuteBackgroundTask(() =>
|
||||
{
|
||||
Assert.IsFalse(mediaFileManager.FileSystem.FileExists("f1.txt"));
|
||||
|
||||
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes("foo")))
|
||||
{
|
||||
mediaFileManager.FileSystem.AddFile("f1.txt", ms);
|
||||
mediaFileManager.FileSystem.AddFile("f2.txt", ms);
|
||||
}
|
||||
|
||||
Assert.IsTrue(mediaFileManager.FileSystem.FileExists("f1.txt"));
|
||||
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
|
||||
|
||||
// execute on another disconnected thread (execution context will not flow)
|
||||
Task t = taskHelper.ExecuteBackgroundTask(() =>
|
||||
{
|
||||
Assert.IsFalse(mediaFileManager.FileSystem.FileExists("f1.txt"));
|
||||
|
||||
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes("foo")))
|
||||
{
|
||||
mediaFileManager.FileSystem.AddFile("f2.txt", ms);
|
||||
}
|
||||
|
||||
Assert.IsTrue(mediaFileManager.FileSystem.FileExists("f2.txt"));
|
||||
Assert.IsTrue(physMediaFileSystem.FileExists("f2.txt"));
|
||||
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
Task.WaitAll(t);
|
||||
|
||||
Assert.IsTrue(mediaFileManager.FileSystem.FileExists("f2.txt"));
|
||||
Assert.IsTrue(physMediaFileSystem.FileExists("f2.txt"));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SingleShadow()
|
||||
{
|
||||
var taskHelper = new TaskHelper(Mock.Of<ILogger<TaskHelper>>());
|
||||
IScopeProvider scopeProvider = ScopeProvider;
|
||||
bool isThrown = false;
|
||||
using (IScope scope = scopeProvider.CreateScope(scopeFileSystems: true))
|
||||
{
|
||||
// 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.
|
||||
Task t = taskHelper.ExecuteBackgroundTask(() =>
|
||||
{
|
||||
// ok to create a 'normal' other scope
|
||||
using (IScope other = scopeProvider.CreateScope())
|
||||
{
|
||||
other.Complete();
|
||||
}
|
||||
|
||||
// not ok to create a 'scoped filesystems' other scope
|
||||
// we will get a "Already shadowing." exception.
|
||||
Assert.Throws<InvalidOperationException>(() =>
|
||||
{
|
||||
using IScope other = scopeProvider.CreateScope(scopeFileSystems: true);
|
||||
});
|
||||
|
||||
isThrown = true;
|
||||
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
Task.WaitAll(t);
|
||||
}
|
||||
|
||||
Assert.IsTrue(isThrown);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SingleShadowEvenDetached()
|
||||
{
|
||||
var taskHelper = new TaskHelper(Mock.Of<ILogger<TaskHelper>>());
|
||||
var scopeProvider = (ScopeProvider)ScopeProvider;
|
||||
using (IScope scope = scopeProvider.CreateScope(scopeFileSystems: true))
|
||||
{
|
||||
// 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.
|
||||
Task t = taskHelper.ExecuteBackgroundTask(() =>
|
||||
{
|
||||
// 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>(() =>
|
||||
{
|
||||
using IScope other = scopeProvider.CreateDetachedScope(scopeFileSystems: true);
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
Task.WaitAll(t);
|
||||
}
|
||||
|
||||
IScope detached = scopeProvider.CreateDetachedScope(scopeFileSystems: true);
|
||||
|
||||
Assert.IsNull(scopeProvider.AmbientScope);
|
||||
|
||||
Assert.Throws<InvalidOperationException>(() =>
|
||||
{
|
||||
// even if there is no ambient scope, there's a single shadow
|
||||
using IScope other = scopeProvider.CreateScope(scopeFileSystems: true);
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
scopeProvider.AttachScope(detached);
|
||||
detached.Dispose();
|
||||
Task.WaitAll(t);
|
||||
|
||||
Assert.IsTrue(mediaFileManager.FileSystem.FileExists("f2.txt"));
|
||||
Assert.IsTrue(physMediaFileSystem.FileExists("f2.txt"));
|
||||
}
|
||||
}
|
||||
|
||||
[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))
|
||||
{
|
||||
// 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(() =>
|
||||
{
|
||||
// ok to create a 'normal' other scope
|
||||
using (var other = scopeProvider.CreateScope())
|
||||
{
|
||||
other.Complete();
|
||||
}
|
||||
|
||||
// not ok to create a 'scoped filesystems' other scope
|
||||
// we will get a "Already shadowing." exception.
|
||||
Assert.Throws<InvalidOperationException>(() =>
|
||||
{
|
||||
using var other = scopeProvider.CreateScope(scopeFileSystems: true);
|
||||
});
|
||||
|
||||
isThrown = true;
|
||||
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
Task.WaitAll(t);
|
||||
}
|
||||
|
||||
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))
|
||||
{
|
||||
// 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(() =>
|
||||
{
|
||||
// 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>(() =>
|
||||
{
|
||||
using var other = scopeProvider.CreateDetachedScope(scopeFileSystems: true);
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
Task.WaitAll(t);
|
||||
}
|
||||
|
||||
var detached = scopeProvider.CreateDetachedScope(scopeFileSystems: true);
|
||||
|
||||
Assert.IsNull(scopeProvider.AmbientScope);
|
||||
|
||||
Assert.Throws<InvalidOperationException>(() =>
|
||||
{
|
||||
// even if there is no ambient scope, there's a single shadow
|
||||
using var other = scopeProvider.CreateScope(scopeFileSystems: true);
|
||||
});
|
||||
|
||||
scopeProvider.AttachScope(detached);
|
||||
detached.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user