Skip to content

Commit 584541e

Browse files
test - fix
Fixed the issue in the `TestDictionaryCollections` test where clients that did not have the initial added target changes for server changes when running a host. Now, upon clients spawning players locally on the clients, the server write dictionary that is already populated by the host will have the added target changes injected during spawn to assure the changes match. (This is only for this specific test when running the host `TestFixture` pass.
1 parent d6fac12 commit 584541e

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

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

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -859,29 +859,8 @@ public IEnumerator TestDictionaryCollections()
859859
compDictionary.ListCollectionServer.Value.Add(newEntry.Item1, newEntry.Item2);
860860
// Checking if dirty on client side should revert back to original known current dictionary state
861861
compDictionary.ListCollectionServer.IsDirty();
862-
if (!m_ServerNetworkManager.IsHost)
863-
{
864-
yield return WaitForConditionOrTimeOut(() => compDictionary.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
865-
AssertOnTimeout($"Client-{client.LocalClientId} add to server write collection property failed to restore on {className} {compDictionary.name}! {compDictionary.GetLog()}");
866-
}
867-
else // Host, for some reason, does not have changes tracked but the dictionaries match... ????
868-
{
869-
// TODO: Need to track down why host is the only failing test.
870-
// NOTES: It seems only the host doesn't track changes made on the owner write permissions (i.e. issue with test itself?),
871-
// but when comparing the values of the dictionaries everything passes (i.e. dictionaries are synchronized)
872-
var compDictionaryTest = (DictionaryTestHelper)null;
873-
var compDictionaryServerTest = (DictionaryTestHelper)null;
874-
var classNameTest = $"{nameof(DictionaryTestHelper)}";
875-
foreach (var clientTest in m_Clients)
876-
{
877-
///////////////////////////////////////////////////////////////////////////
878-
// Dictionary<int, Dictionary<int,SerializableObject>> nested dictionaries
879-
compDictionaryTest = clientTest.LocalClient.PlayerObject.GetComponent<DictionaryTestHelper>();
880-
compDictionaryServerTest = m_PlayerNetworkObjects[NetworkManager.ServerClientId][clientTest.LocalClientId].GetComponent<DictionaryTestHelper>();
881-
Assert.True(compDictionaryTest.ValidateInstances(), $"[Owner] Not all instances of client-{compDictionaryTest.OwnerClientId}'s {classNameTest} {compDictionaryTest.name} component match! {compDictionaryTest.GetLog()}");
882-
Assert.True(compDictionaryServerTest.ValidateInstances(), $"[Server] Not all instances of client-{compDictionaryServerTest.OwnerClientId}'s {classNameTest} {compDictionaryServerTest.name} component match! {compDictionaryServerTest.GetLog()}");
883-
}
884-
}
862+
yield return WaitForConditionOrTimeOut(() => compDictionary.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
863+
AssertOnTimeout($"Client-{client.LocalClientId} add to server write collection property failed to restore on {className} {compDictionary.name}! {compDictionary.GetLog()}");
885864

886865
// Client-side add the same key and SerializableObject to server write permission property (would throw key exists exception too if previous failed)
887866
compDictionary.ListCollectionServer.Value.Add(newEntry.Item1, newEntry.Item2);
@@ -1865,7 +1844,7 @@ private bool ChangesMatch(ulong clientId, Dictionary<DeltaTypes, Dictionary<int,
18651844
var deltaTypes = Enum.GetValues(typeof(DeltaTypes)).OfType<DeltaTypes>().ToList();
18661845
foreach (var deltaType in deltaTypes)
18671846
{
1868-
LogMessage($"Comparing {deltaType}:");
1847+
LogMessage($"[Comparing {deltaType}] Local: {local[deltaType].Count} | Other: {other[deltaType].Count}");
18691848
if (local[deltaType].Count != other[deltaType].Count)
18701849
{
18711850
LogMessage($"[Client-{clientId}] Local {deltaType}s count of {local[deltaType].Count} did not match the other's count of {other[deltaType].Count}!");
@@ -1994,6 +1973,18 @@ public void TrackChanges(Targets target, Dictionary<int, SerializableObject> pre
19941973
contextTable[DeltaTypes.Removed] = whatWasRemoved;
19951974
contextTable[DeltaTypes.Changed] = whatChanged;
19961975
contextTable[DeltaTypes.UnChanged] = whatRemainedTheSame;
1976+
1977+
// Log all incoming changes when debug mode is enabled
1978+
if (!IsOwner && IsDebugMode)
1979+
{
1980+
LogMessage($"[{NetworkManager.name}][TrackChanges-> Client-{OwnerClientId}] Collection was updated!");
1981+
LogMessage($"Added: {whatWasAdded.Count} ");
1982+
LogMessage($"Removed: {whatWasRemoved.Count} ");
1983+
LogMessage($"Changed: {whatChanged.Count} ");
1984+
LogMessage($"UnChanged: {whatRemainedTheSame.Count} ");
1985+
UnityEngine.Debug.Log($"{GetLog()}");
1986+
LogStart();
1987+
}
19971988
}
19981989

19991990
public void OnServerListValuesChanged(Dictionary<int, SerializableObject> previous, Dictionary<int, SerializableObject> current)
@@ -2058,13 +2049,24 @@ public void InitValues()
20582049
if (IsServer)
20592050
{
20602051
ListCollectionServer.Value = OnSetServerValues();
2061-
//ListCollectionOwner.CheckDirtyState();
2052+
ListCollectionServer.CheckDirtyState();
20622053
}
20632054

20642055
if (IsOwner)
20652056
{
20662057
ListCollectionOwner.Value = OnSetOwnerValues();
2067-
//ListCollectionOwner.CheckDirtyState();
2058+
ListCollectionOwner.CheckDirtyState();
2059+
}
2060+
2061+
// When running a host, the changes being tracked will not match because clients will be synchronized with changes
2062+
// already applied. This fixing this issue by injecting "added" server targeted changes during initialization on
2063+
// the connected clients' side.
2064+
if (!IsServer)
2065+
{
2066+
if (ListCollectionServer.Value.Count > 0 && NetworkVariableChanges[Targets.Server][DeltaTypes.Added].Count == 0)
2067+
{
2068+
TrackChanges(Targets.Server, new Dictionary<int, SerializableObject>(), ListCollectionServer.Value);
2069+
}
20682070
}
20692071
}
20702072

0 commit comments

Comments
 (0)