Class NetworkVariableTests
Inheritance
NetworkVariableTests
Assembly: Unity.Netcode.RuntimeTests.dll
Syntax
[TestFixture(new object[] { true })]
[TestFixture(new object[] { false })]
public class NetworkVariableTests : NetcodeIntegrationTest
Constructors
NetworkVariableTests(bool)
Declaration
public NetworkVariableTests(bool ensureLengthSafety)
Parameters
Type |
Name |
Description |
bool |
ensureLengthSafety |
|
Properties
NumberOfClients
Declaration
protected override int NumberOfClients { get; }
Property Value
Overrides
m_EnableTimeTravel
Enables "Time Travel" within the test, which swaps the time provider for the SDK from Unity's
Time class to MockTimeProvider, and also swaps the transport implementation
from UnityTransport to MockTransport.
This enables five important things that help with both performance and determinism of tests that involve a
lot of time and waiting:
- It allows time to move in a completely deterministic way (testing that something happens after n seconds,
the test will always move exactly n seconds with no chance of any variability in the timing),
- It allows skipping periods of time without actually waiting that amount of time, while still simulating
SDK frames as if that time were passing,
- It dissociates the SDK's update loop from Unity's update loop, allowing us to simulate SDK frame updates
without waiting for Unity to process things like physics, animation, and rendering that aren't relevant to
the test,
- It dissociates the SDK's messaging system from the networking hardware, meaning there's no delay between
a message being sent and it being received, allowing us to deterministically rely on the message being
received within specific time frames for the test, and
- It allows tests to be written without the use of coroutines, which not only improves the test's runtime,
but also results in easier-to-read callstacks and removes the possibility for an assertion to result in the
test hanging.
When time travel is enabled, the following methods become available:
TimeTravel(double, int): Simulates a specific number of frames passing over a specific time period
TimeTravelToNextTick(): Skips forward to the next tick, siumlating at the current application frame rate
WaitForConditionOrTimeOutWithTimeTravel(Func<bool>, int): Simulates frames at the application frame rate until the given condition is true
WaitForMessageReceivedWithTimeTravel<T>(List<NetworkManager>, ReceiptType): Simulates frames at the application frame rate until the required message is received
WaitForMessagesReceivedWithTimeTravel(List<Type>, List<NetworkManager>, ReceiptType): Simulates frames at the application frame rate until the required messages are received
StartServerAndClientsWithTimeTravel(): Starts a server and client and allows them to connect via simulated frames
CreateAndStartNewClientWithTimeTravel(): Creates a client and waits for it to connect via simulated frames
WaitForClientsConnectedOrTimeOutWithTimeTravel(NetworkManager[]) Simulates frames at the application frame rate until the given clients are connected
StopOneClientWithTimeTravel(NetworkManager, bool): Stops a client and simulates frames until it's fully disconnected.
When time travel is enabled, NetcodeIntegrationTest will automatically use these in its methods
when doing things like automatically connecting clients during SetUp.
Additionally, the following methods replace their non-time-travel equivalents with variants that are not coroutines:
OnTimeTravelStartedServerAndClients() - called when server and clients are started
OnTimeTravelServerAndClientsConnected() - called when server and clients are connected
Note that all of the non-time travel functions can still be used even when time travel is enabled - this is
sometimes needed for, e.g., testing NetworkAnimator, where the unity update loop needs to run to process animations.
However, it's VERY important to note here that, because the SDK will not be operating based on real-world time
but based on the frozen time that's locked in from MockTimeProvider, actions that pass 10 seconds apart by
real-world clock time will be perceived by the SDK as having happened simultaneously if you don't call
TimeTravel(double) to cover the equivalent time span in the mock time provider.
(Calling TimeTravel(double) instead of TimeTravel(double, int)
will move time forward without simulating any frames, which, in the case where real-world time has passed,
is likely more desirable). In most cases, this desynch won't affect anything, but it is worth noting that
it happens just in case a tested system depends on both the unity update loop happening and time moving forward.
Declaration
protected override bool m_EnableTimeTravel { get; }
Property Value
Overrides
m_SetupIsACoroutine
If this is false, SetUp will call OnInlineSetUp instead of OnSetUp.
This is a performance advantage when not using the coroutine functionality, as a coroutine that
has no yield instructions in it will nonetheless still result in delaying the continuation of the
method that called it for a full frame after it returns.
Declaration
protected override bool m_SetupIsACoroutine { get; }
Property Value
Overrides
m_TearDownIsACoroutine
If this is false, TearDown will call OnInlineTearDown instead of OnTearDown.
This is a performance advantage when not using the coroutine functionality, as a coroutine that
has no yield instructions in it will nonetheless still result in delaying the continuation of the
method that called it for a full frame after it returns.
Declaration
protected override bool m_TearDownIsACoroutine { get; }
Property Value
Overrides
Methods
AllNetworkVariableTypes(HostOrServer)
Runs generalized tests on all predefined NetworkVariable types
Declaration
[Test]
public void AllNetworkVariableTypes(NetcodeIntegrationTest.HostOrServer useHost)
Parameters
ArrayStr<T>(NativeArray<T>)
Declaration
public string ArrayStr<T>(NativeArray<T> arr) where T : unmanaged
Parameters
Returns
Type Parameters
AssertArraysDoNotMatch<T>(ref NativeArray<T>, ref NativeArray<T>)
Declaration
public void AssertArraysDoNotMatch<T>(ref NativeArray<T> a, ref NativeArray<T> b) where T : unmanaged
Parameters
Type Parameters
AssertArraysMatch<T>(ref NativeArray<T>, ref NativeArray<T>)
Declaration
public void AssertArraysMatch<T>(ref NativeArray<T> a, ref NativeArray<T> b) where T : unmanaged
Parameters
Type Parameters
AssertListsDoNotMatch<T>(ref List<T>, ref List<T>)
Declaration
public void AssertListsDoNotMatch<T>(ref List<T> a, ref List<T> b)
Parameters
Type |
Name |
Description |
List<T> |
a |
|
List<T> |
b |
|
Type Parameters
AssertListsMatch<T>(ref List<T>, ref List<T>)
Declaration
public void AssertListsMatch<T>(ref List<T> a, ref List<T> b)
Parameters
Type |
Name |
Description |
List<T> |
a |
|
List<T> |
b |
|
Type Parameters
AssertMapsDoNotMatch<TKey, TVal>(ref Dictionary<TKey, TVal>, ref Dictionary<TKey, TVal>)
Declaration
public void AssertMapsDoNotMatch<TKey, TVal>(ref Dictionary<TKey, TVal> a, ref Dictionary<TKey, TVal> b) where TKey : IEquatable<TKey>
Parameters
Type Parameters
Name |
Description |
TKey |
|
TVal |
|
AssertMapsMatch<TKey, TVal>(ref Dictionary<TKey, TVal>, ref Dictionary<TKey, TVal>)
Declaration
public void AssertMapsMatch<TKey, TVal>(ref Dictionary<TKey, TVal> a, ref Dictionary<TKey, TVal> b) where TKey : IEquatable<TKey>
Parameters
Type Parameters
Name |
Description |
TKey |
|
TVal |
|
AssertSetsDoNotMatch<T>(ref HashSet<T>, ref HashSet<T>)
Declaration
public void AssertSetsDoNotMatch<T>(ref HashSet<T> a, ref HashSet<T> b) where T : IEquatable<T>
Parameters
Type Parameters
AssertSetsMatch<T>(ref HashSet<T>, ref HashSet<T>)
Declaration
public void AssertSetsMatch<T>(ref HashSet<T> a, ref HashSet<T> b) where T : IEquatable<T>
Parameters
Type Parameters
CanStartServerAndClients()
Override this method and return false in order to be able
to manually control when the server and clients are started.
Declaration
protected override bool CanStartServerAndClients()
Returns
Overrides
ClientNetworkVariableTestSpawned(NetworkVariableTest)
Declaration
public static void ClientNetworkVariableTestSpawned(NetworkVariableTest networkVariableTest)
Parameters
ClientWritePermissionTest(HostOrServer)
Declaration
[Test]
public void ClientWritePermissionTest(NetcodeIntegrationTest.HostOrServer useHost)
Parameters
DictionaryStr<TKey, TVal>(Dictionary<TKey, TVal>)
Declaration
public string DictionaryStr<TKey, TVal>(Dictionary<TKey, TVal> list) where TKey : IEquatable<TKey>
Parameters
Returns
Type Parameters
Name |
Description |
TKey |
|
TVal |
|
FixedString32Test(HostOrServer)
Declaration
[Test]
public void FixedString32Test(NetcodeIntegrationTest.HostOrServer useHost)
Parameters
GetArarys<T>(GetRandomElement<T>)
Declaration
public (NativeArray<T> original, NativeArray<T> original2, NativeArray<T> changed, NativeArray<T> changed2) GetArarys<T>(NetworkVariableTests.GetRandomElement<T> generator) where T : unmanaged
Parameters
Returns
Type Parameters
GetDictionaries<TKey, TVal>(GetRandomElement<TKey>, GetRandomElement<TVal>)
Declaration
public (Dictionary<TKey, TVal> original, Dictionary<TKey, TVal> original2, Dictionary<TKey, TVal> changed, Dictionary<TKey, TVal> changed2) GetDictionaries<TKey, TVal>(NetworkVariableTests.GetRandomElement<TKey> keyGenerator, NetworkVariableTests.GetRandomElement<TVal> valGenerator) where TKey : IEquatable<TKey>
Parameters
Returns
Type Parameters
Name |
Description |
TKey |
|
TVal |
|
GetHashSets<T>(GetRandomElement<T>)
Declaration
public (HashSet<T> original, HashSet<T> original2, HashSet<T> changed, HashSet<T> changed2) GetHashSets<T>(NetworkVariableTests.GetRandomElement<T> generator) where T : IEquatable<T>
Parameters
Returns
Type Parameters
GetLists<T>(GetRandomElement<T>)
Declaration
public (List<T> original, List<T> original2, List<T> changed, List<T> changed2) GetLists<T>(NetworkVariableTests.GetRandomElement<T> generator)
Parameters
Returns
Type Parameters
HashSetStr<T>(HashSet<T>)
Declaration
public string HashSetStr<T>(HashSet<T> list) where T : IEquatable<T>
Parameters
Type |
Name |
Description |
HashSet<T> |
list |
|
Returns
Type Parameters
ListStr<T>(List<T>)
Declaration
public string ListStr<T>(List<T> list)
Parameters
Type |
Name |
Description |
List<T> |
list |
|
Returns
Type Parameters
NetworkListAdd(HostOrServer)
Declaration
[Test]
public void NetworkListAdd(NetcodeIntegrationTest.HostOrServer useHost)
Parameters
NetworkListClear(HostOrServer)
Declaration
[Test]
public void NetworkListClear(NetcodeIntegrationTest.HostOrServer useHost)
Parameters
NetworkListContains(HostOrServer)
Declaration
[Test]
public void NetworkListContains(NetcodeIntegrationTest.HostOrServer useHost)
Parameters
NetworkListIndexOf(HostOrServer)
Declaration
[Test]
public void NetworkListIndexOf(NetcodeIntegrationTest.HostOrServer useHost)
Parameters
NetworkListInsert(HostOrServer)
Declaration
[Test]
public void NetworkListInsert(NetcodeIntegrationTest.HostOrServer useHost)
Parameters
NetworkListRemoveTests(HostOrServer, ListRemoveTypes)
Declaration
[Test]
public void NetworkListRemoveTests(NetcodeIntegrationTest.HostOrServer useHost, NetworkVariableTests.ListRemoveTypes listRemoveType)
Parameters
NetworkListValueUpdate(HostOrServer)
Declaration
[Test]
public void NetworkListValueUpdate(NetcodeIntegrationTest.HostOrServer useHost)
Parameters
NetworkVariableSync_WithDifferentTimeScale(HostOrServer, float)
Runs tests that network variables sync on client whatever the local value of timeScale.
Declaration
[Test]
public void NetworkVariableSync_WithDifferentTimeScale(NetcodeIntegrationTest.HostOrServer useHost, float timeScale)
Parameters
OnSetup()
Declaration
protected override IEnumerator OnSetup()
Returns
Overrides
OnTearDown()
Note: For PerTest mode
this is called before ShutdownAndCleanUp.
Declaration
protected override IEnumerator OnTearDown()
Returns
Overrides
RandGenBytes<T>(Random)
Declaration
public T RandGenBytes<T>(Random rand) where T : unmanaged
Parameters
Type |
Name |
Description |
Random |
rand |
|
Returns
Type Parameters
RandGenFixedString32(Random)
Declaration
public FixedString32Bytes RandGenFixedString32(Random rand)
Parameters
Type |
Name |
Description |
Random |
rand |
|
Returns
Type |
Description |
FixedString32Bytes |
|
TestCustomGenericSerialization()
Declaration
[Test]
public void TestCustomGenericSerialization()
TestINetworkSerializableClassCallsNetworkSerialize(HostOrServer)
Declaration
[Test]
public void TestINetworkSerializableClassCallsNetworkSerialize(NetcodeIntegrationTest.HostOrServer useHost)
Parameters
TestINetworkSerializableStructCallsNetworkSerialize(HostOrServer)
Declaration
[Test]
public void TestINetworkSerializableStructCallsNetworkSerialize(NetcodeIntegrationTest.HostOrServer useHost)
Parameters
TestManagedINetworkSerializableNetworkVariablesDeserializeInPlace()
Declaration
[Test]
public void TestManagedINetworkSerializableNetworkVariablesDeserializeInPlace()
TestNetworkListStruct(HostOrServer)
Declaration
[Test]
public void TestNetworkListStruct(NetcodeIntegrationTest.HostOrServer useHost)
Parameters
TestNetworkVariableClass(HostOrServer)
Declaration
[Test]
public void TestNetworkVariableClass(NetcodeIntegrationTest.HostOrServer useHost)
Parameters
TestNetworkVariableEnum(HostOrServer)
Declaration
[Test]
public void TestNetworkVariableEnum(NetcodeIntegrationTest.HostOrServer useHost)
Parameters
TestNetworkVariableStruct(HostOrServer)
Declaration
[Test]
public void TestNetworkVariableStruct(NetcodeIntegrationTest.HostOrServer useHost)
Parameters
TestNetworkVariableTemplateBehaviourClass(HostOrServer)
Declaration
[Test]
public void TestNetworkVariableTemplateBehaviourClass(NetcodeIntegrationTest.HostOrServer useHost)
Parameters
TestNetworkVariableTemplateBehaviourClassNotReferencedElsewhere(HostOrServer)
Declaration
[Test]
public void TestNetworkVariableTemplateBehaviourClassNotReferencedElsewhere(NetcodeIntegrationTest.HostOrServer useHost)
Parameters
TestNetworkVariableTemplateBehaviourStruct(HostOrServer)
Declaration
[Test]
public void TestNetworkVariableTemplateBehaviourStruct(NetcodeIntegrationTest.HostOrServer useHost)
Parameters
TestNetworkVariableTemplateClass(HostOrServer)
Declaration
[Test]
public void TestNetworkVariableTemplateClass(NetcodeIntegrationTest.HostOrServer useHost)
Parameters
TestNetworkVariableTemplateStruct(HostOrServer)
Declaration
[Test]
public void TestNetworkVariableTemplateStruct(NetcodeIntegrationTest.HostOrServer useHost)
Parameters
TestTypesReferencedInSubclassSerializeSuccessfully()
Declaration
[Test]
public void TestTypesReferencedInSubclassSerializeSuccessfully()
TestUnmnagedINetworkSerializableNetworkVariablesDeserializeInPlace()
Declaration
[Test]
public void TestUnmnagedINetworkSerializableNetworkVariablesDeserializeInPlace()
TestUnsupportedManagedTypesThrowExceptions()
Declaration
[Test]
public void TestUnsupportedManagedTypesThrowExceptions()
TestUnsupportedManagedTypesWithUserSerializationDoNotThrowExceptions()
Declaration
[Test]
public void TestUnsupportedManagedTypesWithUserSerializationDoNotThrowExceptions()
TestUnsupportedUnmanagedTypesThrowExceptions()
Declaration
[Test]
public void TestUnsupportedUnmanagedTypesThrowExceptions()
TestUnsupportedUnmanagedTypesWithUserSerializationDoNotThrowExceptions()
Declaration
[Test]
public void TestUnsupportedUnmanagedTypesWithUserSerializationDoNotThrowExceptions()
WhenCreatingAnArrayOfNetVars_InitializingVariablesDoesNotThrowAnException()
Declaration
[Test]
public void WhenCreatingAnArrayOfNetVars_InitializingVariablesDoesNotThrowAnException()
WhenListContainsManyLargeValues_OverflowExceptionIsNotThrown(HostOrServer)
Declaration
[Test]
public void WhenListContainsManyLargeValues_OverflowExceptionIsNotThrown(NetcodeIntegrationTest.HostOrServer useHost)
Parameters
WhenSerializingAndDeserializingValueTypeNativeArrayNetworkVariables_ValuesAreSerializedCorrectly(Type)
Declaration
[Test]
public void WhenSerializingAndDeserializingValueTypeNativeArrayNetworkVariables_ValuesAreSerializedCorrectly(Type testType)
Parameters
Type |
Name |
Description |
Type |
testType |
|
WhenSerializingAndDeserializingValueTypeNetworkVariables_ValuesAreSerializedCorrectly(Type)
Declaration
[Test]
public void WhenSerializingAndDeserializingValueTypeNetworkVariables_ValuesAreSerializedCorrectly(Type testType)
Parameters
Type |
Name |
Description |
Type |
testType |
|
WhenSerializingAndDeserializingVeryLargeDictionaryNetworkVariables_ValuesAreSerializedCorrectly(Type, Type)
Declaration
[Test]
[Repeat(5)]
public void WhenSerializingAndDeserializingVeryLargeDictionaryNetworkVariables_ValuesAreSerializedCorrectly(Type keyType, Type valType)
Parameters
Type |
Name |
Description |
Type |
keyType |
|
Type |
valType |
|
WhenSerializingAndDeserializingVeryLargeHashSetNetworkVariables_ValuesAreSerializedCorrectly(Type)
Declaration
[Test]
[Repeat(5)]
public void WhenSerializingAndDeserializingVeryLargeHashSetNetworkVariables_ValuesAreSerializedCorrectly(Type testType)
Parameters
Type |
Name |
Description |
Type |
testType |
|
WhenSerializingAndDeserializingVeryLargeListNetworkVariables_ValuesAreSerializedCorrectly(Type)
Declaration
[Test]
[Repeat(5)]
public void WhenSerializingAndDeserializingVeryLargeListNetworkVariables_ValuesAreSerializedCorrectly(Type testType)
Parameters
Type |
Name |
Description |
Type |
testType |
|
WhenSerializingAndDeserializingVeryLargeValueTypeNativeArrayNetworkVariables_ValuesAreSerializedCorrectly(Type)
Declaration
[Test]
[Repeat(5)]
public void WhenSerializingAndDeserializingVeryLargeValueTypeNativeArrayNetworkVariables_ValuesAreSerializedCorrectly(Type testType)
Parameters
Type |
Name |
Description |
Type |
testType |
|