1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-21 13:22:57 +08:00
osu-lazer/osu.Game.Tests/OnlinePlay/TestSceneCatchUpSyncManager.cs

199 lines
6.7 KiB
C#
Raw Normal View History

// 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 15:37:17 +08:00
#nullable disable
using System;
using System.Collections.Generic;
using NUnit.Framework;
using osu.Framework.Graphics;
using osu.Framework.Testing;
using osu.Framework.Timing;
using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate;
using osu.Game.Screens.Play;
using osu.Game.Tests.Visual;
namespace osu.Game.Tests.OnlinePlay
{
[HeadlessTest]
2021-04-23 18:12:30 +08:00
public class TestSceneCatchUpSyncManager : OsuTestScene
{
private GameplayClockContainer master;
2022-08-24 14:07:04 +08:00
private SpectatorSyncManager syncManager;
2022-08-24 14:07:04 +08:00
private Dictionary<SpectatorPlayerClock, int> clocksById;
private SpectatorPlayerClock player1;
private SpectatorPlayerClock player2;
[SetUp]
public void Setup()
{
2022-08-24 14:07:04 +08:00
syncManager = new SpectatorSyncManager(master = new GameplayClockContainer(new TestManualClock()));
player1 = syncManager.CreateManagedClock();
player2 = syncManager.CreateManagedClock();
2022-08-24 14:07:04 +08:00
clocksById = new Dictionary<SpectatorPlayerClock, int>
{
{ player1, 1 },
{ player2, 2 }
};
Schedule(() =>
{
Children = new Drawable[]
{
syncManager,
master
};
});
}
[Test]
2021-06-11 16:59:27 +08:00
public void TestPlayerClocksStartWhenAllHaveFrames()
{
setWaiting(() => player1, false);
assertPlayerClockState(() => player1, false);
assertPlayerClockState(() => player2, false);
setWaiting(() => player2, false);
assertPlayerClockState(() => player1, true);
assertPlayerClockState(() => player2, true);
}
[Test]
2021-06-11 16:59:27 +08:00
public void TestReadyPlayersStartWhenReadyForMaximumDelayTime()
{
setWaiting(() => player1, false);
2022-08-24 14:07:04 +08:00
AddWaitStep($"wait {SpectatorSyncManager.MAXIMUM_START_DELAY} milliseconds", (int)Math.Ceiling(SpectatorSyncManager.MAXIMUM_START_DELAY / TimePerAction));
2021-06-11 16:59:27 +08:00
assertPlayerClockState(() => player1, true);
assertPlayerClockState(() => player2, false);
}
[Test]
public void TestPlayerClockDoesNotCatchUpWhenSlightlyOutOfSync()
{
setAllWaiting(false);
2022-08-24 14:07:04 +08:00
setMasterTime(SpectatorSyncManager.SYNC_TARGET + 1);
assertCatchingUp(() => player1, false);
}
[Test]
public void TestPlayerClockStartsCatchingUpWhenTooFarBehind()
{
setAllWaiting(false);
2022-08-24 14:07:04 +08:00
setMasterTime(SpectatorSyncManager.MAX_SYNC_OFFSET + 1);
assertCatchingUp(() => player1, true);
assertCatchingUp(() => player2, true);
}
[Test]
public void TestPlayerClockKeepsCatchingUpWhenSlightlyOutOfSync()
{
setAllWaiting(false);
2022-08-24 14:07:04 +08:00
setMasterTime(SpectatorSyncManager.MAX_SYNC_OFFSET + 1);
setPlayerClockTime(() => player1, SpectatorSyncManager.SYNC_TARGET + 1);
assertCatchingUp(() => player1, true);
}
[Test]
public void TestPlayerClockStopsCatchingUpWhenInSync()
{
setAllWaiting(false);
2022-08-24 14:07:04 +08:00
setMasterTime(SpectatorSyncManager.MAX_SYNC_OFFSET + 2);
setPlayerClockTime(() => player1, SpectatorSyncManager.SYNC_TARGET);
assertCatchingUp(() => player1, false);
assertCatchingUp(() => player2, true);
}
[Test]
public void TestPlayerClockDoesNotStopWhenSlightlyAhead()
{
setAllWaiting(false);
2022-08-24 14:07:04 +08:00
setPlayerClockTime(() => player1, -SpectatorSyncManager.SYNC_TARGET);
assertCatchingUp(() => player1, false);
assertPlayerClockState(() => player1, true);
}
[Test]
public void TestPlayerClockStopsWhenTooFarAheadAndStartsWhenBackInSync()
{
setAllWaiting(false);
2022-08-24 14:07:04 +08:00
setPlayerClockTime(() => player1, -SpectatorSyncManager.SYNC_TARGET - 1);
// This is a silent catchup, where IsCatchingUp = false but IsRunning = false also.
assertCatchingUp(() => player1, false);
assertPlayerClockState(() => player1, false);
setMasterTime(1);
assertCatchingUp(() => player1, false);
assertPlayerClockState(() => player1, true);
}
[Test]
public void TestInSyncPlayerClockDoesNotStartIfWaitingOnFrames()
{
setAllWaiting(false);
assertPlayerClockState(() => player1, true);
setWaiting(() => player1, true);
assertPlayerClockState(() => player1, false);
}
2022-08-24 14:07:04 +08:00
private void setWaiting(Func<SpectatorPlayerClock> playerClock, bool waiting)
2022-08-24 14:40:47 +08:00
=> AddStep($"set player clock {clocksById[playerClock()]} waiting = {waiting}", () => playerClock().WaitingOnFrames = waiting);
private void setAllWaiting(bool waiting) => AddStep($"set all player clocks waiting = {waiting}", () =>
{
2022-08-24 14:40:47 +08:00
player1.WaitingOnFrames = waiting;
player2.WaitingOnFrames = waiting;
});
private void setMasterTime(double time)
=> AddStep($"set master = {time}", () => master.Seek(time));
/// <summary>
/// clock.Time = master.Time - offsetFromMaster
/// </summary>
2022-08-24 14:07:04 +08:00
private void setPlayerClockTime(Func<SpectatorPlayerClock> playerClock, double offsetFromMaster)
=> AddStep($"set player clock {clocksById[playerClock()]} = master - {offsetFromMaster}", () => playerClock().Seek(master.CurrentTime - offsetFromMaster));
2022-08-24 14:07:04 +08:00
private void assertCatchingUp(Func<SpectatorPlayerClock> playerClock, bool catchingUp) =>
AddAssert($"player clock {clocksById[playerClock()]} {(catchingUp ? "is" : "is not")} catching up", () => playerClock().IsCatchingUp == catchingUp);
2022-08-24 14:07:04 +08:00
private void assertPlayerClockState(Func<SpectatorPlayerClock> playerClock, bool running)
=> AddAssert($"player clock {clocksById[playerClock()]} {(running ? "is" : "is not")} running", () => playerClock().IsRunning == running);
private class TestManualClock : ManualClock, IAdjustableClock
{
2021-06-11 16:59:27 +08:00
public TestManualClock()
{
IsRunning = true;
}
public void Start() => IsRunning = true;
public void Stop() => IsRunning = false;
public bool Seek(double position)
{
CurrentTime = position;
return true;
}
public void Reset()
{
}
public void ResetSpeedAdjustments()
{
}
}
}
}