Files
Umbraco-CMS/src/Umbraco.Tests/Services/ThreadSafetyServiceTest.cs

237 lines
8.8 KiB
C#
Raw Normal View History

2017-07-20 11:21:28 +02:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Models;
2017-09-19 15:51:47 +02:00
using Umbraco.Core.Scoping;
using Umbraco.Core.Services;
2017-12-28 09:18:09 +01:00
using Umbraco.Core.Services.Implement;
using Umbraco.Tests.TestHelpers;
using Umbraco.Tests.TestHelpers.Entities;
2016-12-16 10:40:14 +01:00
using Umbraco.Tests.Testing;
namespace Umbraco.Tests.Services
{
2017-06-28 16:31:00 +02:00
// these tests tend to fail from time to time esp. on VSTS
//
// read
// Lock Time-out: https://technet.microsoft.com/en-us/library/ms172402.aspx?f=255&MSPPError=-2147217396
// http://support.x-tensive.com/question/5242/strange-locking-exceptions-with-sqlserverce
// http://debuggingblog.com/wp/2009/05/07/high-cpu-usage-and-windows-forms-application-hang-with-sqlce-database-and-the-sqlcelocktimeoutexception/
//
// tried to increase it via connection string or via SET LOCK_TIMEOUT
// but still, the test fails on VSTS in most cases, so now ignoring it,
// as I could not figure out _why_ and it does not look like we are
// causing it, getting into __sysObjects locks, no idea why
2018-03-30 19:31:42 +02:00
[TestFixture]
[Apartment(ApartmentState.STA)]
2016-11-05 19:23:55 +01:00
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
public class ThreadSafetyServiceTest : TestWithDatabaseBase
2017-07-20 11:21:28 +02:00
{
public override void SetUp()
{
base.SetUp();
CreateTestData();
}
2017-06-28 16:31:00 +02:00
// not sure this is doing anything really
protected override string GetDbConnectionString()
{
// need a longer timeout for tests?
return base.GetDbConnectionString() + "default lock timeout=60000;";
}
private const int MaxThreadCount = 20;
2017-06-28 16:31:00 +02:00
private void Save(ContentService service, IContent content)
2017-07-20 11:21:28 +02:00
{
using (var scope = ScopeProvider.CreateScope())
{
scope.Database.Execute("SET LOCK_TIMEOUT 60000");
service.Save(content);
scope.Complete();
}
2017-06-28 16:31:00 +02:00
}
2017-07-20 11:21:28 +02:00
private void Save(MediaService service, IMedia media)
{
using (var scope = ScopeProvider.CreateScope())
{
scope.Database.Execute("SET LOCK_TIMEOUT 60000");
service.Save(media);
scope.Complete();
}
}
2017-06-28 16:31:00 +02:00
2017-07-20 11:21:28 +02:00
private ManualResetEventSlim TraceLocks()
{
var done = new ManualResetEventSlim(false);
2017-06-28 16:31:00 +02:00
// comment out to trace locks
2017-07-20 11:21:28 +02:00
return done;
2017-06-28 16:31:00 +02:00
2018-05-16 10:06:51 +02:00
//new Thread(() =>
//{
// using (var scope = ScopeProvider.CreateScope())
// while (done.IsSet == false)
// {
// var db = scope.Database;
// var info = db.Query<dynamic>("SELECT * FROM sys.lock_information;");
// Console.WriteLine("LOCKS:");
// foreach (var row in info)
// {
// Console.WriteLine("> " + row.request_spid + " " + row.resource_type + " " + row.resource_description + " " + row.request_mode + " " + row.resource_table + " " + row.resource_table_id + " " + row.request_status);
// }
// Thread.Sleep(50);
// }
//}).Start();
//return done;
2017-07-20 11:21:28 +02:00
}
[Test]
public void Ensure_All_Threads_Execute_Successfully_Content_Service()
{
2017-06-28 16:31:00 +02:00
if (Environment.GetEnvironmentVariable("UMBRACO_TMP") != null)
Assert.Ignore("Do not run on VSTS.");
2017-07-20 11:21:28 +02:00
// the ServiceContext in that each repository in a service (i.e. ContentService) is a singleton
var contentService = (ContentService)ServiceContext.ContentService;
2016-04-12 19:55:50 +02:00
2017-07-20 11:21:28 +02:00
var threads = new List<Thread>();
var exceptions = new List<Exception>();
2016-04-12 19:55:50 +02:00
2017-05-12 14:49:44 +02:00
Debug.WriteLine("Starting...");
2017-06-28 16:31:00 +02:00
var done = TraceLocks();
2016-10-13 21:08:07 +02:00
for (var i = 0; i < MaxThreadCount; i++)
2017-07-20 11:21:28 +02:00
{
var t = new Thread(() =>
{
2017-06-28 16:31:00 +02:00
try
{
Debug.WriteLine("[{0}] Running...", Thread.CurrentThread.ManagedThreadId);
var name1 = "test-" + Guid.NewGuid();
2017-11-30 13:56:29 +01:00
var content1 = contentService.Create(name1, -1, "umbTextpage");
2017-06-28 16:31:00 +02:00
Debug.WriteLine("[{0}] Saving content #1.", Thread.CurrentThread.ManagedThreadId);
Save(contentService, content1);
Thread.Sleep(100); //quick pause for maximum overlap!
var name2 = "test-" + Guid.NewGuid();
2017-11-30 13:56:29 +01:00
var content2 = contentService.Create(name2, -1, "umbTextpage");
2017-06-28 16:31:00 +02:00
Debug.WriteLine("[{0}] Saving content #2.", Thread.CurrentThread.ManagedThreadId);
Save(contentService, content2);
}
catch (Exception e)
{
lock (exceptions) { exceptions.Add(e); }
2016-10-13 21:08:07 +02:00
}
2016-12-14 18:40:16 +01:00
});
2017-07-20 11:21:28 +02:00
threads.Add(t);
}
2016-10-13 21:08:07 +02:00
// start all threads
Debug.WriteLine("Starting threads");
threads.ForEach(x => x.Start());
2016-10-13 21:08:07 +02:00
// wait for all to complete
Debug.WriteLine("Joining threads");
threads.ForEach(x => x.Join());
2017-07-20 11:21:28 +02:00
done.Set();
2016-10-13 21:08:07 +02:00
Debug.WriteLine("Checking exceptions");
if (exceptions.Count == 0)
2017-07-20 11:21:28 +02:00
{
//now look up all items, there should be 40!
var items = contentService.GetRootContent();
Assert.AreEqual(2 * MaxThreadCount, items.Count());
}
else
{
throw new Exception("Exceptions!", exceptions.First()); // rethrow the first one...
}
}
[Test]
public void Ensure_All_Threads_Execute_Successfully_Media_Service()
{
2017-06-28 16:31:00 +02:00
if (Environment.GetEnvironmentVariable("UMBRACO_TMP") != null)
Assert.Ignore("Do not run on VSTS.");
2017-07-20 11:21:28 +02:00
// mimick the ServiceContext in that each repository in a service (i.e. ContentService) is a singleton
var mediaService = (MediaService)ServiceContext.MediaService;
2017-07-20 11:21:28 +02:00
var threads = new List<Thread>();
var exceptions = new List<Exception>();
2017-05-12 14:49:44 +02:00
Debug.WriteLine("Starting...");
2017-06-28 16:31:00 +02:00
var done = TraceLocks();
2017-07-20 11:21:28 +02:00
for (var i = 0; i < MaxThreadCount; i++)
{
var t = new Thread(() =>
{
2017-06-28 16:31:00 +02:00
try
{
Debug.WriteLine("[{0}] Running...", Thread.CurrentThread.ManagedThreadId);
var name1 = "test-" + Guid.NewGuid();
var media1 = mediaService.CreateMedia(name1, -1, Constants.Conventions.MediaTypes.Folder);
Debug.WriteLine("[{0}] Saving media #1.", Thread.CurrentThread.ManagedThreadId);
Save(mediaService, media1);
Thread.Sleep(100); //quick pause for maximum overlap!
var name2 = "test-" + Guid.NewGuid();
var media2 = mediaService.CreateMedia(name2, -1, Constants.Conventions.MediaTypes.Folder);
Debug.WriteLine("[{0}] Saving media #2.", Thread.CurrentThread.ManagedThreadId);
Save(mediaService, media2);
}
catch (Exception e)
{
lock (exceptions) { exceptions.Add(e); }
}
2016-12-14 18:40:16 +01:00
});
2017-07-20 11:21:28 +02:00
threads.Add(t);
}
2016-12-14 18:40:16 +01:00
//start all threads
threads.ForEach(x => x.Start());
2016-12-14 18:40:16 +01:00
//wait for all to complete
threads.ForEach(x => x.Join());
2017-06-28 16:31:00 +02:00
done.Set();
2017-07-20 11:21:28 +02:00
if (exceptions.Count == 0)
{
// now look up all items, there should be 40!
var items = mediaService.GetRootMedia();
Assert.AreEqual(2 * MaxThreadCount, items.Count());
}
else
{
throw new Exception("Exceptions!", exceptions.First()); // rethrow the first one...
}
2017-07-20 11:21:28 +02:00
}
2016-04-12 19:55:50 +02:00
2017-07-20 11:21:28 +02:00
public void CreateTestData()
{
// Create and Save ContentType "umbTextpage" -> 1045
var contentType = MockedContentTypes.CreateSimpleContentType("umbTextpage", "Textpage");
contentType.Key = new Guid("1D3A8E6E-2EA9-4CC1-B229-1AEE19821522");
ServiceContext.ContentTypeService.Save(contentType);
}
2017-05-12 14:49:44 +02:00
}
2017-07-20 11:21:28 +02:00
}