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.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using NUnit.Framework;
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Testing;
|
|
|
|
using osu.Framework.Timing;
|
2021-05-03 04:38:53 +00:00
|
|
|
using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate;
|
2021-04-15 07:33:59 +00:00
|
|
|
using osu.Game.Tests.Visual;
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.OnlinePlay
|
|
|
|
{
|
|
|
|
[HeadlessTest]
|
2021-04-23 10:12:30 +00:00
|
|
|
public class TestSceneCatchUpSyncManager : OsuTestScene
|
2021-04-15 07:33:59 +00:00
|
|
|
{
|
|
|
|
private TestManualClock master;
|
2021-04-16 13:47:52 +00:00
|
|
|
private CatchUpSyncManager syncManager;
|
2021-04-15 07:33:59 +00:00
|
|
|
|
2021-04-26 08:19:44 +00:00
|
|
|
private TestSpectatorPlayerClock player1;
|
|
|
|
private TestSpectatorPlayerClock player2;
|
2021-04-15 07:33:59 +00:00
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void Setup()
|
|
|
|
{
|
2021-04-16 13:47:52 +00:00
|
|
|
syncManager = new CatchUpSyncManager(master = new TestManualClock());
|
2021-04-26 08:19:44 +00:00
|
|
|
syncManager.AddPlayerClock(player1 = new TestSpectatorPlayerClock(1));
|
|
|
|
syncManager.AddPlayerClock(player2 = new TestSpectatorPlayerClock(2));
|
2021-04-15 07:33:59 +00:00
|
|
|
|
2021-04-15 10:32:55 +00:00
|
|
|
Schedule(() => Child = syncManager);
|
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);
|
2021-04-16 13:47:52 +00:00
|
|
|
AddWaitStep($"wait {CatchUpSyncManager.MAXIMUM_START_DELAY} milliseconds", (int)Math.Ceiling(CatchUpSyncManager.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);
|
|
|
|
|
2021-04-16 13:47:52 +00:00
|
|
|
setMasterTime(CatchUpSyncManager.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);
|
|
|
|
|
2021-04-16 13:47:52 +00:00
|
|
|
setMasterTime(CatchUpSyncManager.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);
|
|
|
|
|
2021-04-16 13:47:52 +00:00
|
|
|
setMasterTime(CatchUpSyncManager.MAX_SYNC_OFFSET + 1);
|
2021-04-26 08:19:44 +00:00
|
|
|
setPlayerClockTime(() => player1, CatchUpSyncManager.SYNC_TARGET + 1);
|
|
|
|
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);
|
|
|
|
|
2021-04-16 13:47:52 +00:00
|
|
|
setMasterTime(CatchUpSyncManager.MAX_SYNC_OFFSET + 2);
|
2021-04-26 08:19:44 +00:00
|
|
|
setPlayerClockTime(() => player1, CatchUpSyncManager.SYNC_TARGET);
|
|
|
|
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);
|
|
|
|
|
2021-04-26 08:19:44 +00:00
|
|
|
setPlayerClockTime(() => player1, -CatchUpSyncManager.SYNC_TARGET);
|
|
|
|
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);
|
|
|
|
|
2021-04-26 08:19:44 +00:00
|
|
|
setPlayerClockTime(() => player1, -CatchUpSyncManager.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
|
|
|
}
|
|
|
|
|
2021-04-26 08:19:44 +00:00
|
|
|
private void setWaiting(Func<TestSpectatorPlayerClock> playerClock, bool waiting)
|
|
|
|
=> AddStep($"set player clock {playerClock().Id} waiting = {waiting}", () => playerClock().WaitingOnFrames.Value = 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
|
|
|
{
|
2021-04-26 08:19:44 +00:00
|
|
|
player1.WaitingOnFrames.Value = waiting;
|
|
|
|
player2.WaitingOnFrames.Value = 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>
|
2021-04-26 08:19:44 +00:00
|
|
|
private void setPlayerClockTime(Func<TestSpectatorPlayerClock> playerClock, double offsetFromMaster)
|
|
|
|
=> AddStep($"set player clock {playerClock().Id} = master - {offsetFromMaster}", () => playerClock().Seek(master.CurrentTime - offsetFromMaster));
|
2021-04-15 07:33:59 +00:00
|
|
|
|
2021-04-26 08:19:44 +00:00
|
|
|
private void assertCatchingUp(Func<TestSpectatorPlayerClock> playerClock, bool catchingUp) =>
|
|
|
|
AddAssert($"player clock {playerClock().Id} {(catchingUp ? "is" : "is not")} catching up", () => playerClock().IsCatchingUp == catchingUp);
|
2021-04-15 07:33:59 +00:00
|
|
|
|
2021-04-26 08:19:44 +00:00
|
|
|
private void assertPlayerClockState(Func<TestSpectatorPlayerClock> playerClock, bool running)
|
|
|
|
=> AddAssert($"player clock {playerClock().Id} {(running ? "is" : "is not")} running", () => playerClock().IsRunning == running);
|
2021-04-15 07:33:59 +00:00
|
|
|
|
2021-04-26 08:19:44 +00:00
|
|
|
private class TestSpectatorPlayerClock : TestManualClock, ISpectatorPlayerClock
|
2021-04-15 07:33:59 +00:00
|
|
|
{
|
2021-04-23 10:11:47 +00:00
|
|
|
public Bindable<bool> WaitingOnFrames { get; } = new Bindable<bool>(true);
|
2021-04-15 07:33:59 +00:00
|
|
|
|
|
|
|
public bool IsCatchingUp { get; set; }
|
|
|
|
|
2021-04-26 08:30:27 +00:00
|
|
|
public IFrameBasedClock Source
|
|
|
|
{
|
|
|
|
set => throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
2021-04-15 07:33:59 +00:00
|
|
|
public readonly int Id;
|
|
|
|
|
2021-04-26 08:19:44 +00:00
|
|
|
public TestSpectatorPlayerClock(int id)
|
2021-04-15 07:33:59 +00:00
|
|
|
{
|
|
|
|
Id = id;
|
|
|
|
|
|
|
|
WaitingOnFrames.BindValueChanged(waiting =>
|
|
|
|
{
|
|
|
|
if (waiting.NewValue)
|
|
|
|
Stop();
|
|
|
|
else
|
|
|
|
Start();
|
|
|
|
});
|
|
|
|
}
|
2021-04-15 10:32:55 +00:00
|
|
|
|
|
|
|
public void ProcessFrame()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public double ElapsedFrameTime => 0;
|
|
|
|
|
|
|
|
public double FramesPerSecond => 0;
|
|
|
|
|
|
|
|
public FrameTimeInfo TimeInfo => default;
|
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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ResetSpeedAdjustments()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|