-
Notifications
You must be signed in to change notification settings - Fork 450
test: converting the manual rpc tests over to an automated unit test #830
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
Changes from all commits
ea082af
b785c56
c9f5400
7d70eaf
26b26b1
2ae5e44
838c881
85983c3
3ec4074
b369dff
67e8aea
fe37e7a
718ab4d
d2d8007
6f58c32
f58e4d5
46f705a
bc3d490
9d848da
c356a4f
1238111
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6,14 +6,18 @@ | |||||
using NUnit.Framework; | ||||||
using UnityEngine; | ||||||
using UnityEngine.SceneManagement; | ||||||
using Object = UnityEngine.Object; | ||||||
|
||||||
namespace MLAPI.RuntimeTests | ||||||
{ | ||||||
/// <summary> | ||||||
/// Provides helpers for running multi instance tests. | ||||||
/// </summary> | ||||||
internal static class MultiInstanceHelpers | ||||||
public static class MultiInstanceHelpers | ||||||
{ | ||||||
|
||||||
public static List<NetworkManager> NetworkManagerInstances = new List<NetworkManager>(); | ||||||
|
||||||
/// <summary> | ||||||
/// Creates NetworkingManagers and configures them for use in a multi instance setting. | ||||||
/// </summary> | ||||||
|
@@ -28,11 +32,10 @@ public static bool Create(int clientCount, out NetworkManager server, out Networ | |||||
{ | ||||||
// Create gameObject | ||||||
var go = new GameObject("NetworkManager - Client - " + i); | ||||||
|
||||||
// Create networkManager component | ||||||
clients[i] = go.AddComponent<NetworkManager>(); | ||||||
|
||||||
// Set config | ||||||
// Set the NetworkConfig | ||||||
clients[i].NetworkConfig = new NetworkConfig() | ||||||
{ | ||||||
// Set the current scene to prevent unexpected log messages which would trigger a failure | ||||||
|
@@ -42,14 +45,17 @@ public static bool Create(int clientCount, out NetworkManager server, out Networ | |||||
}; | ||||||
} | ||||||
|
||||||
NetworkManagerInstances = new List<NetworkManager>(clients); | ||||||
|
||||||
{ | ||||||
// Create gameObject | ||||||
var go = new GameObject("NetworkManager - Server"); | ||||||
|
||||||
// Create networkManager component | ||||||
server = go.AddComponent<NetworkManager>(); | ||||||
NetworkManagerInstances.Insert(0, server); | ||||||
|
||||||
// Set config | ||||||
// Set the NetworkConfig | ||||||
server.NetworkConfig = new NetworkConfig() | ||||||
{ | ||||||
// Set the current scene to prevent unexpected log messages which would trigger a failure | ||||||
|
@@ -62,6 +68,25 @@ public static bool Create(int clientCount, out NetworkManager server, out Networ | |||||
return true; | ||||||
} | ||||||
|
||||||
|
||||||
public static void ShutdownAndClean() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor: I'd name this as
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really don't have a preference to what we name this... |
||||||
{ | ||||||
// Shutdown the server which forces clients to disconnect | ||||||
foreach (var networkManager in NetworkManagerInstances) | ||||||
{ | ||||||
if (networkManager.IsServer) | ||||||
{ | ||||||
networkManager.StopHost(); | ||||||
} | ||||||
} | ||||||
|
||||||
// Destroy the network manager instances | ||||||
foreach (var networkManager in NetworkManagerInstances) | ||||||
{ | ||||||
Object.Destroy(networkManager.gameObject); | ||||||
} | ||||||
} | ||||||
|
||||||
/// <summary> | ||||||
/// Starts NetworkManager instances created by the Create method. | ||||||
/// </summary> | ||||||
|
@@ -170,6 +195,52 @@ public static IEnumerator WaitForClientConnected(NetworkManager client, Coroutin | |||||
} | ||||||
} | ||||||
|
||||||
public static IEnumerator WaitForClientsConnected(NetworkManager[] clients, CoroutineResultWrapper<bool> result = null, int maxFrames = 64) | ||||||
{ | ||||||
// Make sure none are the host client | ||||||
foreach (var client in clients) | ||||||
{ | ||||||
if (client.IsServer) | ||||||
{ | ||||||
throw new InvalidOperationException("Cannot wait for connected as server"); | ||||||
} | ||||||
} | ||||||
|
||||||
|
||||||
int startFrame = Time.frameCount; | ||||||
var allConnected = true; | ||||||
while (Time.frameCount - startFrame <= maxFrames) | ||||||
{ | ||||||
allConnected = true; | ||||||
foreach (var client in clients) | ||||||
{ | ||||||
if (!client.IsConnectedClient) | ||||||
{ | ||||||
allConnected = false; | ||||||
break; | ||||||
} | ||||||
} | ||||||
if (allConnected) | ||||||
{ | ||||||
break; | ||||||
} | ||||||
int nextFrameId = Time.frameCount + 1; | ||||||
yield return new WaitUntil(() => Time.frameCount >= nextFrameId); | ||||||
} | ||||||
|
||||||
if (result != null) | ||||||
{ | ||||||
result.Result = allConnected; | ||||||
} | ||||||
else | ||||||
{ | ||||||
foreach (var client in clients) | ||||||
{ | ||||||
Assert.True(client.IsConnectedClient, $"Client {client.LocalClientId} never connected"); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
/// <summary> | ||||||
/// Waits on the server side for 1 client to be connected | ||||||
/// </summary> | ||||||
|
@@ -199,7 +270,40 @@ public static IEnumerator WaitForClientConnectedToServer(NetworkManager server, | |||||
} | ||||||
else | ||||||
{ | ||||||
Assert.True(res, "Client never connected to server"); | ||||||
Assert.True(res, "A Client never connected to server"); | ||||||
} | ||||||
} | ||||||
|
||||||
/// <summary> | ||||||
/// Waits on the server side for 1 client to be connected | ||||||
/// </summary> | ||||||
/// <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, CoroutineResultWrapper<bool> result = null, int maxFrames = 64) | ||||||
{ | ||||||
if (!server.IsServer) | ||||||
{ | ||||||
throw new InvalidOperationException("Cannot wait for connected as client"); | ||||||
} | ||||||
|
||||||
int startFrame = Time.frameCount; | ||||||
|
||||||
while (Time.frameCount - startFrame <= maxFrames && server.ConnectedClients.Count != clientCount) | ||||||
{ | ||||||
int nextFrameId = Time.frameCount + 1; | ||||||
yield return new WaitUntil(() => Time.frameCount >= nextFrameId); | ||||||
} | ||||||
|
||||||
bool res = server.ConnectedClients.Count == clientCount; | ||||||
|
||||||
if (result != null) | ||||||
{ | ||||||
result.Result = res; | ||||||
} | ||||||
else | ||||||
{ | ||||||
Assert.True(res, "A client never connected to server"); | ||||||
} | ||||||
} | ||||||
|
||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.