Skip to content

Commit d06eead

Browse files
test: Tolerate an adaptive tick latency in the tick latency tests
NetworkTimeSystem.TickLatency is recomputed from the averaged round trip time and can legitimately change mid-run. Both tests assumed it would not, and one failed on macOS when it moved from two ticks to three, reporting the value as having gone from 0.0666s to 0.1s. The duration is now only held to being unchanged across samples where the tick latency itself did not change, and the buffer offset test accounts for any tick latency movement between its two samples so that only the buffering is held to an exact figure. Both still fail against the previous absolute timestamp implementation.
1 parent e0a0191 commit d06eead

1 file changed

Lines changed: 26 additions & 10 deletions

File tree

com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformTickLatencyTests.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,33 @@ public IEnumerator GetTickLatencyInSecondsReturnsADuration()
5454
{
5555
var client = m_ClientNetworkManagers[0];
5656

57-
// Sample repeatedly while the session clock advances. A duration tracks the tick latency and stays
58-
// put, where an absolute timestamp would climb by roughly one second per second.
59-
var firstSample = NetworkTransform.GetTickLatencyInSeconds(client);
57+
// Sample repeatedly while the session clock advances. A duration tracks the tick latency, where an
58+
// absolute timestamp would climb by roughly one second per second.
59+
//
60+
// NetworkTimeSystem.TickLatency is adaptive and can legitimately change mid-run, so the value is
61+
// only held to being unchanged across samples where the tick latency itself did not change.
62+
var previousTicksBehind = -1;
63+
var previousValue = 0f;
6064
for (int i = 0; i < k_Samples; i++)
6165
{
66+
var ticksBehind = client.NetworkTimeSystem.TickLatency + NetworkTransform.InterpolationBufferTickOffset;
6267
var expected = GetExpectedLatencyInSeconds(client);
6368
var actual = NetworkTransform.GetTickLatencyInSeconds(client);
69+
6470
Assert.AreEqual(expected, actual, k_Tolerance,
6571
$"Expected the tick latency to be {expected}s but it was {actual}s.");
72+
73+
if (ticksBehind == previousTicksBehind)
74+
{
75+
Assert.AreEqual(previousValue, actual, k_Tolerance,
76+
$"The reported latency moved from {previousValue}s to {actual}s while the tick latency " +
77+
$"stayed at {ticksBehind} ticks, so it is tracking elapsed time rather than latency.");
78+
}
79+
80+
previousTicksBehind = ticksBehind;
81+
previousValue = actual;
6682
yield return null;
6783
}
68-
69-
var lastSample = NetworkTransform.GetTickLatencyInSeconds(client);
70-
Assert.AreEqual(firstSample, lastSample, k_Tolerance,
71-
$"The tick latency changed from {firstSample}s to {lastSample}s while the session ran without " +
72-
$"the tick latency itself changing, so it is tracking elapsed time rather than latency.");
7384
}
7485

7586
[UnityTest]
@@ -78,18 +89,23 @@ public IEnumerator GetTickLatencyInSecondsTracksTheBufferTickOffset()
7889
var client = m_ClientNetworkManagers[0];
7990
var tickInterval = (float)client.ServerTime.FixedDeltaTimeAsDouble;
8091

92+
var latencyBefore = client.NetworkTimeSystem.TickLatency;
8193
var before = NetworkTransform.GetTickLatencyInSeconds(client);
8294

8395
// Buffering more ticks has to lengthen the reported duration by exactly those ticks.
8496
NetworkTransform.InterpolationBufferTickOffset = m_OriginalBufferTickOffset + k_AddedBufferTicks;
8597
yield return null;
8698

99+
var latencyAfter = client.NetworkTimeSystem.TickLatency;
87100
var after = NetworkTransform.GetTickLatencyInSeconds(client);
88-
var expectedIncrease = k_AddedBufferTicks * tickInterval;
101+
102+
// The adaptive tick latency may also have moved in between, so only the buffering is held to an
103+
// exact figure.
104+
var expectedIncrease = (k_AddedBufferTicks + (latencyAfter - latencyBefore)) * tickInterval;
89105
Assert.AreEqual(expectedIncrease, after - before, k_Tolerance,
90106
$"Adding {k_AddedBufferTicks} ticks of buffering changed the reported latency by " +
91107
$"{after - before}s when a tick is {tickInterval}s, so it should have changed by " +
92-
$"{expectedIncrease}s.");
108+
$"{expectedIncrease}s (tick latency went from {latencyBefore} to {latencyAfter}).");
93109
}
94110
}
95111
}

0 commit comments

Comments
 (0)