Skip to content

Commit 13942e7

Browse files
test-fix
OwnerModifiedTests was not properly spawning from the non-session owner client (it was spawning with ownership on the session owner side). NetworkVariableCollectionTests is showing an issue with changes for only the host instance and only within the TestDictionaryCollections (has to do with trying to add and then reverting vs the tracked changes).
1 parent b70e5b1 commit 13942e7

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariable/NetworkVariableCollectionsTests.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,12 @@ public IEnumerator TestDictionaryCollections()
781781
}
782782

783783
m_CurrentKey = 1000;
784+
// Temporarily enabling debug mode on host only.
785+
// TODO: Need to track down why host is the only failing test.
786+
// NOTES: It seems the tracked changes get adjusted for only a host which would have the player object.
787+
// This could be due to when the player is spawned on the host relative to the other clients.
788+
m_EnableDebug = m_ServerNetworkManager.IsHost;
789+
m_EnableVerboseDebug = m_ServerNetworkManager.IsHost;
784790
if (m_EnableDebug)
785791
{
786792
VerboseDebug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>> Init Values <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
@@ -831,7 +837,7 @@ public IEnumerator TestDictionaryCollections()
831837
{
832838
// Server-side add same key and SerializableObject prior to being added to the owner side
833839
compDictionaryServer.ListCollectionOwner.Value.Add(newEntry.Item1, newEntry.Item2);
834-
// Checking if dirty on server side should revert back to origina known current dictionary state
840+
// Checking if dirty on server side should revert back to original known current dictionary state
835841
compDictionaryServer.ListCollectionOwner.IsDirty();
836842
yield return WaitForConditionOrTimeOut(() => compDictionaryServer.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
837843
AssertOnTimeout($"Server add to owner write collection property failed to restore on {className} {compDictionaryServer.name}! {compDictionaryServer.GetLog()}");
@@ -840,7 +846,6 @@ public IEnumerator TestDictionaryCollections()
840846
// Server-side add a completely new key and SerializableObject to to owner write permission property
841847
compDictionaryServer.ListCollectionOwner.Value.Add(GetNextKey(), SerializableObject.GetRandomObject());
842848
// Both should be overridden by the owner-side update
843-
844849
}
845850
VerboseDebug($"[{compDictionary.name}][Owner] Adding Key: {newEntry.Item1}");
846851
// Add key and SerializableObject to owner side
@@ -857,7 +862,7 @@ public IEnumerator TestDictionaryCollections()
857862
{
858863
// Client-side add same key and SerializableObject to server write permission property
859864
compDictionary.ListCollectionServer.Value.Add(newEntry.Item1, newEntry.Item2);
860-
// Checking if dirty on client side should revert back to origina known current dictionary state
865+
// Checking if dirty on client side should revert back to original known current dictionary state
861866
compDictionary.ListCollectionServer.IsDirty();
862867
yield return WaitForConditionOrTimeOut(() => compDictionary.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
863868
AssertOnTimeout($"Client-{client.LocalClientId} add to server write collection property failed to restore on {className} {compDictionary.name}! {compDictionary.GetLog()}");
@@ -892,7 +897,6 @@ public IEnumerator TestDictionaryCollections()
892897

893898
yield return WaitForConditionOrTimeOut(() => compDictionaryServer.ValidateInstances());
894899
AssertOnTimeout($"[Server] Not all instances of client-{compDictionaryServer.OwnerClientId}'s {className} {compDictionaryServer.name} component match! {compDictionaryServer.GetLog()}");
895-
896900
ValidateClientsFlat(client);
897901
////////////////////////////////////
898902
// Owner Change SerializableObject Entry
@@ -915,7 +919,7 @@ public IEnumerator TestDictionaryCollections()
915919
{
916920
// Server-side update same key value prior to being updated to the owner side
917921
compDictionaryServer.ListCollectionOwner.Value[valueInt] = randomObject;
918-
// Checking if dirty on server side should revert back to origina known current dictionary state
922+
// Checking if dirty on server side should revert back to original known current dictionary state
919923
compDictionaryServer.ListCollectionOwner.IsDirty();
920924
yield return WaitForConditionOrTimeOut(() => compDictionaryServer.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
921925
AssertOnTimeout($"Server update collection entry value to local owner write collection property failed to restore on {className} {compDictionaryServer.name}! {compDictionaryServer.GetLog()}");
@@ -956,7 +960,7 @@ public IEnumerator TestDictionaryCollections()
956960
{
957961
// Owner-side update same key value prior to being updated to the server side
958962
compDictionary.ListCollectionServer.Value[valueInt] = randomObject;
959-
// Checking if dirty on owner side should revert back to origina known current dictionary state
963+
// Checking if dirty on owner side should revert back to original known current dictionary state
960964
compDictionary.ListCollectionServer.IsDirty();
961965
yield return WaitForConditionOrTimeOut(() => compDictionary.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
962966
AssertOnTimeout($"Client-{client.LocalClientId} update collection entry value to local server write collection property failed to restore on {className} {compDictionary.name}! {compDictionary.GetLog()}");
@@ -1014,6 +1018,9 @@ public IEnumerator TestDictionaryCollections()
10141018
m_Stage = 0;
10151019
}
10161020
}
1021+
1022+
m_EnableDebug = false;
1023+
m_EnableVerboseDebug = false;
10171024
}
10181025

10191026
[UnityTest]
@@ -1844,7 +1851,7 @@ private bool ChangesMatch(ulong clientId, Dictionary<DeltaTypes, Dictionary<int,
18441851
LogMessage($"Comparing {deltaType}:");
18451852
if (local[deltaType].Count != other[deltaType].Count)
18461853
{
1847-
LogMessage($"{deltaType}s count did not match!");
1854+
LogMessage($"[Client-{clientId}] {deltaType}s count {other[deltaType].Count} did not match the local count {local[deltaType].Count}!");
18481855
return false;
18491856
}
18501857
if (!CompareDictionaries(clientId, local[deltaType], other[deltaType]))

com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariable/OwnerModifiedTests.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,20 +154,26 @@ public IEnumerator VerifyDoesNotRepeatOnSomeClients()
154154
Assert.True(OwnerModifiedObject.Updates > 20);
155155
}
156156

157-
158-
private ChangeValueOnAuthority m_SessionAuthorityInstance;
159157
private ChangeValueOnAuthority m_InstanceAuthority;
160158

161159
private bool NetworkVariablesMatch(StringBuilder errorLog)
162160
{
163161
foreach (var networkManager in m_NetworkManagers)
164162
{
163+
var changeValue = networkManager.SpawnManager.SpawnedObjects[m_InstanceAuthority.NetworkObjectId].GetComponent<ChangeValueOnAuthority>();
165164
if (networkManager == m_InstanceAuthority.NetworkManager)
166165
{
166+
if (m_InstanceAuthority.SomeIntValue.Value != 2)
167+
{
168+
errorLog.AppendLine($"[Client-{networkManager.LocalClientId}] {changeValue.name} value is {changeValue.SomeIntValue.Value} but was expecting 2!");
169+
}
167170
continue;
168171
}
172+
if (changeValue.SomeIntValue.Value != 2)
173+
{
174+
errorLog.AppendLine($"[Client-{networkManager.LocalClientId}] {changeValue.name} value is {changeValue.SomeIntValue.Value} but was expecting 2!");
175+
}
169176

170-
var changeValue = networkManager.SpawnManager.SpawnedObjects[m_InstanceAuthority.NetworkObjectId].GetComponent<ChangeValueOnAuthority>();
171177
if (changeValue.SomeIntValue.Value != m_InstanceAuthority.SomeIntValue.Value)
172178
{
173179
errorLog.AppendLine($"[Client-{networkManager.LocalClientId}] {changeValue.name} value is {changeValue.SomeIntValue.Value} but was expecting {m_InstanceAuthority.SomeIntValue.Value}!");
@@ -185,19 +191,15 @@ public IEnumerator OwnershipSpawnedAndUpdatedDuringSpawn()
185191
{
186192
var authority = GetAuthorityNetworkManager();
187193
var nonAuthority = GetNonAuthorityNetworkManager();
188-
m_SessionAuthorityInstance = Object.Instantiate(m_SpawnObject).GetComponent<ChangeValueOnAuthority>();
189-
190-
SpawnInstanceWithOwnership(m_SessionAuthorityInstance.GetComponent<NetworkObject>(), authority, nonAuthority.LocalClientId);
191-
yield return WaitForSpawnedOnAllOrTimeOut(m_SessionAuthorityInstance.NetworkObjectId);
192-
AssertOnTimeout($"Failed to spawn {m_SessionAuthorityInstance.name} on all clients!");
194+
// If running in distributed authority mode, we use the nonauthority (i.e. not SessionOwner) instance to spawn.
195+
var spawnAuthority = m_DistributedAuthority ? nonAuthority : authority;
196+
m_InstanceAuthority = SpawnObject(m_SpawnObject, spawnAuthority).GetComponent<ChangeValueOnAuthority>();
193197

194-
m_InstanceAuthority = nonAuthority.SpawnManager.SpawnedObjects[m_SessionAuthorityInstance.NetworkObjectId].GetComponent<ChangeValueOnAuthority>();
198+
yield return WaitForSpawnedOnAllOrTimeOut(m_InstanceAuthority.NetworkObjectId);
199+
AssertOnTimeout($"Failed to spawn {m_InstanceAuthority.name} on all clients!");
195200

196201
yield return WaitForConditionOrTimeOut(NetworkVariablesMatch);
197202
AssertOnTimeout($"The {nameof(ChangeValueOnAuthority.SomeIntValue)} failed to synchronize on all clients!");
198-
199-
Assert.IsTrue(m_SessionAuthorityInstance.SomeIntValue.Value == 2, "No values were updated on the spawn authority instance!");
200-
Assert.IsTrue(m_InstanceAuthority.SomeIntValue.Value == 2, "No values were updated on the owner's instance!");
201203
}
202204
}
203205
}

0 commit comments

Comments
 (0)