Skip to content

Commit a3532b7

Browse files
NoelStephensUnitymichalChrobot
authored andcommitted
fix: android time test failures due to edge case float precision errors (#3351)
`NetworkTimeSystemTests.CorrectAmountTicksTest` Using the double version of tick frequency to calculate number of ticks passed. `TimeInitializationTest.TestClientTimeInitializationOnConnect` Dividing the double version of tick frequency to calculate number of ticks passed and using the double version of `Math.Floor` before casting that to an integer. ## Changelog NA ## Testing and Documentation - Includes integration test updates. - No documentation changes or additions were necessary. <!-- Uncomment and mark items off with a * if this PR deprecates any API: ### Deprecated API - [ ] An `[Obsolete]` attribute was added along with a `(RemovedAfter yyyy-mm-dd)` entry. - [ ] An [api updater] was added. - [ ] Deprecation of the API is explained in the CHANGELOG. - [ ] The users can understand why this API was removed and what they should use instead. -->
1 parent a5df3df commit a3532b7

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

com.unity.netcode.gameobjects/Tests/Runtime/Timing/NetworkTimeSystemTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public IEnumerator PlayerLoopTimeTest_WithDifferentTimeScale([Values(0.0f, 0.1f,
6262
public IEnumerator CorrectAmountTicksTest()
6363
{
6464
NetworkTickSystem tickSystem = NetworkManager.Singleton.NetworkTickSystem;
65-
float delta = tickSystem.LocalTime.FixedDeltaTime;
65+
double delta = tickSystem.LocalTime.FixedDeltaTimeAsDouble;
6666
int previous_localTickCalculated = 0;
6767
int previous_serverTickCalculated = 0;
6868

@@ -79,7 +79,6 @@ public IEnumerator CorrectAmountTicksTest()
7979
previous_localTickCalculated++;
8080
}
8181

82-
8382
tickCalculated = NetworkManager.Singleton.ServerTime.Time / delta;
8483
previous_serverTickCalculated = (int)tickCalculated;
8584

com.unity.netcode.gameobjects/Tests/Runtime/Timing/TimeInitializationTest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ public IEnumerator TestClientTimeInitializationOnConnect([Values(0, 1f)] float s
4343
yield return new WaitUntil(() => server.NetworkTickSystem.ServerTime.Tick > 2);
4444

4545
var serverTimePassed = server.NetworkTickSystem.ServerTime.Time;
46-
var expectedServerTickCount = Mathf.FloorToInt((float)(serverTimePassed * 30));
4746

47+
// Use FixedDeltaTimeAsDouble and divide the tick frequency into the time passed to get the accurate tick count
48+
var expectedServerTickCount = (int)System.Math.Floor(serverTimePassed / server.ServerTime.FixedDeltaTimeAsDouble);
4849
var ticksPassed = server.NetworkTickSystem.ServerTime.Tick - serverTick;
49-
Assert.AreEqual(expectedServerTickCount, ticksPassed);
50+
Assert.AreEqual(expectedServerTickCount, ticksPassed, $"Calculated tick failed: Tick ({expectedServerTickCount}) TicksPassed ({ticksPassed}) Server Tick ({server.NetworkTickSystem.ServerTime.Tick}) Prev-Server Tick ({serverTick})");
5051

5152
yield return new WaitForSeconds(clientStartDelay);
5253

0 commit comments

Comments
 (0)