-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathNetworkTimeSystemTests.cs
More file actions
226 lines (178 loc) · 9.03 KB
/
NetworkTimeSystemTests.cs
File metadata and controls
226 lines (178 loc) · 9.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using System.Collections;
using NUnit.Framework;
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine;
using UnityEngine.Assertions.Comparers;
using UnityEngine.TestTools;
namespace Unity.Netcode.RuntimeTests
{
/// <summary>
/// Runtime tests to test the network time system with the Unity player loop.
/// </summary>
internal class NetworkTimeSystemTests
{
private MonoBehaviourTest<PlayerLoopFixedTimeTestComponent> m_PlayerLoopFixedTimeTestComponent; // cache for teardown
private MonoBehaviourTest<PlayerLoopTimeTestComponent> m_PlayerLoopTimeTestComponent; // cache for teardown
private float m_OriginalTimeScale = 1.0f;
[SetUp]
public void Setup()
{
m_OriginalTimeScale = Time.timeScale;
// Create, instantiate, and host
Assert.IsTrue(NetworkManagerHelper.StartNetworkManager(out _));
}
/// <summary>
/// Tests whether time is accessible and has correct values inside Update/FixedUpdate.
/// This test applies only when <see cref="Time.timeScale"> is 1.
/// </summary>
/// <returns></returns>
[UnityTest]
public IEnumerator PlayerLoopFixedTimeTest()
{
m_PlayerLoopFixedTimeTestComponent = new MonoBehaviourTest<PlayerLoopFixedTimeTestComponent>();
yield return m_PlayerLoopFixedTimeTestComponent;
}
/// <summary>
/// Tests whether time is accessible and has correct values inside Update, for multiples <see cref="Time.timeScale"/> values.
/// </summary>
/// <returns></returns>
[UnityTest]
public IEnumerator PlayerLoopTimeTest_WithDifferentTimeScale([Values(0.0f, 0.1f, 0.5f, 1.0f, 2.0f, 5.0f)] float timeScale)
{
Time.timeScale = timeScale;
m_PlayerLoopTimeTestComponent = new MonoBehaviourTest<PlayerLoopTimeTestComponent>();
yield return m_PlayerLoopTimeTestComponent;
}
/// <summary>
/// Tests whether the time system invokes the correct amount of ticks over a period of time.
/// Note we cannot test against Time.Time directly because of floating point precision. Our time is more precise leading to different results.
/// </summary>
/// <returns></returns>
[UnityTest]
public IEnumerator CorrectAmountTicksTest()
{
NetworkTickSystem tickSystem = NetworkManager.Singleton.NetworkTickSystem;
double delta = tickSystem.LocalTime.FixedDeltaTimeAsDouble;
int previous_localTickCalculated = 0;
int previous_serverTickCalculated = 0;
while (tickSystem.LocalTime.Time < 3f)
{
yield return null;
var tickCalculated = tickSystem.LocalTime.Time / delta;
previous_localTickCalculated = (int)tickCalculated;
// This check is needed due to double division imprecision of large numbers
if ((tickCalculated - previous_localTickCalculated) >= 0.999999999999)
{
previous_localTickCalculated++;
}
tickCalculated = NetworkManager.Singleton.ServerTime.Time / delta;
previous_serverTickCalculated = (int)tickCalculated;
// This check is needed due to double division imprecision of large numbers
if ((tickCalculated - previous_serverTickCalculated) >= 0.999999999999)
{
previous_serverTickCalculated++;
}
Assert.AreEqual(previous_localTickCalculated, NetworkManager.Singleton.LocalTime.Tick, $"Calculated local tick {previous_localTickCalculated} does not match local tick {NetworkManager.Singleton.LocalTime.Tick}!");
Assert.AreEqual(previous_serverTickCalculated, NetworkManager.Singleton.ServerTime.Tick, $"Calculated server tick {previous_serverTickCalculated} does not match server tick {NetworkManager.Singleton.ServerTime.Tick}!");
Assert.AreEqual((float)NetworkManager.Singleton.LocalTime.Time, (float)NetworkManager.Singleton.ServerTime.Time, $"Local time {(float)NetworkManager.Singleton.LocalTime.Time} is not approximately server time {(float)NetworkManager.Singleton.ServerTime.Time}!", FloatComparer.s_ComparerWithDefaultTolerance);
}
}
[TearDown]
public void TearDown()
{
// Stop, shutdown, and destroy
NetworkManagerHelper.ShutdownNetworkManager();
Time.timeScale = m_OriginalTimeScale;
if (m_PlayerLoopFixedTimeTestComponent != null)
{
Object.DestroyImmediate(m_PlayerLoopFixedTimeTestComponent.gameObject);
m_PlayerLoopFixedTimeTestComponent = null;
}
if (m_PlayerLoopTimeTestComponent != null)
{
Object.DestroyImmediate(m_PlayerLoopTimeTestComponent.gameObject);
m_PlayerLoopTimeTestComponent = null;
}
}
}
internal class PlayerLoopFixedTimeTestComponent : MonoBehaviour, IMonoBehaviourTest
{
public const int Passes = 100;
private int m_UpdatePasses = 0;
private int m_LastFixedUpdateTick = 0;
private int m_TickOffset = -1;
private NetworkTime m_LocalTimePreviousUpdate;
private NetworkTime m_ServerTimePreviousUpdate;
private NetworkTime m_LocalTimePreviousFixedUpdate;
private void Start()
{
// Run fixed update at same rate as network tick
Time.fixedDeltaTime = NetworkManager.Singleton.LocalTime.FixedDeltaTime;
// Uncap fixed time else we might skip fixed updates
Time.maximumDeltaTime = float.MaxValue;
}
private void Update()
{
// This must run first else it wont run if there is an exception
m_UpdatePasses++;
NetworkTime localTime = NetworkManager.Singleton.LocalTime;
NetworkTime serverTime = NetworkManager.Singleton.ServerTime;
// time should have advanced on the host/server
Assert.Less(m_LocalTimePreviousUpdate.Time, localTime.Time);
Assert.Less(m_ServerTimePreviousUpdate.Time, serverTime.Time);
// time should be further then last fixed step in update
Assert.Less(m_LocalTimePreviousFixedUpdate.FixedTime, localTime.Time);
// we should be in same or further tick then fixed update
Assert.LessOrEqual(m_LocalTimePreviousFixedUpdate.Tick, localTime.Tick);
// fixed update should result in same amounts of tick as network time
if (m_TickOffset == -1)
{
m_TickOffset = serverTime.Tick - m_LastFixedUpdateTick;
}
else
{
// offset of 1 is ok, this happens due to different tick duration offsets
Assert.LessOrEqual(Mathf.Abs(serverTime.Tick - m_TickOffset - m_LastFixedUpdateTick), 1);
}
m_LocalTimePreviousUpdate = localTime;
m_ServerTimePreviousUpdate = serverTime;
}
private void FixedUpdate()
{
m_LocalTimePreviousFixedUpdate = NetworkManager.Singleton.LocalTime;
Assert.AreEqual(Time.fixedDeltaTime, m_LocalTimePreviousFixedUpdate.FixedDeltaTime);
Assert.AreEqual((float)NetworkManager.Singleton.LocalTime.Time, (float)NetworkManager.Singleton.ServerTime.Time, null, FloatComparer.s_ComparerWithDefaultTolerance);
m_LastFixedUpdateTick++;
}
public bool IsTestFinished => m_UpdatePasses >= Passes;
}
internal class PlayerLoopTimeTestComponent : MonoBehaviour, IMonoBehaviourTest
{
public const int Passes = 100;
private int m_UpdatePasses = 0;
private NetworkTime m_LocalTimePreviousUpdate;
private NetworkTime m_ServerTimePreviousUpdate;
private NetworkTime m_LocalTimePreviousFixedUpdate;
private void Update()
{
// This must run first else it wont run if there is an exception
m_UpdatePasses++;
NetworkTime localTime = NetworkManager.Singleton.LocalTime;
NetworkTime serverTime = NetworkManager.Singleton.ServerTime;
// time should have advanced on the host/server
Assert.Less(m_LocalTimePreviousUpdate.Time, localTime.Time);
Assert.Less(m_ServerTimePreviousUpdate.Time, serverTime.Time);
// time should be further then last fixed step in update
Assert.Less(m_LocalTimePreviousFixedUpdate.FixedTime, localTime.Time);
// we should be in same or further tick then fixed update
Assert.LessOrEqual(m_LocalTimePreviousFixedUpdate.Tick, localTime.Tick);
m_LocalTimePreviousUpdate = localTime;
m_ServerTimePreviousUpdate = serverTime;
}
private void FixedUpdate()
{
m_LocalTimePreviousFixedUpdate = NetworkManager.Singleton.LocalTime;
}
public bool IsTestFinished => m_UpdatePasses >= Passes;
}
}