|
| 1 | +using System.Collections; |
| 2 | +using NUnit.Framework; |
| 3 | +using Unity.Netcode.Components; |
| 4 | +using Unity.Netcode.TestHelpers.Runtime; |
| 5 | +using UnityEngine; |
| 6 | +using UnityEngine.TestTools; |
| 7 | + |
| 8 | +namespace Unity.Netcode.RuntimeTests |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Validates that the render time a non-authority instance interpolates towards is derived from the same |
| 12 | + /// clock that the state updates it is interpolating between are stamped on. |
| 13 | + /// </summary> |
| 14 | + /// <remarks> |
| 15 | + /// Measures how far behind ServerTime the state being interpolated towards was sent. The render time is |
| 16 | + /// ServerTime minus the tick latency and only states sent at or before it are eligible, so that measurement |
| 17 | + /// can never be less than the tick latency. Deriving the render time from LocalTime eats into that margin by |
| 18 | + /// however far the two clocks are apart, and can push the target past ServerTime entirely. |
| 19 | + /// </remarks> |
| 20 | + [TestFixture(NetworkTransform.InterpolationTypes.Lerp)] |
| 21 | + [TestFixture(NetworkTransform.InterpolationTypes.SmoothDampening)] |
| 22 | + internal class NetworkTransformInterpolationRenderTimeTests : IntegrationTestWithApproximation |
| 23 | + { |
| 24 | + protected override int NumberOfClients => 1; |
| 25 | + |
| 26 | + // How far LocalTime is pushed ahead of ServerTime, in ticks. An in-process test has no round trip time |
| 27 | + // to separate the two clocks, and this is large enough to exceed NetworkTimeSystem's hard reset |
| 28 | + // threshold so the offset snaps instead of converging at its default adjustment ratio. |
| 29 | + private const int k_LocalBufferTicks = 12; |
| 30 | + |
| 31 | + // The separation the clocks must actually reach before any measurement is taken. |
| 32 | + private const double k_RequiredLeadTicks = 8.0d; |
| 33 | + |
| 34 | + // Ticks of authority motion after the clocks have separated, so the interpolator reaches steady state. |
| 35 | + private const int k_WarmUpTicks = 20; |
| 36 | + |
| 37 | + private const int k_SampledFrames = 90; |
| 38 | + |
| 39 | + // Far enough each tick that every tick produces a state update rather than being filtered out by the |
| 40 | + // position threshold. |
| 41 | + private const float k_DistancePerTick = 1.37f; |
| 42 | + |
| 43 | + private readonly NetworkTransform.InterpolationTypes m_InterpolationType; |
| 44 | + |
| 45 | + private GameObject m_TestPrefab; |
| 46 | + private NetworkManager m_AuthorityNetworkManager; |
| 47 | + private NetworkTransform m_AuthorityInstance; |
| 48 | + private Vector3 m_Direction; |
| 49 | + |
| 50 | + public NetworkTransformInterpolationRenderTimeTests(NetworkTransform.InterpolationTypes interpolationType) |
| 51 | + { |
| 52 | + m_InterpolationType = interpolationType; |
| 53 | + } |
| 54 | + |
| 55 | + protected override void OnServerAndClientsCreated() |
| 56 | + { |
| 57 | + m_TestPrefab = CreateNetworkObjectPrefab("RenderTimeTestObj"); |
| 58 | + var networkTransform = m_TestPrefab.AddComponent<NetworkTransform>(); |
| 59 | + networkTransform.PositionInterpolationType = m_InterpolationType; |
| 60 | + base.OnServerAndClientsCreated(); |
| 61 | + } |
| 62 | + |
| 63 | + private static double GetTickInterval(NetworkManager networkManager) |
| 64 | + { |
| 65 | + return 1.0d / networkManager.NetworkTickSystem.TickRate; |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// How far LocalTime currently leads ServerTime, expressed in ticks. |
| 70 | + /// </summary> |
| 71 | + private static double GetClockLeadInTicks(NetworkManager networkManager) |
| 72 | + { |
| 73 | + return (networkManager.LocalTime.Time - networkManager.ServerTime.Time) / GetTickInterval(networkManager); |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Moves the authority instance once per tick so that a state update is generated every tick. |
| 78 | + /// </summary> |
| 79 | + private void OnNetworkTick() |
| 80 | + { |
| 81 | + m_AuthorityInstance.transform.position += m_Direction * k_DistancePerTick; |
| 82 | + } |
| 83 | + |
| 84 | + [UnityTest] |
| 85 | + public IEnumerator RenderTimeTrailsTheServerClock() |
| 86 | + { |
| 87 | + m_AuthorityNetworkManager = GetAuthorityNetworkManager(); |
| 88 | + m_AuthorityInstance = SpawnObject(m_TestPrefab, m_AuthorityNetworkManager).GetComponent<NetworkTransform>(); |
| 89 | + |
| 90 | + yield return WaitForSpawnedOnAllOrTimeOut(m_AuthorityInstance.NetworkObject); |
| 91 | + AssertOnTimeout($"Not all clients spawned {m_AuthorityInstance.name}!"); |
| 92 | + |
| 93 | + var nonAuthority = GetNonAuthorityNetworkManager(); |
| 94 | + var nonAuthorityInstance = nonAuthority.SpawnManager.SpawnedObjects[m_AuthorityInstance.NetworkObject.NetworkObjectId].GetComponent<NetworkTransform>(); |
| 95 | + |
| 96 | + // Separate the two clocks by a known amount so that which one the render time is derived from is |
| 97 | + // actually distinguishable. |
| 98 | + nonAuthority.NetworkTimeSystem.LocalBufferSec = k_LocalBufferTicks * GetTickInterval(nonAuthority); |
| 99 | + |
| 100 | + // Start continuous motion on the authority. |
| 101 | + m_Direction = GetRandomVector3(-10, 10).normalized; |
| 102 | + m_AuthorityNetworkManager.NetworkTickSystem.Tick += OnNetworkTick; |
| 103 | + |
| 104 | + // The offset only moves when the client next receives a time sync, so wait for the separation to |
| 105 | + // actually take hold rather than assuming it has. |
| 106 | + yield return WaitForConditionOrTimeOut(() => GetClockLeadInTicks(nonAuthority) >= k_RequiredLeadTicks); |
| 107 | + AssertOnTimeout($"The nonAuthority clock never fell {k_RequiredLeadTicks} ticks behind, so this test " + |
| 108 | + "cannot tell the two clocks apart and would pass regardless of which one is used."); |
| 109 | + |
| 110 | + // Let the interpolator settle at the new separation before measuring. |
| 111 | + yield return WaitForTicks(m_AuthorityNetworkManager, k_WarmUpTicks); |
| 112 | + |
| 113 | + // Sample how far behind ServerTime the state being interpolated towards was sent. |
| 114 | + var interpolator = nonAuthorityInstance.GetPositionInterpolator(); |
| 115 | + var totalTargetLagTicks = 0.0d; |
| 116 | + var totalBuffered = 0; |
| 117 | + var samples = 0; |
| 118 | + for (int frame = 0; frame < k_SampledFrames; frame++) |
| 119 | + { |
| 120 | + if (interpolator.InterpolateState.Target.HasValue) |
| 121 | + { |
| 122 | + var targetLag = nonAuthority.ServerTime.Time - interpolator.InterpolateState.Target.Value.TimeSent; |
| 123 | + totalTargetLagTicks += targetLag / GetTickInterval(nonAuthority); |
| 124 | + totalBuffered += interpolator.m_BufferQueue.Count; |
| 125 | + samples++; |
| 126 | + } |
| 127 | + yield return null; |
| 128 | + } |
| 129 | + |
| 130 | + m_AuthorityNetworkManager.NetworkTickSystem.Tick -= OnNetworkTick; |
| 131 | + |
| 132 | + Assert.Greater(samples, 0, $"{nonAuthorityInstance.name} never had a state to interpolate towards!"); |
| 133 | + |
| 134 | + var meanTargetLagTicks = totalTargetLagTicks / samples; |
| 135 | + var meanBuffered = totalBuffered / (float)samples; |
| 136 | + var tickLatency = nonAuthority.NetworkTimeSystem.TickLatency; |
| 137 | + |
| 138 | + // Anything less than the tick latency means the render time came from a clock that leads the one |
| 139 | + // the states are stamped on. |
| 140 | + Assert.GreaterOrEqual(meanTargetLagTicks, tickLatency, |
| 141 | + $"[{m_InterpolationType}] {nonAuthorityInstance.name} was interpolating towards a state sent " + |
| 142 | + $"{meanTargetLagTicks:F3} ticks behind the server clock, but the render time is the server " + |
| 143 | + $"clock minus a tick latency of {tickLatency}, so it should never be less than that. " + |
| 144 | + $"(clock lead {GetClockLeadInTicks(nonAuthority):F3} ticks, mean buffered {meanBuffered:F3})"); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments