Files
RobustToolbox/Robust.UnitTesting/RobustUnitTest.cs
Javier Guardia Fernández 04d029b9a2 Add test pooling (#2146)
* Add test pooling and global test setup

* WIP test pooling changes

* Make asynchronous tests the default again

* Finish fixing tests, make test threads background threads

* Un-pool tests with custom cvars

* Fix not changing FailureLogLevel cvar on instance return

* Fix error when overriding already overriden cvar

* Don't pool some physics integration tests

* Unpool engine tests, the technology just isn't there yet

* Remove explicit Pool = false from physics tests

* Change default pooling setting to false

* Didn't need this anyway

* Remove ConfigurationManager.ResetOverrides

* Bring back enum cvar override parsing

* Make integrationInstances name clearer > notPooledInstaces
Make clients ready and servers ready internal

* Add logging test instances

* Give more info on ran tests

* Show total clients and servers in test output

* Wipe LayerMap on SpriteComponent.AfterDeserialize

* Fix server not properly kicking clients

* Rider shut

* Make all test metrics report totals

* Format tests ran better

* Replace Console.WriteLine with TestContext.Out.WriteLine

* Fix two server test pooling info prints using total clients instead of total servers

Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
2021-11-06 11:18:18 +01:00

144 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NUnit.Framework;
using Robust.Server.GameObjects;
using Robust.Server.Physics;
using Robust.Shared;
using Robust.Shared.Configuration;
using Robust.Shared.ContentPack;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Dynamics;
using Robust.Shared.Reflection;
using Robust.Shared.Utility;
using GridFixtureSystem = Robust.Client.GameObjects.GridFixtureSystem;
namespace Robust.UnitTesting
{
public enum UnitTestProject : byte
{
Server,
Client
}
[Parallelizable]
public abstract partial class RobustUnitTest
{
public virtual UnitTestProject Project => UnitTestProject.Server;
[OneTimeSetUp]
public void BaseSetup()
{
// Clear state across tests.
IoCManager.InitThread();
IoCManager.Clear();
RegisterIoC();
var assemblies = new List<Assembly>(4);
switch (Project)
{
case UnitTestProject.Client:
assemblies.Add(AppDomain.CurrentDomain.GetAssemblyByName("Robust.Client"));
break;
case UnitTestProject.Server:
assemblies.Add(AppDomain.CurrentDomain.GetAssemblyByName("Robust.Server"));
break;
default:
throw new NotSupportedException($"Unknown testing project: {Project}");
}
assemblies.Add(AppDomain.CurrentDomain.GetAssemblyByName("Robust.Shared"));
assemblies.Add(Assembly.GetExecutingAssembly());
var configurationManager = IoCManager.Resolve<IConfigurationManagerInternal>();
configurationManager.Initialize(Project == UnitTestProject.Server);
foreach (var assembly in assemblies)
{
configurationManager.LoadCVarsFromAssembly(assembly);
}
var contentAssemblies = GetContentAssemblies();
foreach (var assembly in contentAssemblies)
{
configurationManager.LoadCVarsFromAssembly(assembly);
}
configurationManager.LoadCVarsFromAssembly(typeof(RobustUnitTest).Assembly);
// Required systems
var systems = IoCManager.Resolve<IEntitySystemManager>();
systems.Initialize();
var entMan = IoCManager.Resolve<IEntityManager>();
if(entMan.EventBus == null)
{
entMan.Initialize();
entMan.Startup();
}
IoCManager.Resolve<IEntityLookup>().Startup();
var mapMan = IoCManager.Resolve<IMapManager>();
mapMan.Initialize();
IoCManager.Resolve<IReflectionManager>().LoadAssemblies(assemblies);
var modLoader = IoCManager.Resolve<TestingModLoader>();
modLoader.Assemblies = contentAssemblies;
modLoader.TryLoadModulesFrom(ResourcePath.Root, "");
// Required components for the engine to work
var compFactory = IoCManager.Resolve<IComponentFactory>();
if (!compFactory.AllRegisteredTypes.Contains(typeof(MetaDataComponent)))
{
compFactory.RegisterClass<MetaDataComponent>();
}
if (!compFactory.AllRegisteredTypes.Contains(typeof(EntityLookupComponent)))
{
compFactory.RegisterClass<EntityLookupComponent>();
}
if (!compFactory.AllRegisteredTypes.Contains(typeof(SharedPhysicsMapComponent)))
{
compFactory.RegisterClass<PhysicsMapComponent>();
}
if(entMan.EventBus == null)
{
entMan.Startup();
}
mapMan.Startup();
}
[OneTimeTearDown]
public void BaseTearDown()
{
IoCManager.Clear();
}
/// <summary>
/// Called after all IoC registration has been done, but before the graph has been built.
/// This allows one to add new IoC types or overwrite existing ones if needed.
/// </summary>
protected virtual void OverrideIoC()
{
}
protected virtual Assembly[] GetContentAssemblies()
{
return Array.Empty<Assembly>();
}
}
}