Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
- Fixed NetworkList to properly call INetworkSerializable's NetworkSerialize() method (#1682)
- Fixed The NetworkConfig's checksum hash includes the NetworkTick so that clients with a different tickrate than the server are identified and not allowed to connect (#1728)
- Fixed OwnedObjects not being properly modified when using ChangeOwnership (#1731)
- Fixed throwing an exception in OnNetworkUpdate causing other OnNetworkUpdate calls to not be executed. (#1739)
- Fixed throwing an exception in OnNetworkSpawn/OnNetworkDespawn causing other OnNetworkSpawn/OnNetworkDespawn calls to not be executed. (#1739)

## [1.0.0-pre.5] - 2022-01-26

Expand Down
4 changes: 2 additions & 2 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ internal void InvokeBehaviourNetworkSpawn()
}
catch (Exception e)
{
Console.WriteLine(e);
NetworkLog.LogError(e.ToString());
}
}
}
Expand All @@ -818,7 +818,7 @@ internal void InvokeBehaviourNetworkDespawn()
}
catch (Exception e)
{
Console.WriteLine(e);
NetworkLog.LogError(e.ToString());
Comment thread
ShadauxCat marked this conversation as resolved.
Outdated
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class OnNetworkSpawnExceptionTests : BaseMultiInstanceTest
[UnityTest]
public IEnumerator WhenOnNetworkSpawnThrowsException_FutureOnNetworkSpawnsAreNotPrevented()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests feel pretty rough in their current state.

  • There are lots of magic numbers. There are even magic numbers that depend on other magic numbers (2, 3, and 5)
  • You have 1 setup for 2 different tests. Is all the code in the setup necessary for both?
  • You have log asserts in your test setup. Are these asserts actual tests, or are they an attempt to verify that setup has done what it's expected to?
  • You are using the multi-instance tests and have a client but it's unclear if this is actually necessary for this test (don't these virtual methods get invoked on the server too? Can't you write a much simpler/more contained UnityTest that doesn't involve a client or waiting for frames with timeout asserts and if (IsClient)?)
  • It's unclear (to me) why you have if (NumClientSpawns > 2) guard in the test components.

While it's a very contentious topic, generally speaking, the more self-contained a test is, the better for understanding and lower the risk for future false positives. I'm not personally a fan of having to read 3 classes and 6 methods to understand what's actually happening in this 1 line test case.

Copy link
Copy Markdown
Member

@NoelStephensUnity NoelStephensUnity May 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While throwing an exception in a component added to the player prefab is valid, I think this test should add more NetworkBehaviour derived components because the real test is to validate that all NetworkBehaviour components on a GameObject get OnNetworkSpawn and OnNetworkDespawn invoked even if one of the components throws and exception.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I've addressed everything you mentioned here, except for the use of multi-instance tests. I think it's valuable to test that the behavior is the same on both server and client. I did, however, update the test to make sure it isn't exclusively a client-side test, since OnNetworkSpawn and OnNetworkDespawn are called on the server too.

{
LogAssert.ignoreFailingMessages = true;
Comment thread
ShadauxCat marked this conversation as resolved.
Outdated
//Spawning was done during setup
Assert.AreEqual(5, OnNetworkSpawnThrowsExceptionComponent.NumClientSpawns);
yield return null;
Expand All @@ -56,6 +57,7 @@ public IEnumerator WhenOnNetworkSpawnThrowsException_FutureOnNetworkSpawnsAreNot
[UnityTest]
public IEnumerator WhenOnNetworkDespawnThrowsException_FutureOnNetworkDespawnsAreNotPrevented()
{
LogAssert.ignoreFailingMessages = true;
//Spawning was done during setup. Now we despawn.
for (var i = 0; i < 5; ++i)
{
Expand All @@ -71,11 +73,15 @@ public IEnumerator WhenOnNetworkDespawnThrowsException_FutureOnNetworkDespawnsAr

public override IEnumerator Setup()
{
yield return StartSomeClientsAndServerWithPlayers(true, NbClients, _ =>
OnNetworkSpawnThrowsExceptionComponent.NumClientSpawns = 0;
OnNetworkDespawnThrowsExceptionComponent.NumClientDespawns = 0;
LogAssert.ignoreFailingMessages = true;
yield return StartSomeClientsAndServerWithPlayers(false, NbClients, _ =>
{
m_Prefab = new GameObject();
var networkObject = m_Prefab.AddComponent<NetworkObject>();
m_Prefab.AddComponent<OnNetworkSpawnThrowsExceptionComponent>();
m_Prefab.AddComponent<OnNetworkDespawnThrowsExceptionComponent>();
MultiInstanceHelpers.MakeNetworkObjectTestPrefab(networkObject);

var validNetworkPrefab = new NetworkPrefab();
Expand All @@ -96,7 +102,7 @@ public override IEnumerator Setup()
}

var result = new MultiInstanceHelpers.CoroutineResultWrapper<bool>();
MultiInstanceHelpers.Run(
yield return MultiInstanceHelpers.Run(
MultiInstanceHelpers.WaitForCondition(
() => OnNetworkSpawnThrowsExceptionComponent.NumClientSpawns == 5, result));
Assert.IsTrue(result.Result);
Expand Down