Files
OldThink/Content.IntegrationTests/ContentIntegrationTest.cs

200 lines
7.0 KiB
C#
Raw Normal View History

2020-06-12 12:45:01 +02:00
using System;
using System.Collections.Generic;
using System.IO;
2020-06-12 12:45:01 +02:00
using System.Threading.Tasks;
2019-06-29 01:58:16 +02:00
using Content.Client;
using Content.Client.Interfaces.Parallax;
using Content.Server;
using Content.Server.Interfaces.GameTicking;
2020-08-20 19:23:16 +02:00
using NUnit.Framework;
using Robust.Server.Interfaces.Maps;
using Robust.Server.Interfaces.Timing;
2019-06-29 01:58:16 +02:00
using Robust.Shared.ContentPack;
using Robust.Shared.Interfaces.Map;
2020-06-12 12:45:01 +02:00
using Robust.Shared.Interfaces.Network;
2019-06-29 01:58:16 +02:00
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
2019-06-29 01:58:16 +02:00
using Robust.UnitTesting;
using EntryPoint = Content.Client.EntryPoint;
2019-06-29 01:58:16 +02:00
namespace Content.IntegrationTests
{
2020-08-20 19:23:16 +02:00
[Parallelizable(ParallelScope.All)]
2019-06-29 01:58:16 +02:00
public abstract class ContentIntegrationTest : RobustIntegrationTest
{
2020-06-12 12:45:01 +02:00
protected sealed override ClientIntegrationInstance StartClient(ClientIntegrationOptions options = null)
2019-06-29 01:58:16 +02:00
{
options ??= new ClientIntegrationOptions();
2020-06-12 12:45:01 +02:00
// ReSharper disable once RedundantNameQualifier
options.ClientContentAssembly = typeof(EntryPoint).Assembly;
options.SharedContentAssembly = typeof(Shared.EntryPoint).Assembly;
2019-06-29 01:58:16 +02:00
options.BeforeStart += () =>
{
IoCManager.Resolve<IModLoader>().SetModuleBaseCallbacks(new ClientModuleTestingCallbacks
{
ClientBeforeIoC = () =>
{
2020-06-12 12:45:01 +02:00
if (options is ClientContentIntegrationOption contentOptions)
{
contentOptions.ContentBeforeIoC?.Invoke();
}
2019-06-29 01:58:16 +02:00
IoCManager.Register<IParallaxManager, DummyParallaxManager>(true);
}
});
};
2020-06-12 12:45:01 +02:00
// Connecting to Discord is a massive waste of time.
// Basically just makes the CI logs a mess.
options.CVarOverrides["discord.enabled"] = "true";
2019-06-29 01:58:16 +02:00
return base.StartClient(options);
}
protected override ServerIntegrationInstance StartServer(ServerIntegrationOptions options = null)
{
options ??= new ServerIntegrationOptions();
options.ServerContentAssembly = typeof(Server.EntryPoint).Assembly;
options.SharedContentAssembly = typeof(Shared.EntryPoint).Assembly;
return base.StartServer(options);
}
protected ServerIntegrationInstance StartServerDummyTicker(ServerIntegrationOptions options = null)
{
options ??= new ServerIntegrationOptions();
options.BeforeStart += () =>
{
IoCManager.Resolve<IModLoader>().SetModuleBaseCallbacks(new ServerModuleTestingCallbacks
{
ServerBeforeIoC = () =>
{
2020-06-12 12:45:01 +02:00
if (options is ServerContentIntegrationOption contentOptions)
{
contentOptions.ContentBeforeIoC?.Invoke();
}
IoCManager.Register<IGameTicker, DummyGameTicker>(true);
}
});
};
return StartServer(options);
}
2020-06-12 12:45:01 +02:00
protected async Task<(ClientIntegrationInstance client, ServerIntegrationInstance server)> StartConnectedServerClientPair(ClientIntegrationOptions clientOptions = null, ServerIntegrationOptions serverOptions = null)
2020-07-17 11:25:11 +02:00
{
var client = StartClient(clientOptions);
var server = StartServer(serverOptions);
await StartConnectedPairShared(client, server);
return (client, server);
}
protected async Task<(ClientIntegrationInstance client, ServerIntegrationInstance server)> StartConnectedServerDummyTickerClientPair(ClientIntegrationOptions clientOptions = null, ServerIntegrationOptions serverOptions = null)
2020-06-12 12:45:01 +02:00
{
var client = StartClient(clientOptions);
var server = StartServerDummyTicker(serverOptions);
2020-07-17 11:25:11 +02:00
await StartConnectedPairShared(client, server);
return (client, server);
}
protected async Task<IMapGrid> InitializeMap(ServerIntegrationInstance server, string mapPath)
{
await server.WaitIdleAsync();
var mapManager = server.ResolveDependency<IMapManager>();
var pauseManager = server.ResolveDependency<IPauseManager>();
var mapLoader = server.ResolveDependency<IMapLoader>();
IMapGrid grid = null;
server.Post(() =>
{
var mapId = mapManager.CreateMap();
pauseManager.AddUninitializedMap(mapId);
grid = mapLoader.LoadBlueprint(mapId, mapPath);
pauseManager.DoMapInitialize(mapId);
});
await server.WaitIdleAsync();
return grid;
}
protected async Task TryLoadEntities(IntegrationInstance instance, params string[] yamls)
{
await instance.WaitIdleAsync();
var prototypeManager = instance.ResolveDependency<IPrototypeManager>();
instance.Post(() =>
{
foreach (var yaml in yamls)
{
using var reader = new StringReader(yaml);
prototypeManager.LoadFromStream(reader);
}
});
await instance.WaitIdleAsync();
}
protected async Task WaitUntil(IntegrationInstance instance, Func<IntegrationInstance, bool> predicate, int tickStep = 10, int maxTicks = 600)
{
var ticksAwaited = 0;
while (!predicate(instance) && ticksAwaited < maxTicks)
{
await instance.WaitIdleAsync();
instance.RunTicks(tickStep);
ticksAwaited += tickStep;
}
await instance.WaitIdleAsync();
}
2020-07-17 11:25:11 +02:00
private static async Task StartConnectedPairShared(ClientIntegrationInstance client, ServerIntegrationInstance server)
{
2020-06-12 12:45:01 +02:00
await Task.WhenAll(client.WaitIdleAsync(), server.WaitIdleAsync());
client.SetConnectTarget(server);
client.Post(() => IoCManager.Resolve<IClientNetManager>().ClientConnect(null!, 0, null!));
2020-06-12 12:45:01 +02:00
await RunTicksSync(client, server, 10);
}
/// <summary>
/// Runs <paramref name="ticks"/> ticks on both server and client while keeping their main loop in sync.
/// </summary>
protected static async Task RunTicksSync(ClientIntegrationInstance client, ServerIntegrationInstance server, int ticks)
{
for (var i = 0; i < ticks; i++)
{
await server.WaitRunTicks(1);
await client.WaitRunTicks(1);
}
}
protected sealed class ClientContentIntegrationOption : ClientIntegrationOptions
{
public Action ContentBeforeIoC { get; set; }
}
protected sealed class ServerContentIntegrationOption : ServerIntegrationOptions
{
public Action ContentBeforeIoC { get; set; }
}
2019-06-29 01:58:16 +02:00
}
}