1
0

RoundEndTest.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System.Threading;
  2. using Content.Server.GameTicking;
  3. using Content.Server.RoundEnd;
  4. using Content.Shared.CCVar;
  5. using Robust.Shared.Configuration;
  6. using Robust.Shared.GameObjects;
  7. namespace Content.IntegrationTests.Tests
  8. {
  9. [TestFixture]
  10. public sealed class RoundEndTest
  11. {
  12. private sealed class RoundEndTestSystem : EntitySystem
  13. {
  14. public int RoundCount;
  15. public override void Initialize()
  16. {
  17. base.Initialize();
  18. SubscribeLocalEvent<RoundEndSystemChangedEvent>(OnRoundEnd);
  19. }
  20. private void OnRoundEnd(RoundEndSystemChangedEvent ev)
  21. {
  22. Interlocked.Increment(ref RoundCount);
  23. }
  24. }
  25. [Test]
  26. public async Task Test()
  27. {
  28. await using var pair = await PoolManager.GetServerClient(new PoolSettings
  29. {
  30. DummyTicker = false,
  31. Connected = true,
  32. Dirty = true
  33. });
  34. var server = pair.Server;
  35. var config = server.ResolveDependency<IConfigurationManager>();
  36. var sysManager = server.ResolveDependency<IEntitySystemManager>();
  37. var ticker = sysManager.GetEntitySystem<GameTicker>();
  38. var roundEndSystem = sysManager.GetEntitySystem<RoundEndSystem>();
  39. var sys = server.System<RoundEndTestSystem>();
  40. sys.RoundCount = 0;
  41. await server.WaitAssertion(() =>
  42. {
  43. config.SetCVar(CCVars.GameLobbyEnabled, true);
  44. config.SetCVar(CCVars.EmergencyShuttleMinTransitTime, 1f);
  45. config.SetCVar(CCVars.EmergencyShuttleDockTime, 1f);
  46. config.SetCVar(CCVars.RoundRestartTime, 1f);
  47. roundEndSystem.DefaultCooldownDuration = TimeSpan.FromMilliseconds(100);
  48. roundEndSystem.DefaultCountdownDuration = TimeSpan.FromMilliseconds(300);
  49. });
  50. await server.WaitAssertion(() =>
  51. {
  52. // Press the shuttle call button
  53. roundEndSystem.RequestRoundEnd();
  54. Assert.Multiple(() =>
  55. {
  56. Assert.That(roundEndSystem.ExpectedCountdownEnd, Is.Not.Null, "Shuttle was called, but countdown time was not set");
  57. Assert.That(roundEndSystem.CanCallOrRecall(), Is.False, "Started the shuttle, but didn't have to wait cooldown to press cancel button");
  58. });
  59. // Check that we can't recall the shuttle yet
  60. roundEndSystem.CancelRoundEndCountdown();
  61. Assert.That(roundEndSystem.ExpectedCountdownEnd, Is.Not.Null, "Shuttle was cancelled, even though the button was on cooldown");
  62. });
  63. await WaitForEvent(); // Wait for Cooldown
  64. await server.WaitAssertion(() =>
  65. {
  66. Assert.Multiple(() =>
  67. {
  68. Assert.That(roundEndSystem.CanCallOrRecall(), Is.True, "We waited a while, but the cooldown is not expired");
  69. Assert.That(roundEndSystem.ExpectedCountdownEnd, Is.Not.Null, "We were waiting for the cooldown, but the round also ended");
  70. });
  71. // Recall the shuttle, which should trigger the cooldown again
  72. roundEndSystem.CancelRoundEndCountdown();
  73. Assert.Multiple(() =>
  74. {
  75. Assert.That(roundEndSystem.ExpectedCountdownEnd, Is.Null, "Recalled shuttle, but countdown has not ended");
  76. Assert.That(roundEndSystem.CanCallOrRecall(), Is.False, "Recalled shuttle, but cooldown has not been enabled");
  77. });
  78. });
  79. await WaitForEvent(); // Wait for Cooldown
  80. await server.WaitAssertion(() =>
  81. {
  82. Assert.That(roundEndSystem.CanCallOrRecall(), Is.True, "We waited a while, but the cooldown is not expired");
  83. // Press the shuttle call button
  84. roundEndSystem.RequestRoundEnd();
  85. });
  86. await WaitForEvent(); // Wait for Cooldown
  87. await server.WaitAssertion(() =>
  88. {
  89. Assert.Multiple(() =>
  90. {
  91. Assert.That(roundEndSystem.CanCallOrRecall(), Is.True, "We waited a while, but the cooldown is not expired");
  92. Assert.That(roundEndSystem.ExpectedCountdownEnd, Is.Not.Null, "The countdown ended, but we just wanted the cooldown to end");
  93. });
  94. });
  95. await WaitForEvent(); // Wait for countdown to end round
  96. await CheckRunLevel(GameRunLevel.PostRound);
  97. await WaitForEvent(); // Wait for Restart
  98. await CheckRunLevel(GameRunLevel.PreRoundLobby);
  99. Task CheckRunLevel(GameRunLevel level)
  100. {
  101. return server.WaitAssertion(() =>
  102. {
  103. Assert.That(ticker.RunLevel, Is.EqualTo(level));
  104. });
  105. }
  106. async Task WaitForEvent()
  107. {
  108. var timeout = Task.Delay(TimeSpan.FromSeconds(10));
  109. var currentCount = Thread.VolatileRead(ref sys.RoundCount);
  110. while (currentCount == Thread.VolatileRead(ref sys.RoundCount) && !timeout.IsCompleted)
  111. {
  112. await pair.RunTicksSync(5);
  113. }
  114. if (timeout.IsCompleted) throw new TimeoutException("Event took too long to trigger");
  115. }
  116. // Need to clean self up
  117. await server.WaitAssertion(() =>
  118. {
  119. config.SetCVar(CCVars.GameLobbyEnabled, false);
  120. config.SetCVar(CCVars.EmergencyShuttleMinTransitTime, CCVars.EmergencyShuttleMinTransitTime.DefaultValue);
  121. config.SetCVar(CCVars.EmergencyShuttleDockTime, CCVars.EmergencyShuttleDockTime.DefaultValue);
  122. config.SetCVar(CCVars.RoundRestartTime, CCVars.RoundRestartTime.DefaultValue);
  123. roundEndSystem.DefaultCooldownDuration = TimeSpan.FromSeconds(30);
  124. roundEndSystem.DefaultCountdownDuration = TimeSpan.FromMinutes(4);
  125. ticker.RestartRound();
  126. });
  127. await pair.CleanReturnAsync();
  128. }
  129. }
  130. }