Skip to content

Commit d743c4b

Browse files
authored
Merge branch 'develop' into refactor/netbhv-netobj-prop
2 parents 3de5a05 + 31b41ae commit d743c4b

File tree

9 files changed

+583
-140
lines changed

9 files changed

+583
-140
lines changed

com.unity.multiplayer.mlapi/Tests/Runtime/MultiInstance/MultiInstanceHelpers.cs

Lines changed: 109 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
using NUnit.Framework;
77
using UnityEngine;
88
using UnityEngine.SceneManagement;
9+
using Object = UnityEngine.Object;
910

1011
namespace MLAPI.RuntimeTests
1112
{
1213
/// <summary>
1314
/// Provides helpers for running multi instance tests.
1415
/// </summary>
15-
internal static class MultiInstanceHelpers
16+
public static class MultiInstanceHelpers
1617
{
18+
19+
public static List<NetworkManager> NetworkManagerInstances = new List<NetworkManager>();
20+
1721
/// <summary>
1822
/// Creates NetworkingManagers and configures them for use in a multi instance setting.
1923
/// </summary>
@@ -28,11 +32,10 @@ public static bool Create(int clientCount, out NetworkManager server, out Networ
2832
{
2933
// Create gameObject
3034
var go = new GameObject("NetworkManager - Client - " + i);
31-
3235
// Create networkManager component
3336
clients[i] = go.AddComponent<NetworkManager>();
3437

35-
// Set config
38+
// Set the NetworkConfig
3639
clients[i].NetworkConfig = new NetworkConfig()
3740
{
3841
// 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
4245
};
4346
}
4447

48+
NetworkManagerInstances = new List<NetworkManager>(clients);
49+
4550
{
4651
// Create gameObject
4752
var go = new GameObject("NetworkManager - Server");
4853

4954
// Create networkManager component
5055
server = go.AddComponent<NetworkManager>();
56+
NetworkManagerInstances.Insert(0, server);
5157

52-
// Set config
58+
// Set the NetworkConfig
5359
server.NetworkConfig = new NetworkConfig()
5460
{
5561
// 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
6268
return true;
6369
}
6470

71+
72+
public static void ShutdownAndClean()
73+
{
74+
// Shutdown the server which forces clients to disconnect
75+
foreach (var networkManager in NetworkManagerInstances)
76+
{
77+
if (networkManager.IsServer)
78+
{
79+
networkManager.StopHost();
80+
}
81+
}
82+
83+
// Destroy the network manager instances
84+
foreach (var networkManager in NetworkManagerInstances)
85+
{
86+
Object.Destroy(networkManager.gameObject);
87+
}
88+
}
89+
6590
/// <summary>
6691
/// Starts NetworkManager instances created by the Create method.
6792
/// </summary>
@@ -170,6 +195,52 @@ public static IEnumerator WaitForClientConnected(NetworkManager client, Coroutin
170195
}
171196
}
172197

198+
public static IEnumerator WaitForClientsConnected(NetworkManager[] clients, CoroutineResultWrapper<bool> result = null, int maxFrames = 64)
199+
{
200+
// Make sure none are the host client
201+
foreach (var client in clients)
202+
{
203+
if (client.IsServer)
204+
{
205+
throw new InvalidOperationException("Cannot wait for connected as server");
206+
}
207+
}
208+
209+
210+
int startFrame = Time.frameCount;
211+
var allConnected = true;
212+
while (Time.frameCount - startFrame <= maxFrames)
213+
{
214+
allConnected = true;
215+
foreach (var client in clients)
216+
{
217+
if (!client.IsConnectedClient)
218+
{
219+
allConnected = false;
220+
break;
221+
}
222+
}
223+
if (allConnected)
224+
{
225+
break;
226+
}
227+
int nextFrameId = Time.frameCount + 1;
228+
yield return new WaitUntil(() => Time.frameCount >= nextFrameId);
229+
}
230+
231+
if (result != null)
232+
{
233+
result.Result = allConnected;
234+
}
235+
else
236+
{
237+
foreach (var client in clients)
238+
{
239+
Assert.True(client.IsConnectedClient, $"Client {client.LocalClientId} never connected");
240+
}
241+
}
242+
}
243+
173244
/// <summary>
174245
/// Waits on the server side for 1 client to be connected
175246
/// </summary>
@@ -199,7 +270,40 @@ public static IEnumerator WaitForClientConnectedToServer(NetworkManager server,
199270
}
200271
else
201272
{
202-
Assert.True(res, "Client never connected to server");
273+
Assert.True(res, "A Client never connected to server");
274+
}
275+
}
276+
277+
/// <summary>
278+
/// Waits on the server side for 1 client to be connected
279+
/// </summary>
280+
/// <param name="server">The server</param>
281+
/// <param name="result">The result. If null, it will automatically assert</param>
282+
/// <param name="maxFrames">The max frames to wait for</param>
283+
public static IEnumerator WaitForClientsConnectedToServer(NetworkManager server, int clientCount, CoroutineResultWrapper<bool> result = null, int maxFrames = 64)
284+
{
285+
if (!server.IsServer)
286+
{
287+
throw new InvalidOperationException("Cannot wait for connected as client");
288+
}
289+
290+
int startFrame = Time.frameCount;
291+
292+
while (Time.frameCount - startFrame <= maxFrames && server.ConnectedClients.Count != clientCount)
293+
{
294+
int nextFrameId = Time.frameCount + 1;
295+
yield return new WaitUntil(() => Time.frameCount >= nextFrameId);
296+
}
297+
298+
bool res = server.ConnectedClients.Count == clientCount;
299+
300+
if (result != null)
301+
{
302+
result.Result = res;
303+
}
304+
else
305+
{
306+
Assert.True(res, "A client never connected to server");
203307
}
204308
}
205309

com.unity.multiplayer.mlapi/Tests/Runtime/Transport/SIPTransport.cs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,35 @@ public override void DisconnectLocalClient()
4444
{
4545
Type = NetworkEvent.Disconnect,
4646
Channel = NetworkChannel.Internal,
47-
ConnectionId = m_LocalConnection.ConnectionId,
47+
ConnectionId = m_LocalConnection != null ? m_LocalConnection.ConnectionId : ServerClientId,
4848
Data = new ArraySegment<byte>()
4949
});
5050

51-
// Inject local disconnect
52-
m_LocalConnection.IncomingBuffer.Enqueue(new Event
51+
if (m_LocalConnection != null)
5352
{
54-
Type = NetworkEvent.Disconnect,
55-
Channel = NetworkChannel.Internal,
56-
ConnectionId = m_LocalConnection.ConnectionId,
57-
Data = new ArraySegment<byte>()
58-
});
53+
// Inject local disconnect
54+
m_LocalConnection.IncomingBuffer.Enqueue(new Event
55+
{
56+
Type = NetworkEvent.Disconnect,
57+
Channel = NetworkChannel.Internal,
58+
ConnectionId = m_LocalConnection.ConnectionId,
59+
Data = new ArraySegment<byte>()
60+
});
61+
62+
if (s_Server != null && m_LocalConnection != null)
63+
{
64+
// Remove the connection
65+
s_Server.Transport.m_Clients.Remove(m_LocalConnection.ConnectionId);
66+
}
5967

60-
// Remove the connection
61-
s_Server.Transport.m_Clients.Remove(m_LocalConnection.ConnectionId);
68+
if (m_LocalConnection.ConnectionId == ServerClientId)
69+
{
70+
s_Server = null;
71+
}
6272

63-
// Remove the local connection
64-
m_LocalConnection = null;
73+
// Remove the local connection
74+
m_LocalConnection = null;
75+
}
6576
}
6677

6778
// Called by server
@@ -115,6 +126,12 @@ public override void Shutdown()
115126
Data = new ArraySegment<byte>()
116127
});
117128
}
129+
130+
if (m_LocalConnection != null && m_LocalConnection.ConnectionId == ServerClientId)
131+
{
132+
s_Server = null;
133+
}
134+
118135

119136
// TODO: Cleanup
120137
}

testproject/Assets/Tests/Manual/HybridScripts.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)