2021-04-15 07:33:59 +00:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2021-04-15 07:33:59 +00:00
|
|
|
using System;
|
2022-08-22 10:14:06 +00:00
|
|
|
using System.Collections.Generic;
|
2021-04-15 07:33:59 +00:00
|
|
|
using NUnit.Framework;
|
2022-08-22 10:14:06 +00:00
|
|
|
using osu.Framework.Graphics;
|
2021-04-15 07:33:59 +00:00
|
|
|
using osu.Framework.Testing;
|
|
|
|
using osu.Framework.Timing;
|
2021-05-03 04:38:53 +00:00
|
|
|
using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate;
|
2022-08-22 10:14:06 +00:00
|
|
|
using osu.Game.Screens.Play;
|
2021-04-15 07:33:59 +00:00
|
|
|
using osu.Game.Tests.Visual;
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.OnlinePlay
|
|
|
|
{
|
2023-09-22 05:06:05 +00:00
|
|
|
// NOTE: This test scene never calls ProcessFrame on clocks.
|
|
|
|
// The current tests are fine without this as they are testing very static scenarios, but it's worth knowing
|
|
|
|
// if adding further tests to this class.
|
2021-04-15 07:33:59 +00:00
|
|
|
[HeadlessTest]
|
2021-04-23 10:12:30 +00:00
|
|
|
public partial class TestSceneCatchUpSyncManager : OsuTestScene
|
2021-04-15 07:33:59 +00:00
|
|
|
{
|
2022-08-22 10:14:06 +00:00
|
|
|
private GameplayClockContainer master;
|
2022-08-24 06:07:04 +00:00
|
|
|
private SpectatorSyncManager syncManager;
|
2021-04-15 07:33:59 +00:00
|
|
|
|
2022-08-24 06:07:04 +00:00
|
|
|
private Dictionary<SpectatorPlayerClock, int> clocksById;
|
|
|
|
private SpectatorPlayerClock player1;
|
|
|
|
private SpectatorPlayerClock player2;
|
2021-04-15 07:33:59 +00:00
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void Setup()
|
|
|
|
{
|
2023-09-22 05:26:33 +00:00
|
|
|
syncManager = new SpectatorSyncManager(master = new GameplayClockContainer(new TestManualClock(), false, false));
|
2022-08-23 04:52:43 +00:00
|
|
|
player1 = syncManager.CreateManagedClock();
|
|
|
|
player2 = syncManager.CreateManagedClock();
|
2021-04-15 07:33:59 +00:00
|
|
|
|
2022-08-24 06:07:04 +00:00
|
|
|
clocksById = new Dictionary<SpectatorPlayerClock, int>
|
2022-08-22 10:14:06 +00:00
|
|
|
{
|
|
|
|
{ player1, 1 },
|
|
|
|
{ player2, 2 }
|
|
|
|
};
|
|
|
|
|
|
|
|
Schedule(() =>
|
|
|
|
{
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
syncManager,
|
|
|
|
master
|
|
|
|
};
|
|
|
|
});
|
2021-04-15 07:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2021-06-11 08:59:27 +00:00
|
|
|
public void TestPlayerClocksStartWhenAllHaveFrames()
|
2021-04-15 07:33:59 +00:00
|
|
|
{
|
2021-04-26 08:19:44 +00:00
|
|
|
setWaiting(() => player1, false);
|
|
|
|
assertPlayerClockState(() => player1, false);
|
|
|
|
assertPlayerClockState(() => player2, false);
|
2021-04-15 07:33:59 +00:00
|
|
|
|
2021-04-26 08:19:44 +00:00
|
|
|
setWaiting(() => player2, false);
|
|
|
|
assertPlayerClockState(() => player1, true);
|
|
|
|
assertPlayerClockState(() => player2, true);
|
2021-04-15 07:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2021-06-11 08:59:27 +00:00
|
|
|
public void TestReadyPlayersStartWhenReadyForMaximumDelayTime()
|
2021-04-15 07:33:59 +00:00
|
|
|
{
|
2021-04-26 08:19:44 +00:00
|
|
|
setWaiting(() => player1, false);
|
2022-08-24 06:07:04 +00:00
|
|
|
AddWaitStep($"wait {SpectatorSyncManager.MAXIMUM_START_DELAY} milliseconds", (int)Math.Ceiling(SpectatorSyncManager.MAXIMUM_START_DELAY / TimePerAction));
|
2021-06-11 08:59:27 +00:00
|
|
|
assertPlayerClockState(() => player1, true);
|
|
|
|
assertPlayerClockState(() => player2, false);
|
2021-04-15 07:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2021-04-26 08:19:44 +00:00
|
|
|
public void TestPlayerClockDoesNotCatchUpWhenSlightlyOutOfSync()
|
2021-04-15 07:33:59 +00:00
|
|
|
{
|
|
|
|
setAllWaiting(false);
|
|
|
|
|
2022-08-24 06:07:04 +00:00
|
|
|
setMasterTime(SpectatorSyncManager.SYNC_TARGET + 1);
|
2021-04-26 08:19:44 +00:00
|
|
|
assertCatchingUp(() => player1, false);
|
2021-04-15 07:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2021-04-26 08:19:44 +00:00
|
|
|
public void TestPlayerClockStartsCatchingUpWhenTooFarBehind()
|
2021-04-15 07:33:59 +00:00
|
|
|
{
|
|
|
|
setAllWaiting(false);
|
|
|
|
|
2022-08-24 06:07:04 +00:00
|
|
|
setMasterTime(SpectatorSyncManager.MAX_SYNC_OFFSET + 1);
|
2021-04-26 08:19:44 +00:00
|
|
|
assertCatchingUp(() => player1, true);
|
|
|
|
assertCatchingUp(() => player2, true);
|
2021-04-15 07:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2021-04-26 08:19:44 +00:00
|
|
|
public void TestPlayerClockKeepsCatchingUpWhenSlightlyOutOfSync()
|
2021-04-15 07:33:59 +00:00
|
|
|
{
|
|
|
|
setAllWaiting(false);
|
|
|
|
|
2022-08-24 06:07:04 +00:00
|
|
|
setMasterTime(SpectatorSyncManager.MAX_SYNC_OFFSET + 1);
|
|
|
|
setPlayerClockTime(() => player1, SpectatorSyncManager.SYNC_TARGET + 1);
|
2021-04-26 08:19:44 +00:00
|
|
|
assertCatchingUp(() => player1, true);
|
2021-04-15 07:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2021-04-26 08:19:44 +00:00
|
|
|
public void TestPlayerClockStopsCatchingUpWhenInSync()
|
2021-04-15 07:33:59 +00:00
|
|
|
{
|
|
|
|
setAllWaiting(false);
|
|
|
|
|
2022-08-24 06:07:04 +00:00
|
|
|
setMasterTime(SpectatorSyncManager.MAX_SYNC_OFFSET + 2);
|
|
|
|
setPlayerClockTime(() => player1, SpectatorSyncManager.SYNC_TARGET);
|
2021-04-26 08:19:44 +00:00
|
|
|
assertCatchingUp(() => player1, false);
|
|
|
|
assertCatchingUp(() => player2, true);
|
2021-04-15 07:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2021-04-26 08:19:44 +00:00
|
|
|
public void TestPlayerClockDoesNotStopWhenSlightlyAhead()
|
2021-04-15 07:33:59 +00:00
|
|
|
{
|
|
|
|
setAllWaiting(false);
|
|
|
|
|
2022-08-24 06:07:04 +00:00
|
|
|
setPlayerClockTime(() => player1, -SpectatorSyncManager.SYNC_TARGET);
|
2021-04-26 08:19:44 +00:00
|
|
|
assertCatchingUp(() => player1, false);
|
|
|
|
assertPlayerClockState(() => player1, true);
|
2021-04-15 07:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2021-04-26 08:19:44 +00:00
|
|
|
public void TestPlayerClockStopsWhenTooFarAheadAndStartsWhenBackInSync()
|
2021-04-15 07:33:59 +00:00
|
|
|
{
|
|
|
|
setAllWaiting(false);
|
|
|
|
|
2022-08-24 06:07:04 +00:00
|
|
|
setPlayerClockTime(() => player1, -SpectatorSyncManager.SYNC_TARGET - 1);
|
2021-04-15 07:33:59 +00:00
|
|
|
|
|
|
|
// This is a silent catchup, where IsCatchingUp = false but IsRunning = false also.
|
2021-04-26 08:19:44 +00:00
|
|
|
assertCatchingUp(() => player1, false);
|
|
|
|
assertPlayerClockState(() => player1, false);
|
2021-04-15 07:33:59 +00:00
|
|
|
|
|
|
|
setMasterTime(1);
|
2021-04-26 08:19:44 +00:00
|
|
|
assertCatchingUp(() => player1, false);
|
|
|
|
assertPlayerClockState(() => player1, true);
|
2021-04-15 07:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2021-04-26 08:19:44 +00:00
|
|
|
public void TestInSyncPlayerClockDoesNotStartIfWaitingOnFrames()
|
2021-04-15 07:33:59 +00:00
|
|
|
{
|
|
|
|
setAllWaiting(false);
|
|
|
|
|
2021-04-26 08:19:44 +00:00
|
|
|
assertPlayerClockState(() => player1, true);
|
|
|
|
setWaiting(() => player1, true);
|
|
|
|
assertPlayerClockState(() => player1, false);
|
2021-04-15 07:33:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 06:07:04 +00:00
|
|
|
private void setWaiting(Func<SpectatorPlayerClock> playerClock, bool waiting)
|
2022-08-24 06:40:47 +00:00
|
|
|
=> AddStep($"set player clock {clocksById[playerClock()]} waiting = {waiting}", () => playerClock().WaitingOnFrames = waiting);
|
2021-04-15 07:33:59 +00:00
|
|
|
|
2021-04-26 08:19:44 +00:00
|
|
|
private void setAllWaiting(bool waiting) => AddStep($"set all player clocks waiting = {waiting}", () =>
|
2021-04-15 07:33:59 +00:00
|
|
|
{
|
2022-08-24 06:40:47 +00:00
|
|
|
player1.WaitingOnFrames = waiting;
|
|
|
|
player2.WaitingOnFrames = waiting;
|
2021-04-15 07:33:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
private void setMasterTime(double time)
|
|
|
|
=> AddStep($"set master = {time}", () => master.Seek(time));
|
|
|
|
|
|
|
|
/// <summary>
|
2021-04-26 08:19:44 +00:00
|
|
|
/// clock.Time = master.Time - offsetFromMaster
|
2021-04-15 07:33:59 +00:00
|
|
|
/// </summary>
|
2022-08-24 06:07:04 +00:00
|
|
|
private void setPlayerClockTime(Func<SpectatorPlayerClock> playerClock, double offsetFromMaster)
|
2022-08-22 10:14:06 +00:00
|
|
|
=> AddStep($"set player clock {clocksById[playerClock()]} = master - {offsetFromMaster}", () => playerClock().Seek(master.CurrentTime - offsetFromMaster));
|
2021-04-26 08:30:27 +00:00
|
|
|
|
2022-08-24 06:07:04 +00:00
|
|
|
private void assertCatchingUp(Func<SpectatorPlayerClock> playerClock, bool catchingUp) =>
|
2022-08-22 10:14:06 +00:00
|
|
|
AddAssert($"player clock {clocksById[playerClock()]} {(catchingUp ? "is" : "is not")} catching up", () => playerClock().IsCatchingUp == catchingUp);
|
2021-04-15 07:33:59 +00:00
|
|
|
|
2022-08-24 06:07:04 +00:00
|
|
|
private void assertPlayerClockState(Func<SpectatorPlayerClock> playerClock, bool running)
|
2022-08-22 10:14:06 +00:00
|
|
|
=> AddAssert($"player clock {clocksById[playerClock()]} {(running ? "is" : "is not")} running", () => playerClock().IsRunning == running);
|
2021-04-15 07:33:59 +00:00
|
|
|
|
|
|
|
private class TestManualClock : ManualClock, IAdjustableClock
|
|
|
|
{
|
2021-06-11 08:59:27 +00:00
|
|
|
public TestManualClock()
|
|
|
|
{
|
|
|
|
IsRunning = true;
|
|
|
|
}
|
|
|
|
|
2021-04-15 07:33:59 +00:00
|
|
|
public void Start() => IsRunning = true;
|
|
|
|
|
|
|
|
public void Stop() => IsRunning = false;
|
|
|
|
|
|
|
|
public bool Seek(double position)
|
|
|
|
{
|
|
|
|
CurrentTime = position;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Reset()
|
|
|
|
{
|
2023-09-21 19:10:57 +00:00
|
|
|
IsRunning = false;
|
|
|
|
CurrentTime = 0;
|
2021-04-15 07:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void ResetSpeedAdjustments()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|