Skip to content

Commit 145aada

Browse files
fix and update
Fixing some issues with getting the session owner prior to actually starting and connecting to a service. Added a better log message that includes the actual name of the NetworkManager when a prefab cannot be found.
1 parent 747711d commit 145aada

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ internal NetworkObject GetNetworkObjectToSpawn(uint globalObjectIdHash, ulong ow
831831
{
832832
if (NetworkLog.CurrentLogLevel <= LogLevel.Error)
833833
{
834-
NetworkLog.LogError($"Failed to create object locally. [{nameof(globalObjectIdHash)}={globalObjectIdHash}]. {nameof(NetworkPrefab)} could not be found. Is the prefab registered with {nameof(NetworkManager)}?");
834+
NetworkLog.LogError($"Failed to create object locally. [{nameof(globalObjectIdHash)}={globalObjectIdHash}]. {nameof(NetworkPrefab)} could not be found. Is the prefab registered with {NetworkManager.name}?");
835835
}
836836
}
837837
else

com.unity.netcode.gameobjects/TestHelpers/Runtime/NetcodeIntegrationTest.cs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,23 @@ protected NetworkManager GetAuthorityNetworkManager()
162162
{
163163
if (m_UseCmbService)
164164
{
165+
// TODO: Because we start the NetworkManagers back-to-back, the service can make a NetworkManager
166+
// (other than the very first one) the session owner. This could lead to test instabilities and
167+
// we need to add additional code during the start process that will start the very 1st instance
168+
// and wait for it to be approved and assigned the session owner before we assume anything.
169+
// Otherwise, any pick from the m_NetworkManagers could result in that specific instance not being
170+
// the first session owner.
171+
// If we haven't even started any NetworkManager, then just return the first
172+
// instance until we resolve the above issue.
173+
if (!NetcodeIntegrationTestHelpers.IsStarted)
174+
{
175+
return m_NetworkManagers[0];
176+
}
177+
165178
foreach (var client in m_NetworkManagers)
166179
{
167-
// If client isn't approved we are still in setup and want to return the first client
168-
// otherwise look for the session owner
180+
// See above notes.
181+
// TODO: Once the above is resolved, just check for client.LocalClient.IsSessionOwner
169182
if (!client.LocalClient.IsApproved || client.LocalClient.IsSessionOwner)
170183
{
171184
return client;
@@ -203,7 +216,7 @@ protected NetworkManager GetNonAuthorityNetworkManager()
203216
/// Indicates whether the currently running tests are targeting the hosted CMB Service
204217
/// </summary>
205218
/// <remarks>Can only be true if <see cref="UseCMBService"/> returns true.</remarks>
206-
protected bool m_UseCmbService;
219+
protected bool m_UseCmbService { get; private set; }
207220

208221
/// <summary>
209222
/// Indicates whether a hosted CMB service is available.
@@ -419,6 +432,9 @@ protected virtual void OnInlineSetup()
419432
[UnitySetUp]
420433
public IEnumerator SetUp()
421434
{
435+
// In addition to setting the number of clients in the OneTimeSetup, we need to re-apply the number of clients for each unique test
436+
// in the event that a previous test stopped a client.
437+
m_NumberOfClients = NumberOfClients;
422438
VerboseDebugLog.Clear();
423439
VerboseDebug($"Entering {nameof(SetUp)}");
424440
NetcodeLogAssert = new NetcodeLogAssert();
@@ -521,7 +537,7 @@ protected void CreateServerAndClients()
521537
{
522538
// If we are connecting to a CMB server and we have a zero client count,
523539
// then we must make m_NumberOfClients = 1 for the session owner.
524-
if (UseCMBService() && NumberOfClients == 0 && m_NumberOfClients == 0)
540+
if (m_UseCmbService && NumberOfClients == 0 && m_NumberOfClients == 0)
525541
{
526542
m_NumberOfClients = 1;
527543
}
@@ -816,7 +832,7 @@ protected void CreateServerAndClients(int numberOfClients)
816832
// In the event this is invoked within a derived integration test and
817833
// the number of clients is 0, then we need to have at least 1 client
818834
// to be the session owner.
819-
if (UseCMBService() && numberOfClients == 0)
835+
if (m_UseCmbService && numberOfClients == 0)
820836
{
821837
numberOfClients = 1;
822838
// If m_NumberOfCleints == 0, then we should increment it.
@@ -995,7 +1011,6 @@ protected virtual bool ShouldCheckForSpawnedPlayers()
9951011
return true;
9961012
}
9971013

998-
9991014
/// <summary>
10001015
/// This starts the server and clients as long as <see cref="CanStartServerAndClients"/>
10011016
/// returns true.
@@ -1009,16 +1024,16 @@ protected IEnumerator StartServerAndClients()
10091024
// Start the instances and pass in our SceneManagerInitialization action that is invoked immediately after host-server
10101025
// is started and after each client is started.
10111026

1012-
// When using the CMBService, we don't have a server, so get the appropriate authority network manager
1013-
var authorityManager = GetAuthorityNetworkManager();
1014-
10151027
VerboseDebug($"Starting with useCmbService: {m_UseCmbService}");
10161028
if (!NetcodeIntegrationTestHelpers.Start(m_UseHost, !m_UseCmbService, m_ServerNetworkManager, m_ClientNetworkManagers))
10171029
{
10181030
Debug.LogError("Failed to start instances");
10191031
Assert.Fail("Failed to start instances");
10201032
}
10211033

1034+
// When using the CMBService, we don't have a server, so get the appropriate authority network manager
1035+
var authorityManager = GetAuthorityNetworkManager();
1036+
10221037
// When scene management is enabled, we need to re-apply the scenes populated list since we have overriden the ISceneManagerHandler
10231038
// imeplementation at this point. This assures any pre-loaded scenes will be automatically assigned to the server and force clients
10241039
// to load their own scenes.
@@ -1382,9 +1397,11 @@ private void DestroyNetworkManagers()
13821397
var networkManagers = Object.FindObjectsByType<NetworkManager>(FindObjectsSortMode.None);
13831398
foreach (var networkManager in networkManagers)
13841399
{
1385-
13861400
Object.DestroyImmediate(networkManager.gameObject);
13871401
}
1402+
m_NetworkManagers = null;
1403+
m_ClientNetworkManagers = null;
1404+
m_ServerNetworkManager = null;
13881405
}
13891406

13901407
/// <summary>
@@ -1949,7 +1966,6 @@ private void InitializeTestConfiguration(NetworkTopologyTypes networkTopologyTyp
19491966
if (UseCMBService())
19501967
{
19511968
m_UseCmbService = m_DistributedAuthority && hostOrServer == HostOrServer.DAHost;
1952-
19531969
}
19541970
}
19551971

com.unity.netcode.gameobjects/TestHelpers/Runtime/NetcodeIntegrationTestHelpers.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public static class NetcodeIntegrationTestHelpers
2121
private static List<NetworkManager> s_NetworkManagerInstances = new List<NetworkManager>();
2222
private static Dictionary<NetworkManager, MultiInstanceHooks> s_Hooks = new Dictionary<NetworkManager, MultiInstanceHooks>();
2323
private static bool s_IsStarted;
24+
internal static bool IsStarted => s_IsStarted;
2425
private static int s_ClientCount;
2526
private static int s_OriginalTargetFrameRate = -1;
2627

@@ -643,11 +644,6 @@ internal static GameObject CreateNetworkObject(string baseName, NetworkManager o
643644
/// <returns>The prefab's root <see cref="GameObject"/></returns>
644645
public static GameObject CreateNetworkObjectPrefab(string baseName, NetworkManager authorityNetworkManager, params NetworkManager[] clients)
645646
{
646-
void AddNetworkPrefab(NetworkConfig config, NetworkPrefab prefab)
647-
{
648-
config.Prefabs.Add(prefab);
649-
}
650-
651647
var prefabCreateAssertError = $"You can only invoke this method before starting the network manager(s)!";
652648
Assert.IsNotNull(authorityNetworkManager, prefabCreateAssertError);
653649
Assert.IsFalse(authorityNetworkManager.IsListening, prefabCreateAssertError);
@@ -657,14 +653,14 @@ void AddNetworkPrefab(NetworkConfig config, NetworkPrefab prefab)
657653

658654
// We could refactor this test framework to share a NetworkPrefabList instance, but at this point it's
659655
// probably more trouble than it's worth to verify these lists stay in sync across all tests...
660-
AddNetworkPrefab(authorityNetworkManager.NetworkConfig, networkPrefab);
656+
authorityNetworkManager.NetworkConfig.Prefabs.Add(networkPrefab);
661657
foreach (var clientNetworkManager in clients)
662658
{
663659
if (clientNetworkManager == authorityNetworkManager)
664660
{
665661
continue;
666662
}
667-
AddNetworkPrefab(clientNetworkManager.NetworkConfig, networkPrefab);
663+
clientNetworkManager.NetworkConfig.Prefabs.Add(new NetworkPrefab() { Prefab = gameObject });
668664
}
669665
return gameObject;
670666
}

0 commit comments

Comments
 (0)