Skip to content

Commit 4e5dfcc

Browse files
test
Adding a test to validate this specific issue is fixed.
1 parent 593c2f7 commit 4e5dfcc

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System.Collections;
2+
using NUnit.Framework;
3+
using Unity.Netcode.TestHelpers.Runtime;
4+
using UnityEngine;
5+
using UnityEngine.TestTools;
6+
7+
namespace Unity.Netcode.RuntimeTests
8+
{
9+
[TestFixture(NetworkTopologyTypes.DistributedAuthority, true)]
10+
[TestFixture(NetworkTopologyTypes.ClientServer, true)]
11+
[TestFixture(NetworkTopologyTypes.DistributedAuthority, false)]
12+
[TestFixture(NetworkTopologyTypes.ClientServer, false)]
13+
internal class RpcDuringOnNetworkSpawn : NetcodeIntegrationTest
14+
{
15+
protected override int NumberOfClients => 2;
16+
17+
private bool m_EnableSceneManagement;
18+
19+
public RpcDuringOnNetworkSpawn(NetworkTopologyTypes topologyType, bool enableSceneManagement) : base(topologyType)
20+
{
21+
m_EnableSceneManagement = enableSceneManagement;
22+
}
23+
24+
/// <summary>
25+
/// Enables or disables scene management when the <see cref="NetworkManager"/>s
26+
/// are created.
27+
/// </summary>
28+
protected override void OnServerAndClientsCreated()
29+
{
30+
foreach (var networkManager in m_NetworkManagers)
31+
{
32+
networkManager.NetworkConfig.EnableSceneManagement = m_EnableSceneManagement;
33+
}
34+
base.OnServerAndClientsCreated();
35+
}
36+
37+
protected override void OnPlayerPrefabGameObjectCreated()
38+
{
39+
var first = m_PlayerPrefab.AddComponent<FirstNetworkBehaviour>();
40+
var second = m_PlayerPrefab.AddComponent<SecondNetworkBehaviour>();
41+
first.SecondNetworkBehaviour = second;
42+
base.OnPlayerPrefabGameObjectCreated();
43+
}
44+
45+
/// <summary>
46+
/// Validates that invoking an RPC on another NetworkBehvaiour from within
47+
/// OnNetworkSpawn when scene management is disabled does not throw an exception
48+
/// </summary>
49+
[UnityTest]
50+
public IEnumerator ValidateRPCInvocationDuringNetworkSpawn()
51+
{
52+
// Just validating the values sent, but in reality if this breaks then an exception would be thrown prior to reaching this point.
53+
foreach (var networkManager in m_NetworkManagers)
54+
{
55+
var first = networkManager.LocalClient.PlayerObject.GetComponent<FirstNetworkBehaviour>();
56+
var second = networkManager.LocalClient.PlayerObject.GetComponent<SecondNetworkBehaviour>();
57+
58+
Assert.IsTrue(first.ValueSent == second.ValueReceived, $"[{networkManager.LocalClient.PlayerObject.name}] Value sent {first.ValueSent} does not equal the value received {second.ValueReceived}!");
59+
}
60+
61+
yield return null;
62+
}
63+
64+
65+
#region Test Components
66+
/// <summary>
67+
/// Should be added before the <see cref="SecondNetworkBehaviour"/>.
68+
/// This invokes the RPC on the <see cref="SecondNetworkBehaviour"/>
69+
/// during <see cref="NetworkBehaviour.OnNetworkSpawn"/>.
70+
/// </summary>
71+
public class FirstNetworkBehaviour : NetworkBehaviour
72+
{
73+
public SecondNetworkBehaviour SecondNetworkBehaviour;
74+
75+
public int ValueSent { get; private set; }
76+
public override void OnNetworkSpawn()
77+
{
78+
// Just invoke on the local player to test for this issue
79+
if (IsLocalPlayer)
80+
{
81+
ValueSent = Random.Range(0, 100);
82+
SecondNetworkBehaviour.SomeRpc(ValueSent);
83+
}
84+
base.OnNetworkSpawn();
85+
}
86+
}
87+
88+
public class SecondNetworkBehaviour : NetworkBehaviour
89+
{
90+
public int ValueReceived { get; private set; }
91+
92+
[Rpc(SendTo.Owner)]
93+
public void SomeRpc(int valueReceived)
94+
{
95+
ValueReceived = valueReceived;
96+
}
97+
}
98+
#endregion
99+
}
100+
}

com.unity.netcode.gameobjects/Tests/Runtime/Rpc/RpcDuringOnNetworkSpawn.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)