This repository has been archived on 2025-04-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
com.unity.netcode.gameobjects/Tests/Editor/Timing/ClientNetworkTimeSystemTests.cs
Unity Technologies 63c7e4c78a com.unity.netcode.gameobjects@2.0.0-exp.4
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com).

## [2.0.0-exp.4] - 2024-05-31

### Added

- Added `NetworkRigidbodyBase.AttachToFixedJoint` and `NetworkRigidbodyBase.DetachFromFixedJoint` to replace parenting for rigid bodies that have `NetworkRigidbodyBase.UseRigidBodyForMotion` enabled. (#2933)
- Added `NetworkBehaviour.OnNetworkPreSpawn` and `NetworkBehaviour.OnNetworkPostSpawn` methods that provide the ability to handle pre and post spawning actions during the `NetworkObject` spawn sequence. (#2912)
- Added a client-side only `NetworkBehaviour.OnNetworkSessionSynchronized` convenience method that is invoked on all `NetworkBehaviour`s after a newly joined client has finished synchronizing with the network session in progress. (#2912)
- Added `NetworkBehaviour.OnInSceneObjectsSpawned` convenience method that is invoked when all in-scene `NetworkObject`s have been spawned after a scene has been loaded or upon a host or server starting. (#2912)

### Fixed

- Fixed issue where non-authoritative rigid bodies with `NetworkRigidbodyBase.UseRigidBodyForMotion` enabled would constantly log errors about the renderTime being before `StartTimeConsumed`. (#2933)
- Fixed issue where in-scene placed NetworkObjects could be destroyed if a client disconnects early and/or before approval. (#2924)
- Fixed issue where a `NetworkObject` component's associated `NetworkBehaviour` components would not be detected if scene loading is disabled in the editor and the currently loaded scene has in-scene placed `NetworkObject`s. (#2912)
- Fixed issue where an in-scene placed `NetworkObject` with `NetworkTransform` that is also parented under a `GameObject` would not properly synchronize when the parent `GameObject` had a world space position other than 0,0,0. (#2898)

### Changed

- Change all the access modifiers of test class from Public to Internal (#2930)
- Changed messages are now sorted by enum values as opposed to ordinally sorting the messages by their type name. (#2929)
- Changed `NetworkClient.SessionModeTypes` to `NetworkClient.NetworkTopologyTypes`. (#2875)
- Changed `NetworkClient.SessionModeType` to `NetworkClient.NetworkTopologyType`. (#2875)
- Changed `NetworkConfig.SessionMode` to `NeworkConfig.NetworkTopology`. (#2875)
2024-05-31 00:00:00 +00:00

183 lines
7.9 KiB
C#

using System;
using NUnit.Framework;
using UnityEngine;
namespace Unity.Netcode.EditorTests
{
/// <summary>
/// Tests for running a <see cref="NetworkTimeSystem"/> as a client.
/// </summary>
internal class ClientNetworkTimeSystemTests
{
private const double k_AcceptableRttOffset = 0.03d; // 30ms offset is fine
/// <summary>
/// Tests whether time is stable if RTT is stable.
/// </summary>
[Test]
public void StableRttTest()
{
double receivedServerTime = 2;
var timeSystem = new NetworkTimeSystem(0.05d, 0.05d, 0.1d);
timeSystem.Reset(receivedServerTime, 0.15);
var tickSystem = new NetworkTickSystem(60, timeSystem.LocalTime, timeSystem.ServerTime);
Assert.True(timeSystem.LocalTime > 2);
var steps = TimingTestHelper.GetRandomTimeSteps(100f, 0.01f, 0.1f, 42);
var rttSteps = TimingTestHelper.GetRandomTimeSteps(1000f, 0.095f, 0.105f, 42); // 10ms jitter
// run for a while so that we reach regular RTT offset
TimingTestHelper.ApplySteps(timeSystem, tickSystem, steps, delegate (int step)
{
// sync network stats
receivedServerTime += steps[step];
timeSystem.Sync(receivedServerTime, rttSteps[step]);
});
// check how we close we are to target time.
var expectedRtt = 0.1d;
var offsetToTarget = (timeSystem.LocalTime - timeSystem.ServerTime) - expectedRtt - timeSystem.ServerBufferSec - timeSystem.LocalBufferSec;
Debug.Log($"offset to target time after running for a while: {offsetToTarget}");
Assert.IsTrue(Math.Abs(offsetToTarget) < k_AcceptableRttOffset);
// run again, test that we never need to speed up or slow down under stable RTT
TimingTestHelper.ApplySteps(timeSystem, tickSystem, steps, delegate (int step)
{
// sync network stats
receivedServerTime += steps[step];
timeSystem.Sync(receivedServerTime, rttSteps[step]);
});
// check again to ensure we are still close to the target
var newOffsetToTarget = (timeSystem.LocalTime - timeSystem.ServerTime) - expectedRtt - timeSystem.ServerBufferSec - timeSystem.LocalBufferSec;
Debug.Log($"offset to target time after running longer: {newOffsetToTarget}");
Assert.IsTrue(Math.Abs(newOffsetToTarget) < k_AcceptableRttOffset);
// difference between first and second offset should be minimal
var dif = offsetToTarget - newOffsetToTarget;
Assert.IsTrue(Math.Abs(dif) < 0.01d); // less than 10ms
}
/// <summary>
/// Tests whether local time can speed up and slow down to catch up when RTT changes.
/// </summary>
[Test]
public void RttCatchupSlowdownTest()
{
double receivedServerTime = 2;
var timeSystem = new NetworkTimeSystem(0.05d, 0.05d, 0.1d);
timeSystem.Reset(receivedServerTime, 0.15);
var tickSystem = new NetworkTickSystem(60, timeSystem.LocalTime, timeSystem.ServerTime);
var steps = TimingTestHelper.GetRandomTimeSteps(100f, 0.01f, 0.1f, 42);
var rttSteps = TimingTestHelper.GetRandomTimeSteps(1000f, 0.095f, 0.105f, 42); // 10ms jitter
// run for a while so that we reach regular RTT offset
TimingTestHelper.ApplySteps(timeSystem, tickSystem, steps, delegate (int step)
{
// sync network stats
receivedServerTime += steps[step];
timeSystem.Sync(receivedServerTime, rttSteps[step]);
});
// increase RTT to ~200ms from ~100ms
var rttSteps2 = TimingTestHelper.GetRandomTimeSteps(1000f, 0.195f, 0.205f, 42);
double unscaledLocalTime = timeSystem.LocalTime;
double unscaledServerTime = timeSystem.ServerTime;
TimingTestHelper.ApplySteps(timeSystem, tickSystem, steps, delegate (int step)
{
// sync network stats
unscaledLocalTime += steps[step];
unscaledServerTime += steps[step];
receivedServerTime += steps[step];
timeSystem.Sync(receivedServerTime, rttSteps2[step]);
});
var totalLocalSpeedUpTime = timeSystem.LocalTime - unscaledLocalTime;
var totalServerSpeedUpTime = timeSystem.ServerTime - unscaledServerTime;
// speed up of 0.1f expected
Debug.Log($"Total local speed up time catch up: {totalLocalSpeedUpTime}");
Assert.True(Math.Abs(totalLocalSpeedUpTime - 0.1) < k_AcceptableRttOffset);
Assert.True(Math.Abs(totalServerSpeedUpTime) < k_AcceptableRttOffset); // server speedup/slowdowns should not be affected by RTT
// run again with RTT ~100ms and see whether we slow down by -0.1f
unscaledLocalTime = timeSystem.LocalTime;
unscaledServerTime = timeSystem.ServerTime;
TimingTestHelper.ApplySteps(timeSystem, tickSystem, steps, delegate (int step)
{
// sync network stats
unscaledLocalTime += steps[step];
unscaledServerTime += steps[step];
receivedServerTime += steps[step];
timeSystem.Sync(receivedServerTime, rttSteps[step]);
});
totalLocalSpeedUpTime = timeSystem.LocalTime - unscaledLocalTime;
totalServerSpeedUpTime = timeSystem.ServerTime - unscaledServerTime;
// slow down of 0.1f expected
Debug.Log($"Total local speed up time slow down: {totalLocalSpeedUpTime}");
Assert.True(Math.Abs(totalLocalSpeedUpTime + 0.1) < k_AcceptableRttOffset);
Assert.True(Math.Abs(totalServerSpeedUpTime) < k_AcceptableRttOffset); // server speedup/slowdowns should not be affected by RTT
}
/// <summary>
/// Tests whether time resets when there is a huge spike in RTT and is able to stabilize again.
/// </summary>
[Test]
public void ResetTest()
{
double receivedServerTime = 2;
var timeSystem = new NetworkTimeSystem(0.05d, 0.05d, 0.1d);
timeSystem.Reset(receivedServerTime, 0.15);
var tickSystem = new NetworkTickSystem(60, timeSystem.LocalTime, timeSystem.ServerTime);
var steps = TimingTestHelper.GetRandomTimeSteps(100f, 0.01f, 0.1f, 42);
var rttSteps = TimingTestHelper.GetRandomTimeSteps(1000f, 0.095f, 0.105f, 42); // 10ms jitter
// run for a while so that we reach regular RTT offset
TimingTestHelper.ApplySteps(timeSystem, tickSystem, steps, delegate (int step)
{
// sync network stats
receivedServerTime += steps[step];
timeSystem.Sync(receivedServerTime, rttSteps[step]);
});
// increase RTT to ~500ms from ~100ms
var rttSteps2 = TimingTestHelper.GetRandomTimeSteps(1000f, 0.495f, 0.505f, 42);
// run a single advance expect a hard rest
receivedServerTime += 1 / 60d;
timeSystem.Sync(receivedServerTime, 0.5);
bool reset = timeSystem.Advance(1 / 60d);
Assert.IsTrue(reset);
TimingTestHelper.ApplySteps(timeSystem, tickSystem, steps, delegate (int step, bool reset)
{
Assert.IsFalse(reset);
// sync network stats
receivedServerTime += steps[step];
timeSystem.Sync(receivedServerTime, rttSteps2[step]);
// after hard reset time should stay close to rtt
var expectedRtt = 0.5d;
Assert.IsTrue(Math.Abs((timeSystem.LocalTime - timeSystem.ServerTime) - expectedRtt - timeSystem.ServerBufferSec - timeSystem.LocalBufferSec) < k_AcceptableRttOffset);
});
}
}
}