-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathNetworkBehaviourGenericTests.cs
More file actions
228 lines (186 loc) · 9.15 KB
/
NetworkBehaviourGenericTests.cs
File metadata and controls
228 lines (186 loc) · 9.15 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
227
228
using System.Collections;
using NUnit.Framework;
using Unity.Netcode.Components;
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine;
using UnityEngine.TestTools;
namespace Unity.Netcode.RuntimeTests
{
/// <summary>
/// This class is for testing general fixes or functionality of NetworkBehaviours
/// </summary>
internal class NetworkBehaviourGenericTests : NetcodeIntegrationTest
{
protected override int NumberOfClients => 0;
private bool m_AllowServerToStart;
private GameObject m_PrefabToSpawn;
protected override bool CanStartServerAndClients()
{
return m_AllowServerToStart;
}
internal class SimpleNetworkBehaviour : NetworkBehaviour
{
public bool OnNetworkDespawnCalled;
public override void OnNetworkDespawn()
{
OnNetworkDespawnCalled = true;
base.OnNetworkDespawn();
}
}
protected override void OnServerAndClientsCreated()
{
m_PrefabToSpawn = CreateNetworkObjectPrefab("TestPrefab");
var childObject = new GameObject
{
name = "ChildObject"
};
childObject.transform.parent = m_PrefabToSpawn.transform;
childObject.AddComponent<NetworkTransform>();
base.OnServerAndClientsCreated();
}
protected override IEnumerator OnSetup()
{
m_AllowServerToStart = false;
return base.OnSetup();
}
protected override void OnNewClientCreated(NetworkManager networkManager)
{
networkManager.NetworkConfig.Prefabs.Add(new NetworkPrefab()
{
Prefab = m_PrefabToSpawn,
});
base.OnNewClientCreated(networkManager);
}
/// <summary>
/// This validates:
/// - The fix for when a child GameObject with a NetworkBehaviour
/// is deleted while the parent GameObject with a NetworkObject is spawned and
/// is not deleted until a later time would cause an exception due to the
/// NetworkBehaviour not being removed from the NetworkObject.ChildNetworkBehaviours
/// list.
/// - That when a child GameObject is deleted/disabled or a NetworkBehaviour is disabled
/// a message is logged and the NetworkObject still can be spawned and synchronized.
/// </summary>
[UnityTest]
public IEnumerator ValidatedDisableddNetworkBehaviourWarning([Values] bool disableGameObject)
{
m_AllowServerToStart = true;
// Now just start the Host
yield return StartServerAndClients();
// Now join a new client to make sure a connected client spawns the instance.
yield return CreateAndStartNewClient();
// Adjust the prefab to either have the child GameObject completely disabled or the NetworkBehaviour
// disabled.
var childBehaviour = m_PrefabToSpawn.GetComponentInChildren<NetworkTransform>(true);
if (disableGameObject)
{
childBehaviour.enabled = true;
childBehaviour.gameObject.SetActive(false);
}
else
{
childBehaviour.enabled = false;
childBehaviour.gameObject.SetActive(true);
}
// Now create an instance of the prefab
var instance = Object.Instantiate(m_PrefabToSpawn);
var instanceNetworkObject = instance.GetComponent<NetworkObject>();
// Generate the expected warning message
var expectedWarning = instanceNetworkObject.GenerateDisabledNetworkBehaviourWarning(instanceNetworkObject.GetComponentInChildren<NetworkTransform>(true));
// Spawn the instance
SpawnObjectInstance(instanceNetworkObject, m_ServerNetworkManager);
LogAssert.Expect(LogType.Warning, $"{expectedWarning}");
// Asure the connected client spawned the object first
yield return WaitForSpawnedOnAllOrTimeOut(instanceNetworkObject);
AssertOnTimeout($"Not all clients spawned {instanceNetworkObject.name}!");
// Now join a new client to make sure the client synchronizes with the disabled GameObject or NetworkBehaviour component.
yield return CreateAndStartNewClient();
// Asure the newly connected client synchronizes the spawned object correctly
yield return WaitForSpawnedOnAllOrTimeOut(instanceNetworkObject);
AssertOnTimeout($"Not all clients spawned {instanceNetworkObject.name}!");
}
/// <summary>
/// This test validates a fix to NetworkBehaviour.NetworkObject when
/// the NetworkManager.LogLevel is set to Developer
/// Note: This test does not require any clients, but should not impact this
/// particular test if new tests are added to this class that do require clients
/// </summary>
[UnityTest]
public IEnumerator ValidateNoSpam()
{
m_AllowServerToStart = true;
var objectToTest = new GameObject();
var simpleNetworkBehaviour = objectToTest.AddComponent<SimpleNetworkBehaviour>();
// Now just start the Host
yield return StartServerAndClients();
// set the log level to developer
m_ServerNetworkManager.LogLevel = LogLevel.Developer;
// The only valid condition for this would be if the NetworkBehaviour is spawned.
simpleNetworkBehaviour.IsSpawned = true;
// Verify the warning gets logged under normal conditions
var isNull = simpleNetworkBehaviour.NetworkObject == null;
LogAssert.Expect(LogType.Warning, $"[Netcode] Could not get {nameof(NetworkObject)} for the {nameof(NetworkBehaviour)}. Are you missing a {nameof(NetworkObject)} component?");
var networkObjectToTest = objectToTest.AddComponent<NetworkObject>();
networkObjectToTest.NetworkManagerOwner = m_ServerNetworkManager;
networkObjectToTest.Spawn();
// Assure no log messages are logged when they should not be logged
isNull = simpleNetworkBehaviour.NetworkObject != null;
LogAssert.NoUnexpectedReceived();
networkObjectToTest.Despawn();
Object.Destroy(networkObjectToTest);
}
/// <summary>
/// This validates the fix for when a child GameObject with a NetworkBehaviour
/// is deleted while the parent GameObject with a NetworkObject is spawned and
/// is not deleted until a later time would cause an exception due to the
/// NetworkBehaviour not being removed from the NetworkObject.ChildNetworkBehaviours
/// list.
/// </summary>
[UnityTest]
public IEnumerator ValidateDeleteChildNetworkBehaviour()
{
m_AllowServerToStart = true;
yield return s_DefaultWaitForTick;
// Now just start the Host
yield return StartServerAndClients();
var parentObject = new GameObject();
var childObject = new GameObject();
childObject.transform.parent = parentObject.transform;
var parentNetworkObject = parentObject.AddComponent<NetworkObject>();
childObject.AddComponent<SimpleNetworkBehaviour>();
parentNetworkObject.Spawn();
yield return s_DefaultWaitForTick;
// Destroy the child object with child NetworkBehaviour
Object.Destroy(childObject);
yield return s_DefaultWaitForTick;
// Assure no log messages are logged when they should not be logged
LogAssert.NoUnexpectedReceived();
// Destroy the parent object which should not cause any exceptions
// (validating the fix)
Object.Destroy(parentObject);
}
protected override void OnPlayerPrefabGameObjectCreated()
{
// Adds the SimpleNetworkBehaviour before the NetworkObject
// for OnNetworkDespawnInvokedWhenClientDisconnects testing
m_PlayerPrefab.AddComponent<SimpleNetworkBehaviour>();
}
/// <summary>
/// This validates that upon a client disconnecting, the server-side
/// client's player clone will invoke NetworkBehaviour.OnNetworkDespawn
/// when the component precedes the NetworkObject component.(PR-2323)
/// </summary>
[UnityTest]
public IEnumerator OnNetworkDespawnInvokedWhenClientDisconnects()
{
m_AllowServerToStart = true;
// Now just start the Host
yield return StartServerAndClients();
// Now create and connect a new client
yield return CreateAndStartNewClient();
var serverSidePlayer = m_PlayerNetworkObjects[NetworkManager.ServerClientId][m_ClientNetworkManagers[0].LocalClientId].GetComponent<SimpleNetworkBehaviour>();
yield return StopOneClient(m_ClientNetworkManagers[0]);
Assert.True(serverSidePlayer.OnNetworkDespawnCalled, $"Server-side player clone did not invoke OnNetworkDespawn!");
}
}
}