Skip to content

test: added min frames to multi-instance helper #1170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace Unity.Netcode.RuntimeTests
/// </summary>
public static class MultiInstanceHelpers
{
public const int DefaultMinFrames = 1;
public const int DefaultMaxFrames = 64;
private static List<NetworkManager> s_NetworkManagerInstances = new List<NetworkManager>();
private static bool s_IsStarted;
private static int s_ClientCount;
Expand Down Expand Up @@ -258,7 +260,7 @@ public static void MarkAsSceneObjectRoot(GameObject networkObjectRoot, NetworkMa
/// <param name="client">The client</param>
/// <param name="result">The result. If null, it will automatically assert</param>
/// <param name="maxFrames">The max frames to wait for</param>
public static IEnumerator WaitForClientConnected(NetworkManager client, CoroutineResultWrapper<bool> result = null, int maxFrames = 64)
public static IEnumerator WaitForClientConnected(NetworkManager client, CoroutineResultWrapper<bool> result = null, int maxFrames = DefaultMaxFrames)
{
yield return WaitForClientsConnected(new NetworkManager[] { client }, result, maxFrames);
}
Expand All @@ -270,7 +272,7 @@ public static IEnumerator WaitForClientConnected(NetworkManager client, Coroutin
/// <param name="result">The result. If null, it will automatically assert<</param>
/// <param name="maxFrames">The max frames to wait for</param>
/// <returns></returns>
public static IEnumerator WaitForClientsConnected(NetworkManager[] clients, CoroutineResultWrapper<bool> result = null, int maxFrames = 64)
public static IEnumerator WaitForClientsConnected(NetworkManager[] clients, CoroutineResultWrapper<bool> result = null, int maxFrames = DefaultMaxFrames)
{
// Make sure none are the host client
foreach (var client in clients)
Expand Down Expand Up @@ -324,7 +326,7 @@ public static IEnumerator WaitForClientsConnected(NetworkManager[] clients, Coro
/// <param name="server">The server</param>
/// <param name="result">The result. If null, it will automatically assert</param>
/// <param name="maxFrames">The max frames to wait for</param>
public static IEnumerator WaitForClientConnectedToServer(NetworkManager server, CoroutineResultWrapper<bool> result = null, int maxFrames = 64)
public static IEnumerator WaitForClientConnectedToServer(NetworkManager server, CoroutineResultWrapper<bool> result = null, int maxFrames = DefaultMaxFrames)
{
yield return WaitForClientsConnectedToServer(server, server.IsHost ? s_ClientCount + 1 : s_ClientCount, result, maxFrames);
}
Expand All @@ -335,7 +337,7 @@ public static IEnumerator WaitForClientConnectedToServer(NetworkManager server,
/// <param name="server">The server</param>
/// <param name="result">The result. If null, it will automatically assert</param>
/// <param name="maxFrames">The max frames to wait for</param>
public static IEnumerator WaitForClientsConnectedToServer(NetworkManager server, int clientCount = 1, CoroutineResultWrapper<bool> result = null, int maxFrames = 64)
public static IEnumerator WaitForClientsConnectedToServer(NetworkManager server, int clientCount = 1, CoroutineResultWrapper<bool> result = null, int maxFrames = DefaultMaxFrames)
{
if (!server.IsServer)
{
Expand Down Expand Up @@ -370,7 +372,7 @@ public static IEnumerator WaitForClientsConnectedToServer(NetworkManager server,
/// <param name="result">The result</param>
/// <param name="failIfNull">Whether or not to fail if no object is found and result is null</param>
/// <param name="maxFrames">The max frames to wait for</param>
public static IEnumerator GetNetworkObjectByRepresentation(ulong networkObjectId, NetworkManager representation, CoroutineResultWrapper<NetworkObject> result, bool failIfNull = true, int maxFrames = 64)
public static IEnumerator GetNetworkObjectByRepresentation(ulong networkObjectId, NetworkManager representation, CoroutineResultWrapper<NetworkObject> result, bool failIfNull = true, int maxFrames = DefaultMaxFrames)
{
if (result == null)
{
Expand Down Expand Up @@ -401,7 +403,7 @@ public static IEnumerator GetNetworkObjectByRepresentation(ulong networkObjectId
/// <param name="result">The result</param>
/// <param name="failIfNull">Whether or not to fail if no object is found and result is null</param>
/// <param name="maxFrames">The max frames to wait for</param>
public static IEnumerator GetNetworkObjectByRepresentation(Func<NetworkObject, bool> predicate, NetworkManager representation, CoroutineResultWrapper<NetworkObject> result, bool failIfNull = true, int maxFrames = 64)
public static IEnumerator GetNetworkObjectByRepresentation(Func<NetworkObject, bool> predicate, NetworkManager representation, CoroutineResultWrapper<NetworkObject> result, bool failIfNull = true, int maxFrames = DefaultMaxFrames)
{
if (result == null)
{
Expand Down Expand Up @@ -435,30 +437,31 @@ public static IEnumerator GetNetworkObjectByRepresentation(Func<NetworkObject, b
/// <param name="workload">Action / code to run</param>
/// <param name="predicate">The predicate to wait for</param>
/// <param name="maxFrames">The max frames to wait for</param>
public static IEnumerator RunAndWaitForCondition(Action workload, Func<bool> predicate, int maxFrames = 64)
public static IEnumerator RunAndWaitForCondition(Action workload, Func<bool> predicate, int maxFrames = DefaultMaxFrames, int minFrames = DefaultMinFrames)
{
var waitResult = new CoroutineResultWrapper<bool>();
workload();

yield return Run(WaitForCondition(
predicate,
waitResult,
maxFrames: maxFrames));
maxFrames: maxFrames,
minFrames: minFrames));

if (!waitResult.Result)
{
throw new Exception();
}
}


/// <summary>
/// Waits for a predicate condition to be met
/// </summary>
/// <param name="predicate">The predicate to wait for</param>
/// <param name="result">The result. If null, it will fail if the predicate is not met</param>
/// <param name="minFrames">The min frames to wait for</param>
/// <param name="maxFrames">The max frames to wait for</param>
public static IEnumerator WaitForCondition(Func<bool> predicate, CoroutineResultWrapper<bool> result = null, int maxFrames = 64)
public static IEnumerator WaitForCondition(Func<bool> predicate, CoroutineResultWrapper<bool> result = null, int maxFrames = DefaultMaxFrames, int minFrames = DefaultMinFrames)
{
if (predicate == null)
{
Expand All @@ -467,12 +470,24 @@ public static IEnumerator WaitForCondition(Func<bool> predicate, CoroutineResult

var startFrameNumber = Time.frameCount;

while (Time.frameCount - startFrameNumber <= maxFrames && !predicate())
if (minFrames > 0)
{
yield return new WaitUntil(() =>
{
return Time.frameCount >= minFrames;
});
}

while (Time.frameCount - startFrameNumber <= maxFrames &&
!predicate())
{
// Changed to 2 frames to avoid the scenario where it would take 1+ frames to
// see a value change (i.e. discovered in the NetworkTransformTests)
var nextFrameNumber = Time.frameCount + 2;
yield return new WaitUntil(() => Time.frameCount >= nextFrameNumber);
yield return new WaitUntil(() =>
{
return Time.frameCount >= nextFrameNumber;
});
}

var res = predicate();
Expand Down