Skip to content
Merged
Show file tree
Hide file tree
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 @@ -6,18 +6,14 @@
using NUnit.Framework;
using UnityEngine;
using UnityEngine.SceneManagement;
using Object = UnityEngine.Object;

namespace MLAPI.RuntimeTests
{
/// <summary>
/// Provides helpers for running multi instance tests.
/// </summary>
public static class MultiInstanceHelpers
internal 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 @@ -32,10 +28,11 @@ 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 the NetworkConfig
// Set config
clients[i].NetworkConfig = new NetworkConfig()
{
// Set the current scene to prevent unexpected log messages which would trigger a failure
Expand All @@ -45,17 +42,14 @@ 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 the NetworkConfig
// Set config
server.NetworkConfig = new NetworkConfig()
{
// Set the current scene to prevent unexpected log messages which would trigger a failure
Expand All @@ -68,25 +62,6 @@ public static bool Create(int clientCount, out NetworkManager server, out Networ
return true;
}


public static void ShutdownAndClean()
{
// 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 @@ -195,52 +170,6 @@ 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 @@ -270,40 +199,7 @@ public static IEnumerator WaitForClientConnectedToServer(NetworkManager server,
}
else
{
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");
Assert.True(res, "Client never connected to server");
}
}

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

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

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

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

// Called by server
Expand Down Expand Up @@ -126,12 +115,6 @@ 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: 0 additions & 8 deletions testproject/Assets/Tests/Manual/HybridScripts.meta

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ GameObject:
- component: {fileID: 1207168974}
- component: {fileID: 1207168973}
m_Layer: 5
m_Name: RpcTestsCanvas
m_Name: ClientRpcTestsCanvas
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
Expand Down Expand Up @@ -875,12 +875,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 47f41f8e19d061b49866be44887f53ee, type: 3}
m_Name:
m_EditorClassIdentifier:
m_RunInTestMode: 1
m_IterationsToRun: 5
m_CounterTextObject: {fileID: 1859881229}
m_ClientProgressBar: {fileID: 521221618}
m_ConnectionModeButtonParent: {fileID: 1705962118}
m_ManualTestNetworkManager: {fileID: 0}
--- !u!1 &1544159781
GameObject:
m_ObjectHideFlags: 0
Expand Down
Loading