Skip to content

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

Merged
merged 21 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
ea082af
test: converting RpcTestsAutomated
NoelStephensUnity May 13, 2021
b785c56
style
NoelStephensUnity May 13, 2021
c9f5400
Merge branch 'develop' into test/manualRpcTests-to-Automated
NoelStephensUnity May 13, 2021
7d70eaf
refactor
NoelStephensUnity May 13, 2021
26b26b1
fix
NoelStephensUnity May 13, 2021
2ae5e44
refactor
NoelStephensUnity May 13, 2021
838c881
Merge branch 'develop' into test/manualRpcTests-to-Automated
0xFA11 May 13, 2021
85983c3
style
NoelStephensUnity May 13, 2021
3ec4074
Merge branch 'test/manualRpcTests-to-Automated' of https://github.com…
NoelStephensUnity May 13, 2021
b369dff
refactor
NoelStephensUnity May 14, 2021
67e8aea
refactor
NoelStephensUnity May 14, 2021
fe37e7a
Update com.unity.multiplayer.mlapi/Tests/Runtime/Transport/SIPTranspo…
0xFA11 May 14, 2021
718ab4d
Update com.unity.multiplayer.mlapi/Tests/Runtime/Transport/SIPTranspo…
0xFA11 May 14, 2021
d2d8007
Update com.unity.multiplayer.mlapi/Tests/Runtime/Transport/SIPTranspo…
0xFA11 May 14, 2021
6f58c32
Update com.unity.multiplayer.mlapi/Tests/Runtime/Transport/SIPTranspo…
NoelStephensUnity May 14, 2021
f58e4d5
Update com.unity.multiplayer.mlapi/Tests/Runtime/Transport/SIPTranspo…
NoelStephensUnity May 14, 2021
46f705a
Update testproject/Assets/Tests/Runtime/RpcTestsAutomated.cs
NoelStephensUnity May 14, 2021
bc3d490
Update com.unity.multiplayer.mlapi/Tests/Runtime/Transport/SIPTranspo…
NoelStephensUnity May 14, 2021
9d848da
Update testproject/Assets/Tests/Runtime/RpcTestsAutomated.cs
NoelStephensUnity May 14, 2021
c356a4f
refactor
NoelStephensUnity May 14, 2021
1238111
refactor
NoelStephensUnity May 14, 2021
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 @@ -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>
Expand All @@ -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
Expand All @@ -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
Expand All @@ -62,6 +68,25 @@ public static bool Create(int clientCount, out NetworkManager server, out Networ
return true;
}


public static void ShutdownAndClean()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: I'd name this as Destroy to keep it similar to Create/Start formats.

Suggested change
public static void ShutdownAndClean()
public static void Destroy()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't have a preference to what we name this...
@TwoTenPvP What do you want this to be named?

{
// 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>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,35 @@ public override void DisconnectLocalClient()
{
Type = NetworkEvent.Disconnect,
Channel = NetworkChannel.Internal,
ConnectionId = m_LocalConnection.ConnectionId,
ConnectionId = m_LocalConnection != null ? m_LocalConnection.ConnectionId : ServerClientId,
Data = new ArraySegment<byte>()
});

// Inject local disconnect
m_LocalConnection.IncomingBuffer.Enqueue(new Event
if (m_LocalConnection != null)
{
Type = NetworkEvent.Disconnect,
Channel = NetworkChannel.Internal,
ConnectionId = m_LocalConnection.ConnectionId,
Data = new ArraySegment<byte>()
});
// Inject local disconnect
m_LocalConnection.IncomingBuffer.Enqueue(new Event
{
Type = NetworkEvent.Disconnect,
Channel = NetworkChannel.Internal,
ConnectionId = m_LocalConnection.ConnectionId,
Data = new ArraySegment<byte>()
});

if (s_Server != null && m_LocalConnection != null)
{
// Remove the connection
s_Server.Transport.m_Clients.Remove(m_LocalConnection.ConnectionId);
}

// Remove the connection
s_Server.Transport.m_Clients.Remove(m_LocalConnection.ConnectionId);
if (m_LocalConnection.ConnectionId == ServerClientId)
{
s_Server = null;
}

// Remove the local connection
m_LocalConnection = null;
// Remove the local connection
m_LocalConnection = null;
}
}

// Called by server
Expand Down Expand Up @@ -115,6 +126,12 @@ public override void Shutdown()
Data = new ArraySegment<byte>()
});
}

if (m_LocalConnection != null && m_LocalConnection.ConnectionId == ServerClientId)
{
s_Server = null;
}


// TODO: Cleanup
}
Expand Down
8 changes: 8 additions & 0 deletions testproject/Assets/Tests/Manual/HybridScripts.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading