Skip to content

Commit 82138d5

Browse files
committed
Fix InScenePlacedNetworkObjectTests
1 parent 5b6266a commit 82138d5

File tree

6 files changed

+35
-80
lines changed

6 files changed

+35
-80
lines changed

com.unity.netcode.gameobjects/TestHelpers/Runtime/NetcodeIntegrationTest.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,6 @@ public IEnumerator SetUp()
425425

426426
if (m_SetupIsACoroutine)
427427
{
428-
// yield return NetcodeIntegrationTestHelpers.WaitBetweenCmbServiceTests(m_UseCmbService);
429428
yield return OnSetup();
430429
}
431430
else
@@ -650,6 +649,11 @@ private bool AllPlayerObjectClonesSpawned(NetworkManager joinedClient)
650649
continue;
651650
}
652651

652+
// if (networkManager.SpawnManager == null)
653+
// {
654+
// return false;
655+
// }
656+
653657
var playerObjectRelative = networkManager.SpawnManager.PlayerObjects.FirstOrDefault(c => c.OwnerClientId == joinedClient.LocalClientId);
654658
if (playerObjectRelative == null)
655659
{
@@ -963,6 +967,7 @@ protected IEnumerator StartServerAndClients()
963967
Debug.LogError("Failed to start instances");
964968
Assert.Fail("Failed to start instances");
965969
}
970+
Debug.Log($"Server has SpawnManager after start : {m_ServerNetworkManager.SpawnManager != null}");
966971

967972
// When scene management is enabled, we need to re-apply the scenes populated list since we have overriden the ISceneManagerHandler
968973
// imeplementation at this point. This assures any pre-loaded scenes will be automatically assigned to the server and force clients
@@ -990,6 +995,7 @@ protected IEnumerator StartServerAndClients()
990995
yield return WaitForClientsConnectedOrTimeOut();
991996

992997
AssertOnTimeout($"{nameof(StartServerAndClients)} timed out waiting for all clients to be connected!\n {m_InternalErrorLog}");
998+
Debug.Log($"all clients should be connected. SpawnManger exists: {m_ServerNetworkManager.SpawnManager != null}");
993999

9941000
if (m_UseHost || m_ServerNetworkManager.IsHost)
9951001
{
@@ -1017,6 +1023,7 @@ protected IEnumerator StartServerAndClients()
10171023
{
10181024
foreach (var networkManager in m_NetworkManagers)
10191025
{
1026+
Debug.Log($"Attempting to check spawn manager of client: {networkManager.LocalClientId}. Has spawned manager: {networkManager.SpawnManager != null}");
10201027
yield return WaitForConditionOrTimeOut(() => AllPlayerObjectClonesSpawned(networkManager));
10211028
AssertOnTimeout($"{nameof(CreateAndStartNewClient)} timed out waiting for all sessions to spawn Client-{networkManager.LocalClientId}'s player object!\n {m_InternalErrorLog}");
10221029
}

com.unity.netcode.gameobjects/TestHelpers/Runtime/NetcodeIntegrationTestHelpers.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ public static void StopOneClient(NetworkManager clientToStop, bool destroy = tru
341341
/// <summary>
342342
/// Starts one single client and makes sure to register the required hooks and handlers
343343
/// </summary>
344+
/// <remarks>
345+
/// Do not call this function directly. Use <see cref="NetcodeIntegrationTest.CreateAndStartNewClient"/> instead.
346+
/// </remarks>
344347
/// <param name="clientToStart">The NetworkManager instance to start</param>
345348
public static void StartOneClient(NetworkManager clientToStart)
346349
{

testproject/Assets/Tests/Runtime/AddressablesTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public class AddressablesTests : NetcodeIntegrationTest
2727

2828
public AddressablesTests(HostOrServer hostOrServer)
2929
{
30-
m_EnableVerboseDebug = true;
3130
m_UseHost = hostOrServer == HostOrServer.Host;
3231
}
3332
protected override NetworkManagerInstatiationMode OnSetIntegrationTestMode()

testproject/Assets/Tests/Runtime/NetworkSceneManager/InScenePlacedNetworkObjectTests.cs

Lines changed: 21 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ namespace TestProject.RuntimeTests
1818
[TestFixture(NetworkTopologyTypes.ClientServer, HostOrServer.Server)]
1919
public class InScenePlacedNetworkObjectTests : IntegrationTestWithApproximation
2020
{
21-
protected override int NumberOfClients => 3;
21+
protected override int NumberOfClients => 2;
2222

2323
private const string k_SceneToLoad = "InSceneNetworkObject";
2424
private const string k_InSceneUnder = "InSceneUnderGameObject";
2525
private const string k_InSceneUnderWithNT = "InSceneUnderGameObjectWithNT";
2626
private Scene m_ServerSideSceneLoaded;
27-
private bool m_CanStartServerAndClients;
2827
private string m_SceneLoading = k_SceneToLoad;
28+
private NetworkManager m_LateJoinClient;
2929

3030
// TODO: [CmbServiceTests] Adapt to run with the service
3131
protected override bool UseCMBService()
@@ -38,9 +38,7 @@ public InScenePlacedNetworkObjectTests(NetworkTopologyTypes networkTopologyType,
3838
protected override IEnumerator OnSetup()
3939
{
4040
NetworkObjectTestComponent.Reset();
41-
m_EnableVerboseDebug = true;
4241
NetworkObjectTestComponent.VerboseDebug = m_EnableVerboseDebug;
43-
m_CanStartServerAndClients = false;
4442
return base.OnSetup();
4543
}
4644

@@ -52,12 +50,14 @@ protected override IEnumerator OnSetup()
5250
/// <returns></returns>
5351
protected override IEnumerator OnTearDown()
5452
{
53+
m_LateJoinClient = null;
5554
yield return CleanUpLoadedScene();
5655
}
5756

58-
protected override bool CanStartServerAndClients()
57+
protected override void OnNewClientCreated(NetworkManager networkManager)
5958
{
60-
return m_CanStartServerAndClients;
59+
m_LateJoinClient = networkManager;
60+
base.OnNewClientCreated(networkManager);
6161
}
6262

6363
public enum DespawnMode
@@ -81,30 +81,11 @@ public IEnumerator InSceneNetworkObjectSynchAndSpawn([Values] DespawnMode despaw
8181
Assert.Ignore($"Test ignored as DeferDespawn is only valid with Distributed Authority mode.");
8282
}
8383

84-
NetworkObjectTestComponent.VerboseDebug = false;
85-
// Because despawning a client will cause it to shutdown and clean everything in the
86-
// scene hierarchy, we have to prevent one of the clients from spawning initially before
87-
// we test synchronizing late joining clients with despawned in-scene placed NetworkObjects.
88-
// So, we prevent the automatic starting of the server and clients, remove the client we
89-
// will be targeting to join late from the m_ClientNetworkManagers array, start the server
90-
// and the remaining client, despawn the in-scene NetworkObject, and then start and synchronize
91-
// the clientToTest.
92-
var clientToTest = m_ClientNetworkManagers[2];
93-
var clients = m_ClientNetworkManagers.ToList();
94-
clients.Remove(clientToTest);
95-
m_ClientNetworkManagers = clients.ToArray();
96-
m_CanStartServerAndClients = true;
97-
NetworkObjectTestComponent.Reset();
98-
yield return StartServerAndClients();
99-
clients.Add(clientToTest);
100-
m_ClientNetworkManagers = clients.ToArray();
101-
102-
10384
m_ServerNetworkManager.SceneManager.OnSceneEvent += Server_OnSceneEvent;
10485
var status = m_ServerNetworkManager.SceneManager.LoadScene(k_SceneToLoad, LoadSceneMode.Additive);
10586
Assert.IsTrue(status == SceneEventProgressStatus.Started, $"When attempting to load scene {k_SceneToLoad} was returned the following progress status: {status}");
106-
// We removed a client from the initial spawn and server should spawn too
107-
var clientCount = TotalClients - (m_UseHost ? 1 : 0);
87+
88+
var clientCount = NumberOfClients + 1;
10889

10990
// This verifies the scene loaded and the in-scene placed NetworkObjects spawned.
11091
yield return WaitForConditionOrTimeOut(() => NetworkObjectTestComponent.SpawnedInstances.Count == clientCount);
@@ -136,24 +117,24 @@ public IEnumerator InSceneNetworkObjectSynchAndSpawn([Values] DespawnMode despaw
136117

137118
// Now late join a client
138119
NetworkObjectTestComponent.OnInSceneObjectDespawned += OnInSceneObjectDespawned;
139-
NetcodeIntegrationTestHelpers.StartOneClient(clientToTest);
120+
yield return CreateAndStartNewClient();
140121
// Spawned another client
141122
clientCount++;
142123

143-
yield return WaitForConditionOrTimeOut(() => (clientToTest.IsConnectedClient && clientToTest.IsListening));
144-
AssertOnTimeout($"Timed out waiting for {clientToTest.name} to reconnect!");
124+
yield return WaitForConditionOrTimeOut(() => (m_LateJoinClient.IsConnectedClient && m_LateJoinClient.IsListening));
125+
AssertOnTimeout($"Timed out waiting for {m_LateJoinClient.name} to connect!");
145126

146127
yield return s_DefaultWaitForTick;
147128

148129
// Make sure the late-joining client's in-scene placed NetworkObject received the despawn notification during synchronization
149-
Assert.IsNotNull(m_JoinedClientDespawnedNetworkObject, $"{clientToTest.name} did not despawn the in-scene placed NetworkObject when connecting and synchronizing!");
130+
Assert.IsNotNull(m_JoinedClientDespawnedNetworkObject, $"{m_LateJoinClient.name} did not despawn the in-scene placed NetworkObject when connecting and synchronizing!");
150131

151132
// Update the newly joined client information
152133
ClientNetworkManagerPostStartInit();
153134

154135
// We should still have no spawned in-scene placed NetworkObjects at this point
155136
yield return WaitForConditionOrTimeOut(() => NetworkObjectTestComponent.SpawnedInstances.Count == 0);
156-
AssertOnTimeout($"{clientToTest.name} spawned in-scene placed NetworkObject!");
137+
AssertOnTimeout($"{m_LateJoinClient.name} spawned in-scene placed NetworkObject!");
157138

158139
// Now test that the despawned in-scene placed NetworkObject can be re-spawned (without having been registered as a NetworkPrefab)
159140
serverObject.Spawn();
@@ -185,22 +166,6 @@ public IEnumerator InSceneNetworkObjectSynchAndSpawn([Values] DespawnMode despaw
185166
[UnityTest]
186167
public IEnumerator ParentedInSceneObjectLateJoiningClient()
187168
{
188-
// Because despawning a client will cause it to shutdown and clean everything in the
189-
// scene hierarchy, we have to prevent one of the clients from spawning initially before
190-
// we test synchronizing late joining clients.
191-
// So, we prevent the automatic starting of the server and clients, remove the client we
192-
// will be targeting to join late from the m_ClientNetworkManagers array, start the server
193-
// and the remaining client, despawn the in-scene NetworkObject, and then start and synchronize
194-
// the clientToTest.
195-
var clientToTest = m_ClientNetworkManagers[2];
196-
var clients = m_ClientNetworkManagers.ToList();
197-
clients.Remove(clientToTest);
198-
m_ClientNetworkManagers = clients.ToArray();
199-
m_CanStartServerAndClients = true;
200-
yield return StartServerAndClients();
201-
clients.Add(clientToTest);
202-
m_ClientNetworkManagers = clients.ToArray();
203-
204169
NetworkObjectTestComponent.ServerNetworkObjectInstance = null;
205170

206171
m_ClientNetworkManagers[0].SceneManager.OnSceneEvent += OnSceneEvent;
@@ -232,9 +197,9 @@ public IEnumerator ParentedInSceneObjectLateJoiningClient()
232197
yield return WaitForConditionOrTimeOut(() => firstClientInSceneObjectInstance.transform.parent != null && firstClientInSceneObjectInstance.transform.parent == clientSidePlayer.transform);
233198
AssertOnTimeout($"Timed out waiting for the client-side id ({m_ClientNetworkManagers[0].LocalClientId}) server player transform to be set on the client-side in-scene object!");
234199
// Now late join a client
235-
NetcodeIntegrationTestHelpers.StartOneClient(clientToTest);
236-
yield return WaitForConditionOrTimeOut(() => (clientToTest.IsConnectedClient && clientToTest.IsListening));
237-
AssertOnTimeout($"Timed out waiting for {clientToTest.name} to reconnect!");
200+
yield return CreateAndStartNewClient();
201+
yield return WaitForConditionOrTimeOut(() => (m_LateJoinClient.IsConnectedClient && m_LateJoinClient.IsListening));
202+
AssertOnTimeout($"Timed out waiting for {m_LateJoinClient.name} to reconnect!");
238203

239204
yield return s_DefaultWaitForTick;
240205

@@ -245,7 +210,7 @@ public IEnumerator ParentedInSceneObjectLateJoiningClient()
245210
Assert.IsNotNull(lateJoinClientInSceneObjectInstance, $"Could not get the client-side registration of {nameof(NetworkObjectTestComponent)} for the late joining client!");
246211

247212
// Now get the late-joining client's instance for the server player
248-
clientSidePlayer = m_PlayerNetworkObjects[clientToTest.LocalClientId][clientSidePlayer.OwnerClientId];
213+
clientSidePlayer = m_PlayerNetworkObjects[m_LateJoinClient.LocalClientId][clientSidePlayer.OwnerClientId];
249214

250215
// Validate the late joined client's in-scene NetworkObject is parented to the server-side player
251216
yield return WaitForConditionOrTimeOut(() => lateJoinClientInSceneObjectInstance.transform.parent != null && lateJoinClientInSceneObjectInstance.transform.parent == clientSidePlayer.transform);
@@ -360,11 +325,8 @@ public IEnumerator EnableDisableInSceneObjectTests()
360325
// Enabled disabling the NetworkObject when it is despawned
361326
NetworkObjectTestComponent.DisableOnDespawn = true;
362327
// Set the number of instances to expect
363-
m_NumberOfInstancesCheck = NumberOfClients + (m_UseHost ? 1 : 0);
328+
m_NumberOfInstancesCheck = TotalClients;
364329

365-
// Start the host and clients and load the in-scene object scene additively
366-
m_CanStartServerAndClients = true;
367-
yield return StartServerAndClients();
368330
m_ServerNetworkManager.SceneManager.OnLoadEventCompleted += SceneManager_OnLoadEventCompleted;
369331
m_ServerNetworkManager.SceneManager.LoadScene(k_SceneToLoad, LoadSceneMode.Additive);
370332
yield return WaitForConditionOrTimeOut(() => m_AllClientsLoadedScene);
@@ -499,28 +461,12 @@ public IEnumerator ParentedInSceneObjectUnderGameObject([Values(k_InSceneUnder,
499461
var useNetworkTransform = m_SceneLoading == k_InSceneUnderWithNT;
500462

501463
m_SceneLoading = inSceneUnderToLoad;
502-
// Because despawning a client will cause it to shutdown and clean everything in the
503-
// scene hierarchy, we have to prevent one of the clients from spawning initially before
504-
// we test synchronizing late joining clients.
505-
// So, we prevent the automatic starting of the server and clients, remove the client we
506-
// will be targeting to join late from the m_ClientNetworkManagers array, start the server
507-
// and the remaining client, despawn the in-scene NetworkObject, and then start and synchronize
508-
// the clientToTest.
509-
var clientToTest = m_ClientNetworkManagers[1];
510-
var clients = m_ClientNetworkManagers.ToList();
511464

512465
// Note: This test is a modified copy of ParentedInSceneObjectLateJoiningClient.
513466
// The 1st client is being ignored in this test and the focus is primarily on the late joining
514467
// 2nd client after adjustments have been made to the child NetworkBehaviour and if applicable
515468
// NetworkTransform.
516469

517-
clients.Remove(clientToTest);
518-
m_ClientNetworkManagers = clients.ToArray();
519-
m_CanStartServerAndClients = true;
520-
yield return StartServerAndClients();
521-
clients.Add(clientToTest);
522-
m_ClientNetworkManagers = clients.ToArray();
523-
524470
NetworkObjectTestComponent.ServerNetworkObjectInstance = null;
525471

526472
m_ClientNetworkManagers[0].SceneManager.OnSceneEvent += OnSceneEvent;
@@ -545,9 +491,9 @@ public IEnumerator ParentedInSceneObjectUnderGameObject([Values(k_InSceneUnder,
545491
}
546492

547493
// Now late join a client
548-
NetcodeIntegrationTestHelpers.StartOneClient(clientToTest);
549-
yield return WaitForConditionOrTimeOut(() => (clientToTest.IsConnectedClient && clientToTest.IsListening));
550-
AssertOnTimeout($"Timed out waiting for {clientToTest.name} to reconnect!");
494+
yield return CreateAndStartNewClient();
495+
yield return WaitForConditionOrTimeOut(() => (m_LateJoinClient.IsConnectedClient && m_LateJoinClient.IsListening));
496+
AssertOnTimeout($"Timed out waiting for {m_LateJoinClient.name} to reconnect!");
551497

552498
yield return s_DefaultWaitForTick;
553499

testproject/Assets/Tests/Runtime/NetworkTransform/NestedNetworkTransformTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,6 @@ public void NestedNetworkTransformSynchronization()
360360
{
361361
var timeStarted = Time.realtimeSinceStartup;
362362
var startFrameCount = Time.frameCount;
363-
m_EnableVerboseDebug = true;
364363
AutomatedPlayerMover.StopMovement = false;
365364
ChildMoverManager.StopMovement = false;
366365
m_ValidationErrors = new StringBuilder();
@@ -399,7 +398,6 @@ public void NestedNetworkTransformSynchronization()
399398
// If we have 5 precision failures in a row and fail to correct, then fail this test
400399
if (precisionFailures > k_MaximumPrecisionFailures)
401400
{
402-
m_EnableVerboseDebug = true;
403401
DisplayFrameAndTimeInfo(timeStarted, startFrameCount, false);
404402
Assert.True(success, $"[{m_Interpolation}][{m_Precision}][{m_Authority}][Iteration: {i}]\n[Precision Failure] Exceeded Precision Failure Count " +
405403
$"({precisionFailures})\n Timed out waiting for all nested NetworkTransform cloned instances to match!\n{m_ValidationErrors}");
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2-
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=CMB/@EntryIndexedValue">CMB</s:String></wpf:ResourceDictionary>
2+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=CMB/@EntryIndexedValue">CMB</s:String>
3+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=DDOL/@EntryIndexedValue">DDOL</s:String>
4+
<s:Boolean x:Key="/Default/UserDictionary/Words/=ddol/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

0 commit comments

Comments
 (0)