Skip to content

Commit e92af7c

Browse files
test
up-port test of #3029 while also taking into consideration the two network topologies.
1 parent 8ec8e95 commit e92af7c

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

com.unity.netcode.gameobjects/Tests/Runtime/NetworkVisibilityTests.cs

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ namespace Unity.Netcode.RuntimeTests
1414
internal class NetworkVisibilityTests : NetcodeIntegrationTest
1515
{
1616

17-
protected override int NumberOfClients => 1;
17+
protected override int NumberOfClients => 2;
1818
private GameObject m_TestNetworkPrefab;
1919
private bool m_SceneManagementEnabled;
20+
private GameObject m_SpawnedObject;
21+
private NetworkManager m_SessionOwner;
2022

2123
public NetworkVisibilityTests(SceneManagementState sceneManagementState, NetworkTopologyTypes networkTopologyType) : base(networkTopologyType)
2224
{
@@ -27,7 +29,11 @@ protected override void OnServerAndClientsCreated()
2729
{
2830
m_TestNetworkPrefab = CreateNetworkObjectPrefab("Object");
2931
m_TestNetworkPrefab.AddComponent<NetworkVisibilityComponent>();
30-
m_ServerNetworkManager.NetworkConfig.EnableSceneManagement = m_SceneManagementEnabled;
32+
if (!UseCMBService())
33+
{
34+
m_ServerNetworkManager.NetworkConfig.EnableSceneManagement = m_SceneManagementEnabled;
35+
}
36+
3137
foreach (var clientNetworkManager in m_ClientNetworkManagers)
3238
{
3339
clientNetworkManager.NetworkConfig.EnableSceneManagement = m_SceneManagementEnabled;
@@ -38,21 +44,58 @@ protected override void OnServerAndClientsCreated()
3844

3945
protected override IEnumerator OnServerAndClientsConnected()
4046
{
41-
SpawnObject(m_TestNetworkPrefab, m_ServerNetworkManager);
47+
m_SessionOwner = UseCMBService() ? m_ClientNetworkManagers[0] : m_ServerNetworkManager;
48+
m_SpawnedObject = SpawnObject(m_TestNetworkPrefab, m_SessionOwner);
4249

4350
yield return base.OnServerAndClientsConnected();
4451
}
4552

4653
[UnityTest]
4754
public IEnumerator HiddenObjectsTest()
4855
{
56+
var expectedCount = UseCMBService() ? 2 : 3;
4957
#if UNITY_2023_1_OR_NEWER
50-
yield return WaitForConditionOrTimeOut(() => Object.FindObjectsByType<NetworkVisibilityComponent>(FindObjectsSortMode.None).Where((c) => c.IsSpawned).Count() == 2);
58+
yield return WaitForConditionOrTimeOut(() => Object.FindObjectsByType<NetworkVisibilityComponent>(FindObjectsSortMode.None).Where((c) => c.IsSpawned).Count() == expectedCount);
5159
#else
52-
yield return WaitForConditionOrTimeOut(() => Object.FindObjectsOfType<NetworkVisibilityComponent>().Where((c) => c.IsSpawned).Count() == 2);
60+
yield return WaitForConditionOrTimeOut(() => Object.FindObjectsOfType<NetworkVisibilityComponent>().Where((c) => c.IsSpawned).Count() == expectedCount);
5361
#endif
5462

5563
Assert.IsFalse(s_GlobalTimeoutHelper.TimedOut, "Timed out waiting for the visible object count to equal 2!");
5664
}
65+
66+
[UnityTest]
67+
public IEnumerator HideShowAndDeleteTest()
68+
{
69+
var expectedCount = UseCMBService() ? 2 : 3;
70+
#if UNITY_2023_1_OR_NEWER
71+
yield return WaitForConditionOrTimeOut(() => Object.FindObjectsByType<NetworkVisibilityComponent>(FindObjectsSortMode.None).Where((c) => c.IsSpawned).Count() == expectedCount);
72+
#else
73+
yield return WaitForConditionOrTimeOut(() => Object.FindObjectsOfType<NetworkVisibilityComponent>().Where((c) => c.IsSpawned).Count() == expectedCount);
74+
#endif
75+
AssertOnTimeout("Timed out waiting for the visible object count to equal 2!");
76+
77+
var sessionOwnerNetworkObject = m_SpawnedObject.GetComponent<NetworkObject>();
78+
var clientIndex = UseCMBService() ? 1 : 0;
79+
sessionOwnerNetworkObject.NetworkHide(m_ClientNetworkManagers[clientIndex].LocalClientId);
80+
#if UNITY_2023_1_OR_NEWER
81+
yield return WaitForConditionOrTimeOut(() => Object.FindObjectsByType<NetworkVisibilityComponent>(FindObjectsSortMode.None).Where((c) => c.IsSpawned).Count() == expectedCount - 1);
82+
#else
83+
yield return WaitForConditionOrTimeOut(() => Object.FindObjectsOfType<NetworkVisibilityComponent>().Where((c) => c.IsSpawned).Count() == expectedCount - 1);
84+
#endif
85+
AssertOnTimeout($"Timed out waiting for {m_SpawnedObject.name} to be hidden from client!");
86+
var networkObjectId = sessionOwnerNetworkObject.NetworkObjectId;
87+
sessionOwnerNetworkObject.NetworkShow(m_ClientNetworkManagers[clientIndex].LocalClientId);
88+
sessionOwnerNetworkObject.Despawn(true);
89+
90+
// Expect no exceptions
91+
yield return s_DefaultWaitForTick;
92+
93+
// Now force a scenario where it normally would have caused an exception
94+
m_SessionOwner.SpawnManager.ObjectsToShowToClient.Add(m_ClientNetworkManagers[clientIndex].LocalClientId, new System.Collections.Generic.List<NetworkObject>());
95+
m_SessionOwner.SpawnManager.ObjectsToShowToClient[m_ClientNetworkManagers[clientIndex].LocalClientId].Add(null);
96+
97+
// Expect no exceptions
98+
yield return s_DefaultWaitForTick;
99+
}
57100
}
58101
}

0 commit comments

Comments
 (0)