Files
Umbraco-CMS/src/Umbraco.Tests/Scoping/ScopeFileSystemsTests.cs

173 lines
6.2 KiB
C#
Raw Normal View History

2017-05-12 14:49:44 +02:00
using System;
using System.IO;
using System.Text;
using NUnit.Framework;
using Umbraco.Core;
2017-05-30 15:46:25 +02:00
using Umbraco.Core.Composing;
2017-05-12 14:49:44 +02:00
using Umbraco.Core.IO;
using Umbraco.Tests.TestHelpers;
using Umbraco.Tests.Testing;
2019-02-14 09:15:47 +01:00
using Umbraco.Core.Composing.CompositionExtensions;
using FileSystems = Umbraco.Core.IO.FileSystems;
2017-05-12 14:49:44 +02:00
namespace Umbraco.Tests.Scoping
{
[TestFixture]
[UmbracoTest(Database = UmbracoTestOptions.Database.NewEmptyPerTest)]
public class ScopeFileSystemsTests : TestWithDatabaseBase
{
public override void SetUp()
{
base.SetUp();
SafeCallContext.Clear();
ClearFiles();
}
2018-07-23 08:56:08 +02:00
protected override void ComposeApplication(bool withApplication)
{
base.ComposeApplication(withApplication);
if (!withApplication) return;
// re-register with actual media fs
2018-11-27 10:37:33 +01:00
Composition.ComposeFileSystems();
2018-07-23 08:56:08 +02:00
}
2017-05-12 14:49:44 +02:00
public override void TearDown()
{
base.TearDown();
SafeCallContext.Clear();
2018-11-19 14:40:59 +01:00
FileSystems.ResetShadowId();
2017-05-12 14:49:44 +02:00
ClearFiles();
}
private static void ClearFiles()
{
TestHelper.DeleteDirectory(Current.IOHelper.MapPath("media"));
TestHelper.DeleteDirectory(Current.IOHelper.MapPath("FileSysTests"));
TestHelper.DeleteDirectory(Current.IOHelper.MapPath(Constants.SystemDirectories.TempData.EnsureEndsWith('/') + "ShadowFs"));
2017-05-12 14:49:44 +02:00
}
[TestCase(true)]
[TestCase(false)]
public void CreateMediaTest(bool complete)
{
var physMediaFileSystem = new PhysicalFileSystem(Current.IOHelper.MapPath("media"), "ignore");
2018-11-19 14:40:59 +01:00
var mediaFileSystem = Current.MediaFileSystem;
2017-05-12 14:49:44 +02:00
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
var scopeProvider = ScopeProvider;
using (var scope = scopeProvider.CreateScope(scopeFileSystems: true))
{
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes("foo")))
mediaFileSystem.AddFile("f1.txt", ms);
Assert.IsTrue(mediaFileSystem.FileExists("f1.txt"));
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
if (complete)
scope.Complete();
Assert.IsTrue(mediaFileSystem.FileExists("f1.txt"));
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
}
if (complete)
{
2018-11-19 14:40:59 +01:00
Assert.IsTrue(Current.MediaFileSystem.FileExists("f1.txt"));
2017-05-12 14:49:44 +02:00
Assert.IsTrue(physMediaFileSystem.FileExists("f1.txt"));
}
else
{
2018-11-19 14:40:59 +01:00
Assert.IsFalse(Current.MediaFileSystem.FileExists("f1.txt"));
2017-05-12 14:49:44 +02:00
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
}
}
[Test]
public void MultiThread()
{
var physMediaFileSystem = new PhysicalFileSystem(Current.IOHelper.MapPath("media"), "ignore");
2018-11-19 14:40:59 +01:00
var mediaFileSystem = Current.MediaFileSystem;
2017-05-12 14:49:44 +02:00
var scopeProvider = ScopeProvider;
using (var scope = scopeProvider.CreateScope(scopeFileSystems: true))
{
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes("foo")))
mediaFileSystem.AddFile("f1.txt", ms);
Assert.IsTrue(mediaFileSystem.FileExists("f1.txt"));
Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt"));
using (new SafeCallContext())
{
Assert.IsFalse(mediaFileSystem.FileExists("f1.txt"));
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes("foo")))
mediaFileSystem.AddFile("f2.txt", ms);
Assert.IsTrue(mediaFileSystem.FileExists("f2.txt"));
Assert.IsTrue(physMediaFileSystem.FileExists("f2.txt"));
}
Assert.IsTrue(mediaFileSystem.FileExists("f2.txt"));
Assert.IsTrue(physMediaFileSystem.FileExists("f2.txt"));
}
}
[Test]
public void SingleShadow()
{
var scopeProvider = ScopeProvider;
using (var scope = scopeProvider.CreateScope(scopeFileSystems: true))
{
using (new SafeCallContext()) // not nesting!
{
// ok to create a 'normal' other scope
using (var other = scopeProvider.CreateScope())
{
other.Complete();
}
// not ok to create a 'scoped filesystems' other scope
// because at the moment we don't support concurrent scoped filesystems
Assert.Throws<InvalidOperationException>(() =>
{
var other = scopeProvider.CreateScope(scopeFileSystems: true);
});
}
}
}
[Test]
public void SingleShadowEvenDetached()
{
2017-05-30 19:41:37 +02:00
var scopeProvider = ScopeProvider;
2017-05-12 14:49:44 +02:00
using (var scope = scopeProvider.CreateScope(scopeFileSystems: true))
{
using (new SafeCallContext()) // not nesting!
{
// not ok to create a 'scoped filesystems' other scope
// because at the moment we don't support concurrent scoped filesystems
// even a detached one
Assert.Throws<InvalidOperationException>(() =>
{
var other = scopeProvider.CreateDetachedScope(scopeFileSystems: true);
});
}
}
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();
}
}
}