1
0

PoolManager.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. #nullable enable
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading;
  8. using Content.Client.IoC;
  9. using Content.Client.Parallax.Managers;
  10. using Content.IntegrationTests.Pair;
  11. using Content.IntegrationTests.Tests;
  12. using Content.IntegrationTests.Tests.Destructible;
  13. using Content.IntegrationTests.Tests.DeviceNetwork;
  14. using Content.IntegrationTests.Tests.Interaction.Click;
  15. using Robust.Client;
  16. using Robust.Server;
  17. using Robust.Shared.Configuration;
  18. using Robust.Shared.ContentPack;
  19. using Robust.Shared.GameObjects;
  20. using Robust.Shared.IoC;
  21. using Robust.Shared.Log;
  22. using Robust.Shared.Prototypes;
  23. using Robust.Shared.Timing;
  24. using Robust.UnitTesting;
  25. namespace Content.IntegrationTests;
  26. /// <summary>
  27. /// Making clients, and servers is slow, this manages a pool of them so tests can reuse them.
  28. /// </summary>
  29. public static partial class PoolManager
  30. {
  31. public const string TestMap = "Nomads";
  32. private static int _pairId;
  33. private static readonly object PairLock = new();
  34. private static bool _initialized;
  35. // Pair, IsBorrowed
  36. private static readonly Dictionary<TestPair, bool> Pairs = new();
  37. private static bool _dead;
  38. private static Exception? _poolFailureReason;
  39. private static HashSet<Assembly> _contentAssemblies = default!;
  40. public static async Task<(RobustIntegrationTest.ServerIntegrationInstance, PoolTestLogHandler)> GenerateServer(
  41. PoolSettings poolSettings,
  42. TextWriter testOut)
  43. {
  44. var options = new RobustIntegrationTest.ServerIntegrationOptions
  45. {
  46. ContentStart = true,
  47. Options = new ServerOptions()
  48. {
  49. LoadConfigAndUserData = false,
  50. LoadContentResources = !poolSettings.NoLoadContent,
  51. },
  52. ContentAssemblies = _contentAssemblies.ToArray()
  53. };
  54. var logHandler = new PoolTestLogHandler("SERVER");
  55. logHandler.ActivateContext(testOut);
  56. options.OverrideLogHandler = () => logHandler;
  57. options.BeforeStart += () =>
  58. {
  59. // Server-only systems (i.e., systems that subscribe to events with server-only components)
  60. var entSysMan = IoCManager.Resolve<IEntitySystemManager>();
  61. entSysMan.LoadExtraSystemType<DeviceNetworkTestSystem>();
  62. entSysMan.LoadExtraSystemType<TestDestructibleListenerSystem>();
  63. IoCManager.Resolve<ILogManager>().GetSawmill("loc").Level = LogLevel.Error;
  64. IoCManager.Resolve<IConfigurationManager>()
  65. .OnValueChanged(RTCVars.FailureLogLevel, value => logHandler.FailureLevel = value, true);
  66. };
  67. SetDefaultCVars(options);
  68. var server = new RobustIntegrationTest.ServerIntegrationInstance(options);
  69. await server.WaitIdleAsync();
  70. await SetupCVars(server, poolSettings);
  71. return (server, logHandler);
  72. }
  73. /// <summary>
  74. /// This shuts down the pool, and disposes all the server/client pairs.
  75. /// This is a one time operation to be used when the testing program is exiting.
  76. /// </summary>
  77. public static void Shutdown()
  78. {
  79. List<TestPair> localPairs;
  80. lock (PairLock)
  81. {
  82. if (_dead)
  83. return;
  84. _dead = true;
  85. localPairs = Pairs.Keys.ToList();
  86. }
  87. foreach (var pair in localPairs)
  88. {
  89. pair.Kill();
  90. }
  91. _initialized = false;
  92. }
  93. public static string DeathReport()
  94. {
  95. lock (PairLock)
  96. {
  97. var builder = new StringBuilder();
  98. var pairs = Pairs.Keys.OrderBy(pair => pair.Id);
  99. foreach (var pair in pairs)
  100. {
  101. var borrowed = Pairs[pair];
  102. builder.AppendLine($"Pair {pair.Id}, Tests Run: {pair.TestHistory.Count}, Borrowed: {borrowed}");
  103. for (var i = 0; i < pair.TestHistory.Count; i++)
  104. {
  105. builder.AppendLine($"#{i}: {pair.TestHistory[i]}");
  106. }
  107. }
  108. return builder.ToString();
  109. }
  110. }
  111. public static async Task<(RobustIntegrationTest.ClientIntegrationInstance, PoolTestLogHandler)> GenerateClient(
  112. PoolSettings poolSettings,
  113. TextWriter testOut)
  114. {
  115. var options = new RobustIntegrationTest.ClientIntegrationOptions
  116. {
  117. FailureLogLevel = LogLevel.Warning,
  118. ContentStart = true,
  119. ContentAssemblies = new[]
  120. {
  121. typeof(Shared.Entry.EntryPoint).Assembly,
  122. typeof(Client.Entry.EntryPoint).Assembly,
  123. typeof(PoolManager).Assembly,
  124. }
  125. };
  126. if (poolSettings.NoLoadContent)
  127. {
  128. Assert.Warn("NoLoadContent does not work on the client, ignoring");
  129. }
  130. options.Options = new GameControllerOptions()
  131. {
  132. LoadConfigAndUserData = false,
  133. // LoadContentResources = !poolSettings.NoLoadContent
  134. };
  135. var logHandler = new PoolTestLogHandler("CLIENT");
  136. logHandler.ActivateContext(testOut);
  137. options.OverrideLogHandler = () => logHandler;
  138. options.BeforeStart += () =>
  139. {
  140. IoCManager.Resolve<IModLoader>().SetModuleBaseCallbacks(new ClientModuleTestingCallbacks
  141. {
  142. ClientBeforeIoC = () =>
  143. {
  144. // do not register extra systems or components here -- they will get cleared when the client is
  145. // disconnected. just use reflection.
  146. IoCManager.Register<IParallaxManager, DummyParallaxManager>(true);
  147. IoCManager.Resolve<ILogManager>().GetSawmill("loc").Level = LogLevel.Error;
  148. IoCManager.Resolve<IConfigurationManager>()
  149. .OnValueChanged(RTCVars.FailureLogLevel, value => logHandler.FailureLevel = value, true);
  150. }
  151. });
  152. };
  153. SetDefaultCVars(options);
  154. var client = new RobustIntegrationTest.ClientIntegrationInstance(options);
  155. await client.WaitIdleAsync();
  156. await SetupCVars(client, poolSettings);
  157. return (client, logHandler);
  158. }
  159. /// <summary>
  160. /// Gets a <see cref="Pair.TestPair"/>, which can be used to get access to a server, and client <see cref="Pair.TestPair"/>
  161. /// </summary>
  162. /// <param name="poolSettings">See <see cref="PoolSettings"/></param>
  163. /// <returns></returns>
  164. public static async Task<TestPair> GetServerClient(PoolSettings? poolSettings = null)
  165. {
  166. return await GetServerClientPair(poolSettings ?? new PoolSettings());
  167. }
  168. private static string GetDefaultTestName(TestContext testContext)
  169. {
  170. return testContext.Test.FullName.Replace("Content.IntegrationTests.Tests.", "");
  171. }
  172. private static async Task<TestPair> GetServerClientPair(PoolSettings poolSettings)
  173. {
  174. if (!_initialized)
  175. throw new InvalidOperationException($"Pool manager has not been initialized");
  176. // Trust issues with the AsyncLocal that backs this.
  177. var testContext = TestContext.CurrentContext;
  178. var testOut = TestContext.Out;
  179. DieIfPoolFailure();
  180. var currentTestName = poolSettings.TestName ?? GetDefaultTestName(testContext);
  181. var poolRetrieveTimeWatch = new Stopwatch();
  182. await testOut.WriteLineAsync($"{nameof(GetServerClientPair)}: Called by test {currentTestName}");
  183. TestPair? pair = null;
  184. try
  185. {
  186. poolRetrieveTimeWatch.Start();
  187. if (poolSettings.MustBeNew)
  188. {
  189. await testOut.WriteLineAsync(
  190. $"{nameof(GetServerClientPair)}: Creating pair, because settings of pool settings");
  191. pair = await CreateServerClientPair(poolSettings, testOut);
  192. }
  193. else
  194. {
  195. await testOut.WriteLineAsync($"{nameof(GetServerClientPair)}: Looking in pool for a suitable pair");
  196. pair = GrabOptimalPair(poolSettings);
  197. if (pair != null)
  198. {
  199. pair.ActivateContext(testOut);
  200. await testOut.WriteLineAsync($"{nameof(GetServerClientPair)}: Suitable pair found");
  201. var canSkip = pair.Settings.CanFastRecycle(poolSettings);
  202. if (canSkip)
  203. {
  204. await testOut.WriteLineAsync($"{nameof(GetServerClientPair)}: Cleanup not needed, Skipping cleanup of pair");
  205. await SetupCVars(pair.Client, poolSettings);
  206. await SetupCVars(pair.Server, poolSettings);
  207. await pair.RunTicksSync(1);
  208. }
  209. else
  210. {
  211. await testOut.WriteLineAsync($"{nameof(GetServerClientPair)}: Cleaning existing pair");
  212. await pair.CleanPooledPair(poolSettings, testOut);
  213. }
  214. await pair.RunTicksSync(5);
  215. await pair.SyncTicks(targetDelta: 1);
  216. }
  217. else
  218. {
  219. await testOut.WriteLineAsync($"{nameof(GetServerClientPair)}: Creating a new pair, no suitable pair found in pool");
  220. pair = await CreateServerClientPair(poolSettings, testOut);
  221. }
  222. }
  223. }
  224. finally
  225. {
  226. if (pair != null && pair.TestHistory.Count > 0)
  227. {
  228. await testOut.WriteLineAsync($"{nameof(GetServerClientPair)}: Pair {pair.Id} Test History Start");
  229. for (var i = 0; i < pair.TestHistory.Count; i++)
  230. {
  231. await testOut.WriteLineAsync($"- Pair {pair.Id} Test #{i}: {pair.TestHistory[i]}");
  232. }
  233. await testOut.WriteLineAsync($"{nameof(GetServerClientPair)}: Pair {pair.Id} Test History End");
  234. }
  235. }
  236. pair.ValidateSettings(poolSettings);
  237. var poolRetrieveTime = poolRetrieveTimeWatch.Elapsed;
  238. await testOut.WriteLineAsync(
  239. $"{nameof(GetServerClientPair)}: Retrieving pair {pair.Id} from pool took {poolRetrieveTime.TotalMilliseconds} ms");
  240. pair.ClearModifiedCvars();
  241. pair.Settings = poolSettings;
  242. pair.TestHistory.Add(currentTestName);
  243. pair.SetupSeed();
  244. await testOut.WriteLineAsync(
  245. $"{nameof(GetServerClientPair)}: Returning pair {pair.Id} with client/server seeds: {pair.ClientSeed}/{pair.ServerSeed}");
  246. pair.Watch.Restart();
  247. return pair;
  248. }
  249. private static TestPair? GrabOptimalPair(PoolSettings poolSettings)
  250. {
  251. lock (PairLock)
  252. {
  253. TestPair? fallback = null;
  254. foreach (var pair in Pairs.Keys)
  255. {
  256. if (Pairs[pair])
  257. continue;
  258. if (!pair.Settings.CanFastRecycle(poolSettings))
  259. {
  260. fallback = pair;
  261. continue;
  262. }
  263. pair.Use();
  264. Pairs[pair] = true;
  265. return pair;
  266. }
  267. if (fallback != null)
  268. {
  269. fallback.Use();
  270. Pairs[fallback!] = true;
  271. }
  272. return fallback;
  273. }
  274. }
  275. /// <summary>
  276. /// Used by TestPair after checking the server/client pair, Don't use this.
  277. /// </summary>
  278. public static void NoCheckReturn(TestPair pair)
  279. {
  280. lock (PairLock)
  281. {
  282. if (pair.State == TestPair.PairState.Dead)
  283. Pairs.Remove(pair);
  284. else if (pair.State == TestPair.PairState.Ready)
  285. Pairs[pair] = false;
  286. else
  287. throw new InvalidOperationException($"Attempted to return a pair in an invalid state. Pair: {pair.Id}. State: {pair.State}.");
  288. }
  289. }
  290. private static void DieIfPoolFailure()
  291. {
  292. if (_poolFailureReason != null)
  293. {
  294. // If the _poolFailureReason is not null, we can assume at least one test failed.
  295. // So we say inconclusive so we don't add more failed tests to search through.
  296. Assert.Inconclusive(@$"
  297. In a different test, the pool manager had an exception when trying to create a server/client pair.
  298. Instead of risking that the pool manager will fail at creating a server/client pairs for every single test,
  299. we are just going to end this here to save a lot of time. This is the exception that started this:\n {_poolFailureReason}");
  300. }
  301. if (_dead)
  302. {
  303. // If Pairs is null, we ran out of time, we can't assume a test failed.
  304. // So we are going to tell it all future tests are a failure.
  305. Assert.Fail("The pool was shut down");
  306. }
  307. }
  308. private static async Task<TestPair> CreateServerClientPair(PoolSettings poolSettings, TextWriter testOut)
  309. {
  310. try
  311. {
  312. var id = Interlocked.Increment(ref _pairId);
  313. var pair = new TestPair(id);
  314. await pair.Initialize(poolSettings, testOut, _testPrototypes);
  315. pair.Use();
  316. await pair.RunTicksSync(5);
  317. await pair.SyncTicks(targetDelta: 1);
  318. return pair;
  319. }
  320. catch (Exception ex)
  321. {
  322. _poolFailureReason = ex;
  323. throw;
  324. }
  325. }
  326. /// <summary>
  327. /// Runs a server, or a client until a condition is true
  328. /// </summary>
  329. /// <param name="instance">The server or client</param>
  330. /// <param name="func">The condition to check</param>
  331. /// <param name="maxTicks">How many ticks to try before giving up</param>
  332. /// <param name="tickStep">How many ticks to wait between checks</param>
  333. public static async Task WaitUntil(RobustIntegrationTest.IntegrationInstance instance, Func<bool> func,
  334. int maxTicks = 600,
  335. int tickStep = 1)
  336. {
  337. await WaitUntil(instance, async () => await Task.FromResult(func()), maxTicks, tickStep);
  338. }
  339. /// <summary>
  340. /// Runs a server, or a client until a condition is true
  341. /// </summary>
  342. /// <param name="instance">The server or client</param>
  343. /// <param name="func">The async condition to check</param>
  344. /// <param name="maxTicks">How many ticks to try before giving up</param>
  345. /// <param name="tickStep">How many ticks to wait between checks</param>
  346. public static async Task WaitUntil(RobustIntegrationTest.IntegrationInstance instance, Func<Task<bool>> func,
  347. int maxTicks = 600,
  348. int tickStep = 1)
  349. {
  350. var ticksAwaited = 0;
  351. bool passed;
  352. await instance.WaitIdleAsync();
  353. while (!(passed = await func()) && ticksAwaited < maxTicks)
  354. {
  355. var ticksToRun = tickStep;
  356. if (ticksAwaited + tickStep > maxTicks)
  357. {
  358. ticksToRun = maxTicks - ticksAwaited;
  359. }
  360. await instance.WaitRunTicks(ticksToRun);
  361. ticksAwaited += ticksToRun;
  362. }
  363. if (!passed)
  364. {
  365. Assert.Fail($"Condition did not pass after {maxTicks} ticks.\n" +
  366. $"Tests ran ({instance.TestsRan.Count}):\n" +
  367. $"{string.Join('\n', instance.TestsRan)}");
  368. }
  369. Assert.That(passed);
  370. }
  371. /// <summary>
  372. /// Initialize the pool manager.
  373. /// </summary>
  374. /// <param name="extraAssemblies">Assemblies to search for to discover extra prototypes and systems.</param>
  375. public static void Startup(params Assembly[] extraAssemblies)
  376. {
  377. if (_initialized)
  378. throw new InvalidOperationException("Already initialized");
  379. _initialized = true;
  380. _contentAssemblies =
  381. [
  382. typeof(Shared.Entry.EntryPoint).Assembly,
  383. typeof(Server.Entry.EntryPoint).Assembly,
  384. typeof(PoolManager).Assembly
  385. ];
  386. _contentAssemblies.UnionWith(extraAssemblies);
  387. _testPrototypes.Clear();
  388. DiscoverTestPrototypes(typeof(PoolManager).Assembly);
  389. foreach (var assembly in extraAssemblies)
  390. {
  391. DiscoverTestPrototypes(assembly);
  392. }
  393. }
  394. }