This repository has been archived on 2025-04-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
com.unity.netcode.gameobjects/Tests/Runtime/NetworkBehaviourUpdaterTests.cs
Unity Technologies 36d07fad5e com.unity.netcode.gameobjects@1.0.0-pre.4
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com).

## [1.0.0-pre.4] - 2021-01-04

### Added

- Added `com.unity.modules.physics` and `com.unity.modules.physics2d` package dependencies (#1565)

### Removed

- Removed `com.unity.modules.ai` package dependency (#1565)
- Removed `FixedQueue`, `StreamExtensions`, `TypeExtensions` (#1398)

### Fixed
- Fixed in-scene NetworkObjects that are moved into the DDOL scene not getting restored to their original active state (enabled/disabled) after a full scene transition (#1354)
- Fixed invalid IL code being generated when using `this` instead of `this ref` for the FastBufferReader/FastBufferWriter parameter of an extension method. (#1393)
- Fixed an issue where if you are running as a server (not host) the LoadEventCompleted and UnloadEventCompleted events would fire early by the NetworkSceneManager (#1379)
- Fixed a runtime error when sending an array of an INetworkSerializable type that's implemented as a struct (#1402)
- NetworkConfig will no longer throw an OverflowException in GetConfig() when ForceSamePrefabs is enabled and the number of prefabs causes the config blob size to exceed 1300 bytes. (#1385)
- Fixed NetworkVariable not calling NetworkSerialize on INetworkSerializable types (#1383)
- Fixed NullReferenceException on ImportReferences call in NetworkBehaviourILPP (#1434)
- Fixed NetworkObjects not being despawned before they are destroyed during shutdown for client, host, and server instances. (#1390)
- Fixed KeyNotFound exception when removing ownership of a newly spawned NetworkObject that is already owned by the server. (#1500)
- Fixed NetworkManager.LocalClient not being set when starting as a host. (#1511)
- Fixed a few memory leak cases when shutting down NetworkManager during Incoming Message Queue processing. (#1323)

### Changed
- The SDK no longer limits message size to 64k. (The transport may still impose its own limits, but the SDK no longer does.) (#1384)
- Updated com.unity.collections to 1.1.0 (#1451)
2021-01-04 00:00:00 +00:00

181 lines
8.5 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
using Object = UnityEngine.Object;
namespace Unity.Netcode.RuntimeTests
{
public class NetworkBehaviourUpdaterTests : BaseMultiInstanceTest
{
protected override int NbClients => throw new NotSupportedException("handled per test");
private static Type[] s_TypesToTest = new[] { null, typeof(ZeroNetVar), typeof(OneNetVar), typeof(TwoNetVar) };
[UnitySetUp]
public override IEnumerator Setup()
{
yield break;
}
/// <summary>
/// This runs test combinations for the following
/// test with 0, 1, 2 clients
/// test with host and server mode
/// test with 0, 1, 2 spawned objects
/// test with 0, 1, 2 network behaviour per prefab
/// test with 0, 1, 2 network variable per network behaviour
/// for each, update netvar
/// for each check value changed
/// check that all network variables are no longer dirty after update
/// </summary>
/// <param name="nbClients"></param>
/// <param name="useHost"></param>
/// <param name="nbSpawnedObjects"></param>
/// <param name="firstNetworkBehaviour"></param>
/// <param name="secondNetworkBehaviour"></param>
/// <param name="thirdNetworkBehaviour"></param>
/// <returns></returns>
[UnityTest]
public IEnumerator BehaviourUpdaterAllTests([Values(0, 1, 2)] int nbClients, [Values] bool useHost, [Values(0, 1, 2)] int nbSpawnedObjects,
[ValueSource(nameof(s_TypesToTest))] Type firstNetworkBehaviour, [ValueSource(nameof(s_TypesToTest))] Type secondNetworkBehaviour)
{
// Create multiple NetworkManager instances
if (!MultiInstanceHelpers.Create(nbClients, out NetworkManager server, out NetworkManager[] clients))
{
Debug.LogError("Failed to create instances");
Assert.Fail("Failed to create instances");
}
m_ClientNetworkManagers = clients;
m_ServerNetworkManager = server;
Assert.That(m_ClientNetworkManagers.Length, Is.EqualTo(nbClients));
Assert.That(m_ServerNetworkManager, Is.Not.Null);
// setup prefab to spawn
void AddNetworkBehaviour(Type type, GameObject prefab)
{
if (type != null)
{
var info = prefab.AddComponent(type) as INetVarInfo;
}
}
var prefabToSpawn = new GameObject();
var networkObjectPrefab = prefabToSpawn.AddComponent<NetworkObject>();
AddNetworkBehaviour(firstNetworkBehaviour, prefabToSpawn);
AddNetworkBehaviour(secondNetworkBehaviour, prefabToSpawn);
MultiInstanceHelpers.MakeNetworkObjectTestPrefab(networkObjectPrefab);
m_ServerNetworkManager.NetworkConfig.NetworkPrefabs.Add(new NetworkPrefab() { Prefab = prefabToSpawn });
foreach (var clientNetworkManager in m_ClientNetworkManagers)
{
clientNetworkManager.NetworkConfig.NetworkPrefabs.Add(new NetworkPrefab() { Prefab = prefabToSpawn });
}
// Start the instances
if (!MultiInstanceHelpers.Start(useHost, server, clients))
{
Debug.LogError("Failed to start instances");
Assert.Fail("Failed to start instances");
}
// Wait for connection on client side
yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForClientsConnected(clients));
// Wait for connection on server side
yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForClientsConnectedToServer(server, clientCount: useHost ? nbClients + 1 : nbClients));
// gathering netvars to test on
var serverNetVarsToUpdate = new List<NetworkVariable<int>>();
for (int i = 0; i < nbSpawnedObjects; i++)
{
var spawnedObject = Object.Instantiate(prefabToSpawn);
var networkSpawnedObject = spawnedObject.GetComponent<NetworkObject>();
networkSpawnedObject.NetworkManagerOwner = m_ServerNetworkManager;
networkSpawnedObject.Spawn();
int nbBehaviours = 0;
foreach (var networkBehaviour in spawnedObject.GetComponents<NetworkBehaviour>())
{
serverNetVarsToUpdate.AddRange(((INetVarInfo)networkBehaviour).AllNetVars);
nbBehaviours++;
}
Assert.That(nbBehaviours, Is.EqualTo((firstNetworkBehaviour == null ? 0 : 1) + (secondNetworkBehaviour == null ? 0 : 1)));
}
var serverNetVarCount = serverNetVarsToUpdate.Count;
yield return new WaitForSeconds(0); // wait a frame to make sure spawn is done
// todo: with Snapshot spawns enabled and the current race condition, the following line is needed:
// yield return new WaitForSeconds(0.2f); // wait a bit to fix the spawn/update race condition
foreach (var netVar in serverNetVarsToUpdate)
{
Assert.That(netVar.Value, Is.EqualTo(0)); // sanity check
}
// test updating all netvars
int updatedValue = 1;
foreach (var netVar in serverNetVarsToUpdate)
{
netVar.Value = updatedValue;
Assert.That(netVar.IsDirty, Is.True);
}
m_ServerNetworkManager.BehaviourUpdater.NetworkBehaviourUpdate(m_ServerNetworkManager);
// make sure we're not dirty anymore and that clients will receive that new value
foreach (var netVar in serverNetVarsToUpdate)
{
// if we don't have connected clients, netvars remain dirty
Assert.That(netVar.IsDirty, nbClients > 0 || useHost ? Is.Not.True : Is.True);
}
foreach (var client in m_ClientNetworkManagers)
{
var nbVarsCheckedClientSide = 0;
var countSpawnObjectResult = new MultiInstanceHelpers.CoroutineResultWrapper<bool>();
yield return MultiInstanceHelpers.WaitForCondition(() => client.SpawnManager.SpawnedObjects.Count == nbSpawnedObjects, countSpawnObjectResult);
Assert.That(countSpawnObjectResult.Result, Is.True);
foreach (var spawnedObject in client.SpawnManager.SpawnedObjects)
{
foreach (var behaviour in spawnedObject.Value.GetComponentsInChildren<NetworkBehaviour>())
{
foreach (var networkVariable in behaviour.NetworkVariableFields)
{
var varInt = networkVariable as NetworkVariable<int>;
var varUpdateResult = new MultiInstanceHelpers.CoroutineResultWrapper<bool>();
yield return MultiInstanceHelpers.WaitForCondition(() => varInt.Value == updatedValue, varUpdateResult);
Assert.That(varUpdateResult.Result, Is.True);
nbVarsCheckedClientSide++;
Assert.That(varInt.Value, Is.EqualTo(updatedValue));
}
}
}
Assert.That(nbVarsCheckedClientSide, Is.EqualTo(m_ClientNetworkManagers.Length > 0 ? serverNetVarCount : 0));
}
}
}
public interface INetVarInfo
{
public List<NetworkVariable<int>> AllNetVars { get; }
}
public class ZeroNetVar : NetworkBehaviour, INetVarInfo
{
public List<NetworkVariable<int>> AllNetVars => new List<NetworkVariable<int>>(); // Needed to be independant from NetworkBehaviour's list of fields. This way, if that changes, we can still do this validation in this test
}
public class OneNetVar : NetworkBehaviour, INetVarInfo
{
private NetworkVariable<int> m_SomeValue = new NetworkVariable<int>();
public List<NetworkVariable<int>> AllNetVars => new List<NetworkVariable<int>>() { m_SomeValue };
}
public class TwoNetVar : NetworkBehaviour, INetVarInfo
{
private NetworkVariable<int> m_SomeValue = new NetworkVariable<int>();
private NetworkVariable<int> m_SomeOtherValue = new NetworkVariable<int>();
public List<NetworkVariable<int>> AllNetVars => new List<NetworkVariable<int>>() { m_SomeValue, m_SomeOtherValue };
}
}