PoolSettings.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #nullable enable
  2. using Robust.Shared.Random;
  3. namespace Content.IntegrationTests;
  4. /// <summary>
  5. /// Settings for the pooled server, and client pair.
  6. /// Some options are for changing the pair, and others are
  7. /// so the pool can properly clean up what you borrowed.
  8. /// </summary>
  9. public sealed class PoolSettings
  10. {
  11. /// <summary>
  12. /// Set to true if the test will ruin the server/client pair.
  13. /// </summary>
  14. public bool Destructive { get; init; }
  15. /// <summary>
  16. /// Set to true if the given server/client pair should be created fresh.
  17. /// </summary>
  18. public bool Fresh { get; init; }
  19. /// <summary>
  20. /// Set to true if the given server should be using a dummy ticker. Ignored if <see cref="InLobby"/> is true.
  21. /// </summary>
  22. public bool DummyTicker { get; init; } = true;
  23. /// <summary>
  24. /// If true, this enables the creation of admin logs during the test.
  25. /// </summary>
  26. public bool AdminLogsEnabled { get; init; }
  27. /// <summary>
  28. /// Set to true if the given server/client pair should be connected from each other.
  29. /// Defaults to disconnected as it makes dirty recycling slightly faster.
  30. /// If <see cref="InLobby"/> is true, this option is ignored.
  31. /// </summary>
  32. public bool Connected { get; init; }
  33. /// <summary>
  34. /// Set to true if the given server/client pair should be in the lobby.
  35. /// If the pair is not in the lobby at the end of the test, this test must be marked as dirty.
  36. /// </summary>
  37. /// <remarks>
  38. /// If this is enabled, the value of <see cref="DummyTicker"/> is ignored.
  39. /// </remarks>
  40. public bool InLobby { get; init; }
  41. /// <summary>
  42. /// Set this to true to skip loading the content files.
  43. /// Note: This setting won't work with a client.
  44. /// </summary>
  45. public bool NoLoadContent { get; init; }
  46. /// <summary>
  47. /// This will return a server-client pair that has not loaded test prototypes.
  48. /// Try avoiding this whenever possible, as this will always create & destroy a new pair.
  49. /// Use <see cref="Pair.TestPair.IsTestPrototype(Robust.Shared.Prototypes.EntityPrototype)"/> if you need to exclude test prototypees.
  50. /// </summary>
  51. public bool NoLoadTestPrototypes { get; init; }
  52. /// <summary>
  53. /// Set this to true to disable the NetInterp CVar on the given server/client pair
  54. /// </summary>
  55. public bool DisableInterpolate { get; init; }
  56. /// <summary>
  57. /// Set this to true to always clean up the server/client pair before giving it to another borrower
  58. /// </summary>
  59. public bool Dirty { get; init; }
  60. /// <summary>
  61. /// Set this to the path of a map to have the given server/client pair load the map.
  62. /// </summary>
  63. public string Map { get; init; } = PoolManager.TestMap;
  64. /// <summary>
  65. /// Overrides the test name detection, and uses this in the test history instead
  66. /// </summary>
  67. public string? TestName { get; set; }
  68. /// <summary>
  69. /// If set, this will be used to call <see cref="IRobustRandom.SetSeed"/>
  70. /// </summary>
  71. public int? ServerSeed { get; set; }
  72. /// <summary>
  73. /// If set, this will be used to call <see cref="IRobustRandom.SetSeed"/>
  74. /// </summary>
  75. public int? ClientSeed { get; set; }
  76. #region Inferred Properties
  77. /// <summary>
  78. /// If the returned pair must not be reused
  79. /// </summary>
  80. public bool MustNotBeReused => Destructive || NoLoadContent || NoLoadTestPrototypes;
  81. /// <summary>
  82. /// If the given pair must be brand new
  83. /// </summary>
  84. public bool MustBeNew => Fresh || NoLoadContent || NoLoadTestPrototypes;
  85. public bool UseDummyTicker => !InLobby && DummyTicker;
  86. public bool ShouldBeConnected => InLobby || Connected;
  87. #endregion
  88. /// <summary>
  89. /// Tries to guess if we can skip recycling the server/client pair.
  90. /// </summary>
  91. /// <param name="nextSettings">The next set of settings the old pair will be set to</param>
  92. /// <returns>If we can skip cleaning it up</returns>
  93. public bool CanFastRecycle(PoolSettings nextSettings)
  94. {
  95. if (MustNotBeReused)
  96. throw new InvalidOperationException("Attempting to recycle a non-reusable test.");
  97. if (nextSettings.MustBeNew)
  98. throw new InvalidOperationException("Attempting to recycle a test while requesting a fresh test.");
  99. if (Dirty)
  100. return false;
  101. // Check that certain settings match.
  102. return !ShouldBeConnected == !nextSettings.ShouldBeConnected
  103. && UseDummyTicker == nextSettings.UseDummyTicker
  104. && Map == nextSettings.Map
  105. && InLobby == nextSettings.InLobby;
  106. }
  107. }