Class NetcodeIntegrationTestHelpers
Provides helpers for running multi instance tests.
Inherited Members
Namespace: Unity.Netcode.TestHelpers.Runtime
Assembly: Unity.Netcode.TestHelpers.Runtime.dll
Syntax
public static class NetcodeIntegrationTestHelpers
  Fields
DefaultMinFrames
Declaration
public const int DefaultMinFrames = 1
  Field Value
| Type | Description | 
|---|---|
| int | 
DefaultTimeout
Declaration
public const float DefaultTimeout = 4
  Field Value
| Type | Description | 
|---|---|
| float | 
Properties
IsNetcodeIntegrationTestRunning
Declaration
public static bool IsNetcodeIntegrationTestRunning { get; }
  Property Value
| Type | Description | 
|---|---|
| bool | 
NetworkManagerInstances
Declaration
public static List<NetworkManager> NetworkManagerInstances { get; }
  Property Value
| Type | Description | 
|---|---|
| List<NetworkManager> | 
Methods
CleanUpHandlers()
Call this to clean up the IntegrationTestSceneHandler and destroy the s_CoroutineRunner. Note: If deriving from NetcodeIntegrationTest or using Destroy() then you typically won't need to do this.
Declaration
public static void CleanUpHandlers()
  Create(int, out NetworkManager, out NetworkManager[], int, bool, bool)
Creates NetworkingManagers and configures them for use in a multi instance setting.
Declaration
public static bool Create(int clientCount, out NetworkManager server, out NetworkManager[] clients, int targetFrameRate = 60, bool serverFirst = true, bool useMockTransport = false)
  Parameters
| Type | Name | Description | 
|---|---|---|
| int | clientCount | The amount of clients  | 
      
| NetworkManager | server | The server NetworkManager  | 
      
| NetworkManager[] | clients | The clients NetworkManagers  | 
      
| int | targetFrameRate | The targetFrameRate of the Unity engine to use while the multi instance helper is running. Will be reset on shutdown.  | 
      
| bool | serverFirst | This determines if the server or clients will be instantiated first (defaults to server first)  | 
      
| bool | useMockTransport | 
Returns
| Type | Description | 
|---|---|
| bool | 
CreateNetworkObjectPrefab(string, NetworkManager, params NetworkManager[])
Declaration
public static GameObject CreateNetworkObjectPrefab(string baseName, NetworkManager server, params NetworkManager[] clients)
  Parameters
| Type | Name | Description | 
|---|---|---|
| string | baseName | |
| NetworkManager | server | |
| NetworkManager[] | clients | 
Returns
| Type | Description | 
|---|---|
| GameObject | 
CreateNewClients(int, out NetworkManager[], bool)
Used to add a client to the already existing list of clients
Declaration
public static bool CreateNewClients(int clientCount, out NetworkManager[] clients, bool useMockTransport = false)
  Parameters
| Type | Name | Description | 
|---|---|---|
| int | clientCount | The amount of clients  | 
      
| NetworkManager[] | clients | Output array containing the newly created NetworkManager instances.  | 
      
| bool | useMockTransport | If true, uses mock transport for testing. Defaults to false.  | 
      
Returns
| Type | Description | 
|---|---|
| bool | 
CreateServer(bool)
Declaration
public static NetworkManager CreateServer(bool mockTransport = false)
  Parameters
| Type | Name | Description | 
|---|---|---|
| bool | mockTransport | 
Returns
| Type | Description | 
|---|---|
| NetworkManager | 
Destroy()
Should always be invoked when finished with a single unit test (i.e. during TearDown)
Declaration
public static void Destroy()
  GetGlobalObjectIdHash(NetworkObject)
Declaration
public static uint GetGlobalObjectIdHash(NetworkObject networkObject)
  Parameters
| Type | Name | Description | 
|---|---|---|
| NetworkObject | networkObject | 
Returns
| Type | Description | 
|---|---|
| uint | 
GetNetworkObjectByRepresentation(Func<NetworkObject, bool>, NetworkManager, ResultWrapper<NetworkObject>, bool, float)
Gets a NetworkObject instance as it's represented by a certain peer.
Declaration
public static IEnumerator GetNetworkObjectByRepresentation(Func<NetworkObject, bool> predicate, NetworkManager representation, NetcodeIntegrationTestHelpers.ResultWrapper<NetworkObject> result, bool failIfNull = true, float timeout = 4)
  Parameters
| Type | Name | Description | 
|---|---|---|
| Func<NetworkObject, bool> | predicate | The predicate used to filter for your target NetworkObject  | 
      
| NetworkManager | representation | The representation to get the object from  | 
      
| NetcodeIntegrationTestHelpers.ResultWrapper<NetworkObject> | result | The result  | 
      
| bool | failIfNull | Whether or not to fail if no object is found and result is null  | 
      
| float | timeout | Maximum time in seconds to wait for the object to be found.  | 
      
Returns
| Type | Description | 
|---|---|
| IEnumerator | 
GetNetworkObjectByRepresentation(ulong, NetworkManager, ResultWrapper<NetworkObject>, bool, float)
Gets a NetworkObject instance as it's represented by a certain peer.
Declaration
public static IEnumerator GetNetworkObjectByRepresentation(ulong networkObjectId, NetworkManager representation, NetcodeIntegrationTestHelpers.ResultWrapper<NetworkObject> result, bool failIfNull = true, float timeout = 4)
  Parameters
| Type | Name | Description | 
|---|---|---|
| ulong | networkObjectId | The networkObjectId to get  | 
      
| NetworkManager | representation | The representation to get the object from  | 
      
| NetcodeIntegrationTestHelpers.ResultWrapper<NetworkObject> | result | The result  | 
      
| bool | failIfNull | Whether or not to fail if no object is found and result is null  | 
      
| float | timeout | Maximum time in seconds to wait for the object to be found.  | 
      
Returns
| Type | Description | 
|---|---|
| IEnumerator | 
GetNetworkObjectByRepresentationWithTimeTravel(Func<NetworkObject, bool>, NetworkManager, ResultWrapper<NetworkObject>, bool, int)
Gets a NetworkObject instance as it's represented by a certain peer.
Declaration
public static void GetNetworkObjectByRepresentationWithTimeTravel(Func<NetworkObject, bool> predicate, NetworkManager representation, NetcodeIntegrationTestHelpers.ResultWrapper<NetworkObject> result, bool failIfNull = true, int maxTries = 60)
  Parameters
| Type | Name | Description | 
|---|---|---|
| Func<NetworkObject, bool> | predicate | The predicate used to filter for your target NetworkObject  | 
      
| NetworkManager | representation | The representation to get the object from  | 
      
| NetcodeIntegrationTestHelpers.ResultWrapper<NetworkObject> | result | The result  | 
      
| bool | failIfNull | Whether or not to fail if no object is found and result is null  | 
      
| int | maxTries | The max frames to wait for  | 
      
GetNextGlobalIdHashValue()
Declaration
public static uint GetNextGlobalIdHashValue()
  Returns
| Type | Description | 
|---|---|
| uint | 
MakeNetworkObjectTestPrefab(NetworkObject, uint)
Normally we would only allow player prefabs to be set to a prefab. Not runtime created objects. In order to prevent having a Resource folder full of a TON of prefabs that we have to maintain, MultiInstanceHelper has a helper function that lets you mark a runtime created object to be treated as a prefab by the Netcode. That's how we can get away with creating the player prefab at runtime without it being treated as a SceneObject or causing other conflicts with the Netcode.
Declaration
public static void MakeNetworkObjectTestPrefab(NetworkObject networkObject, uint globalObjectIdHash = 0)
  Parameters
| Type | Name | Description | 
|---|---|---|
| NetworkObject | networkObject | The networkObject to be treated as Prefab  | 
      
| uint | globalObjectIdHash | The GlobalObjectId to force  | 
      
MarkAsSceneObjectRoot(GameObject, NetworkManager, NetworkManager[])
Declaration
public static void MarkAsSceneObjectRoot(GameObject networkObjectRoot, NetworkManager server, NetworkManager[] clients)
  Parameters
| Type | Name | Description | 
|---|---|---|
| GameObject | networkObjectRoot | |
| NetworkManager | server | |
| NetworkManager[] | clients | 
RefreshAllPrefabInstances(NetworkObject, Action)
Declaration
public static void RefreshAllPrefabInstances(NetworkObject networkObject, Action scenesProcessed)
  Parameters
| Type | Name | Description | 
|---|---|---|
| NetworkObject | networkObject | |
| Action | scenesProcessed | 
RegisterHandlers(NetworkManager, bool)
Call this to register scene validation and the IntegrationTestSceneHandler Note: If deriving from NetcodeIntegrationTest or using Destroy() then you typically won't need to call this.
Declaration
public static void RegisterHandlers(NetworkManager networkManager, bool serverSideSceneManager = false)
  Parameters
| Type | Name | Description | 
|---|---|---|
| NetworkManager | networkManager | |
| bool | serverSideSceneManager | 
RegisterNetcodeIntegrationTest(bool)
Declaration
public static void RegisterNetcodeIntegrationTest(bool registered)
  Parameters
| Type | Name | Description | 
|---|---|---|
| bool | registered | 
SetRefreshAllPrefabsCallback(Action)
Declaration
public static void SetRefreshAllPrefabsCallback(Action scenesProcessed)
  Parameters
| Type | Name | Description | 
|---|---|---|
| Action | scenesProcessed | 
Start(bool, NetworkManager, NetworkManager[], BeforeClientStartCallback)
Starts NetworkManager instances created by the Create method.
Declaration
public static bool Start(bool host, NetworkManager server, NetworkManager[] clients, NetcodeIntegrationTestHelpers.BeforeClientStartCallback callback = null)
  Parameters
| Type | Name | Description | 
|---|---|---|
| bool | host | Whether or not to create a Host instead of Server.  | 
      
| NetworkManager | server | The Server NetworkManager.  | 
      
| NetworkManager[] | clients | The Clients NetworkManager.  | 
      
| NetcodeIntegrationTestHelpers.BeforeClientStartCallback | callback | called immediately after server is started and before client(s) are started.  | 
      
Returns
| Type | Description | 
|---|---|
| bool | True if the network instances were started successfully, throws InvalidOperationException if already started.  | 
      
StartOneClient(NetworkManager)
Starts one single client and makes sure to register the required hooks and handlers
Declaration
public static void StartOneClient(NetworkManager clientToStart)
  Parameters
| Type | Name | Description | 
|---|---|---|
| NetworkManager | clientToStart | The NetworkManager instance to start. Must not be null.  | 
      
StopOneClient(NetworkManager, bool)
Stops one single client and makes sure to cleanup any static variables in this helper
Declaration
public static void StopOneClient(NetworkManager clientToStop, bool destroy = true)
  Parameters
| Type | Name | Description | 
|---|---|---|
| NetworkManager | clientToStop | The NetworkManager instance to stop. Must not be null.  | 
      
| bool | destroy | If true, destroys the GameObject and removes from NetworkManagerInstances. Defaults to true.  | 
      
WaitForClientConnected(NetworkManager, ResultWrapper<bool>, float)
Waits on the client side to be connected.
Declaration
public static IEnumerator WaitForClientConnected(NetworkManager client, NetcodeIntegrationTestHelpers.ResultWrapper<bool> result = null, float timeout = 4)
  Parameters
| Type | Name | Description | 
|---|---|---|
| NetworkManager | client | The client  | 
      
| NetcodeIntegrationTestHelpers.ResultWrapper<bool> | result | The result. If null, it will automatically assert  | 
      
| float | timeout | Maximum time in seconds to wait for the object to be found.  | 
      
Returns
| Type | Description | 
|---|---|
| IEnumerator | 
WaitForClientConnectedToServer(NetworkManager, ResultWrapper<bool>, float)
Waits on the server side for 1 client to be connected
Declaration
public static IEnumerator WaitForClientConnectedToServer(NetworkManager server, NetcodeIntegrationTestHelpers.ResultWrapper<bool> result = null, float timeout = 4)
  Parameters
| Type | Name | Description | 
|---|---|---|
| NetworkManager | server | The server  | 
      
| NetcodeIntegrationTestHelpers.ResultWrapper<bool> | result | The result. If null, it will automatically assert  | 
      
| float | timeout | Maximum time in seconds to wait for the object to be found.  | 
      
Returns
| Type | Description | 
|---|---|
| IEnumerator | 
WaitForClientsConnected(NetworkManager[], ResultWrapper<bool>, float)
Similar to WaitForClientConnected, this waits for multiple clients to be connected.
Declaration
public static IEnumerator WaitForClientsConnected(NetworkManager[] clients, NetcodeIntegrationTestHelpers.ResultWrapper<bool> result = null, float timeout = 4)
  Parameters
| Type | Name | Description | 
|---|---|---|
| NetworkManager[] | clients | Array of clients to wait for.  | 
      
| NetcodeIntegrationTestHelpers.ResultWrapper<bool> | result | The result. If null, it will automatically assert.  | 
      
| float | timeout | Maximum time in seconds to wait for the object to be found.  | 
      
Returns
| Type | Description | 
|---|---|
| IEnumerator | An IEnumerator that yields until all clients are connected or timeout is reached.  | 
      
WaitForClientsConnectedToServer(NetworkManager, int, ResultWrapper<bool>, float)
Waits on the server side for 1 client to be connected
Declaration
public static IEnumerator WaitForClientsConnectedToServer(NetworkManager server, int clientCount = 1, NetcodeIntegrationTestHelpers.ResultWrapper<bool> result = null, float timeout = 4)
  Parameters
| Type | Name | Description | 
|---|---|---|
| NetworkManager | server | The server  | 
      
| int | clientCount | |
| NetcodeIntegrationTestHelpers.ResultWrapper<bool> | result | The result. If null, it will automatically assert  | 
      
| float | timeout | Maximum time in seconds to wait for the object to be found.  | 
      
Returns
| Type | Description | 
|---|---|
| IEnumerator | 
WaitForCondition(Func<bool>, ResultWrapper<bool>, float, int)
Waits for a predicate condition to be met
Declaration
public static IEnumerator WaitForCondition(Func<bool> predicate, NetcodeIntegrationTestHelpers.ResultWrapper<bool> result = null, float timeout = 4, int minFrames = 1)
  Parameters
| Type | Name | Description | 
|---|---|---|
| Func<bool> | predicate | The predicate to wait for  | 
      
| NetcodeIntegrationTestHelpers.ResultWrapper<bool> | result | The result. If null, it will fail if the predicate is not met  | 
      
| float | timeout | Maximum time in seconds to wait for the object to be found.  | 
      
| int | minFrames | The min frames to wait for  | 
      
Returns
| Type | Description | 
|---|---|
| IEnumerator |