Skip to content

Commit 8bcf2d6

Browse files
fix: fixing connection management test [MTT-4492][MTT-6242] (#826)
* Adding OnServerShutdown event to ConnectionManager and ConnectionStates * Fixing failing test
1 parent 32f4732 commit 8bcf2d6

File tree

8 files changed

+67
-34
lines changed

8 files changed

+67
-34
lines changed

Assets/Scripts/ConnectionManagement/ConnectionManager.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ void Start()
9898
NetworkManager.OnServerStarted += OnServerStarted;
9999
NetworkManager.ConnectionApprovalCallback += ApprovalCheck;
100100
NetworkManager.OnTransportFailure += OnTransportFailure;
101+
NetworkManager.OnServerStopped += OnServerStopped;
101102
}
102103

103104
void OnDestroy()
@@ -107,6 +108,7 @@ void OnDestroy()
107108
NetworkManager.OnServerStarted -= OnServerStarted;
108109
NetworkManager.ConnectionApprovalCallback -= ApprovalCheck;
109110
NetworkManager.OnTransportFailure -= OnTransportFailure;
111+
NetworkManager.OnServerStopped -= OnServerStopped;
110112
}
111113

112114
internal void ChangeState(ConnectionState nextState)
@@ -146,6 +148,11 @@ void OnTransportFailure()
146148
m_CurrentState.OnTransportFailure();
147149
}
148150

151+
void OnServerStopped(bool _) // we don't need this parameter as the ConnectionState already carries the relevant information
152+
{
153+
m_CurrentState.OnServerStopped();
154+
}
155+
149156
public void StartClientLobby(string playerName)
150157
{
151158
m_CurrentState.StartClientLobby(playerName);

Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ public override void OnClientConnected(ulong _)
3636
public override void OnClientDisconnect(ulong _)
3737
{
3838
// client ID is for sure ours here
39-
StartingClientFailedAsync();
39+
StartingClientFailed();
4040
}
4141

42-
protected void StartingClientFailedAsync()
42+
void StartingClientFailed()
4343
{
4444
var disconnectReason = m_ConnectionManager.NetworkManager.DisconnectReason;
4545
if (string.IsNullOrEmpty(disconnectReason))
@@ -72,7 +72,7 @@ internal async Task ConnectClientAsync()
7272
{
7373
Debug.LogError("Error connecting client, see following exception");
7474
Debug.LogException(e);
75-
StartingClientFailedAsync();
75+
StartingClientFailed();
7676
throw;
7777
}
7878
}

Assets/Scripts/ConnectionManagement/ConnectionState/ClientReconnectingState.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ IEnumerator ReconnectCoroutine()
122122
if (!reconnectingSetupTask.IsFaulted && reconnectingSetupTask.Result.success)
123123
{
124124
// If this fails, the OnClientDisconnect callback will be invoked by Netcode
125-
var connectingToRelay = ConnectClientAsync();
126-
yield return new WaitUntil(() => connectingToRelay.IsCompleted);
125+
var connectingTask = ConnectClientAsync();
126+
yield return new WaitUntil(() => connectingTask.IsCompleted);
127127
}
128128
else
129129
{

Assets/Scripts/ConnectionManagement/ConnectionState/ConnectionState.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,7 @@ public virtual void OnUserRequestedShutdown() { }
3939
public virtual void ApprovalCheck(NetworkManager.ConnectionApprovalRequest request, NetworkManager.ConnectionApprovalResponse response) { }
4040

4141
public virtual void OnTransportFailure() { }
42+
43+
public virtual void OnServerStopped() { }
4244
}
4345
}

Assets/Scripts/ConnectionManagement/ConnectionState/HostingState.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@ public override void OnClientConnected(ulong clientId)
4747

4848
public override void OnClientDisconnect(ulong clientId)
4949
{
50-
if (clientId == m_ConnectionManager.NetworkManager.LocalClientId)
51-
{
52-
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
53-
}
54-
else
50+
if (clientId != m_ConnectionManager.NetworkManager.LocalClientId)
5551
{
5652
var playerId = SessionManager<SessionPlayerData>.Instance.GetPlayerId(clientId);
5753
if (playerId != null)
@@ -80,6 +76,12 @@ public override void OnUserRequestedShutdown()
8076
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
8177
}
8278

79+
public override void OnServerStopped()
80+
{
81+
m_ConnectStatusPublisher.Publish(ConnectStatus.GenericDisconnect);
82+
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
83+
}
84+
8385
/// <summary>
8486
/// This logic plugs into the "ConnectionApprovalResponse" exposed by Netcode.NetworkManager. It is run every time a client connects to us.
8587
/// The complementary logic that runs when the client starts its connection can be found in ClientConnectingState.

Assets/Scripts/ConnectionManagement/ConnectionState/StartingHostState.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,6 @@ public override void Enter()
3333

3434
public override void Exit() { }
3535

36-
public override void OnClientDisconnect(ulong clientId)
37-
{
38-
if (clientId == m_ConnectionManager.NetworkManager.LocalClientId)
39-
{
40-
StartHostFailed();
41-
}
42-
}
43-
44-
void StartHostFailed()
45-
{
46-
m_ConnectStatusPublisher.Publish(ConnectStatus.StartHostFailed);
47-
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
48-
}
49-
5036
public override void OnServerStarted()
5137
{
5238
m_ConnectStatusPublisher.Publish(ConnectStatus.Success);
@@ -72,6 +58,11 @@ public override void ApprovalCheck(NetworkManager.ConnectionApprovalRequest requ
7258
}
7359
}
7460

61+
public override void OnServerStopped()
62+
{
63+
StartHostFailed();
64+
}
65+
7566
async void StartHost()
7667
{
7768
try
@@ -82,7 +73,7 @@ async void StartHost()
8273
// NGO's StartHost launches everything
8374
if (!m_ConnectionManager.NetworkManager.StartHost())
8475
{
85-
OnClientDisconnect(m_ConnectionManager.NetworkManager.LocalClientId);
76+
StartHostFailed();
8677
}
8778
}
8879
catch (Exception)
@@ -91,5 +82,11 @@ async void StartHost()
9182
throw;
9283
}
9384
}
85+
86+
void StartHostFailed()
87+
{
88+
m_ConnectStatusPublisher.Publish(ConnectStatus.StartHostFailed);
89+
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
90+
}
9491
}
9592
}

Assets/Tests/Runtime/ConnectionManagementTests.cs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections;
3+
using System.Collections.Generic;
34
using NUnit.Framework;
45
using Unity.BossRoom.ConnectionManagement;
56
using Unity.BossRoom.Infrastructure;
@@ -355,8 +356,7 @@ public IEnumerator UnexpectedClientDisconnect_ClientReconnectingSuccessfully()
355356
subscriptions.Dispose();
356357
}
357358

358-
359-
[UnityTest, Ignore("Test fails because shutdowns do not invoke OnClientDisconnect on the host, so the ConnectionManager doesn't properly transition to the Offline state")]
359+
[UnityTest]
360360
public IEnumerator UnexpectedServerShutdown_ClientsFailToReconnect()
361361
{
362362
StartHost();
@@ -368,6 +368,7 @@ public IEnumerator UnexpectedServerShutdown_ClientsFailToReconnect()
368368
AssertAllClientsAreConnected();
369369

370370
var nbReconnectingMsgsReceived = 0;
371+
var nbGenericDisconnectMsgReceived = 0;
371372
var subscriptions = new DisposableGroup();
372373

373374
for (int i = 0; i < NumberOfClients; i++)
@@ -377,8 +378,18 @@ public IEnumerator UnexpectedServerShutdown_ClientsFailToReconnect()
377378
// ignoring the first success message that is in the buffer
378379
if (message != ConnectStatus.Success)
379380
{
380-
Assert.AreEqual(ConnectStatus.Reconnecting, message, "Received unexpected ConnectStatus message.");
381-
nbReconnectingMsgsReceived++;
381+
var possibleMessages = new List<ConnectStatus>();
382+
possibleMessages.Add(ConnectStatus.Reconnecting);
383+
possibleMessages.Add(ConnectStatus.GenericDisconnect);
384+
Assert.Contains(message, possibleMessages, "Received unexpected ConnectStatus message.");
385+
if (message == ConnectStatus.Reconnecting)
386+
{
387+
nbReconnectingMsgsReceived++;
388+
}
389+
else if (message == ConnectStatus.GenericDisconnect)
390+
{
391+
nbGenericDisconnectMsgReceived++;
392+
}
382393
}
383394
}));
384395
}
@@ -396,22 +407,32 @@ public IEnumerator UnexpectedServerShutdown_ClientsFailToReconnect()
396407
Assert.IsFalse(m_ClientNetworkManagers[clientId].IsConnectedClient, $"Client{clientId} has not shut down properly after losing connection.");
397408
}
398409

399-
// Waiting for clients to fail to automatically reconnect
410+
var maxNbReconnectionAttempts = 0;
411+
400412
for (var i = 0; i < NumberOfClients; i++)
401413
{
402-
for (var j = 0; j < m_ClientConnectionManagers[i].NbReconnectAttempts; j++)
414+
var nbReconnectionAttempts = m_ClientConnectionManagers[i].NbReconnectAttempts;
415+
maxNbReconnectionAttempts = Math.Max(maxNbReconnectionAttempts, nbReconnectionAttempts);
416+
for (var j = 0; j < nbReconnectionAttempts; j++)
403417
{
404418
// Expecting this error for each reconnection attempt for each client
405419
LogAssert.Expect(LogType.Error, k_FailedToConnectToServerErrorMessage);
406420
}
407421
}
408-
yield return WaitForClientsConnectedOrTimeOut();
409-
for (var i = 0; i < NumberOfClients; i++)
422+
423+
// Waiting for clients to fail to automatically reconnect. We wait once for each reconnection attempt.
424+
for (var i = 0; i < maxNbReconnectionAttempts; i++)
410425
{
411-
Assert.IsFalse(m_ClientNetworkManagers[i].IsConnectedClient, $"Client{i} is connected while no server is running.");
426+
yield return WaitForClientsConnectedOrTimeOut();
427+
for (var j = 0; j < NumberOfClients; j++)
428+
{
429+
Assert.IsFalse(m_ClientNetworkManagers[j].IsConnectedClient, $"Client{j} is connected while no server is running.");
430+
}
431+
412432
}
413433

414434
Assert.AreEqual(NumberOfClients, nbReconnectingMsgsReceived, "Not all clients received a Reconnecting message.");
435+
Assert.AreEqual(NumberOfClients, nbGenericDisconnectMsgReceived, "Not all clients received a GenericDisconnect message.");
415436
subscriptions.Dispose();
416437
}
417438

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Additional documentation and release notes are available at [Multiplayer Documen
88

99
## [unreleased] - yyyy-mm-dd
1010

11+
### Added
12+
* Added OnServerStopped event to ConnectionManager and ConnectionState (#826). This allows for the detection of an unexpected shutdown on the server side.
13+
1114
### Changed
1215
* Replaced our polling for lobby updates with a subscription to the new Websocket based LobbyEvents (#805). This saves up a significant amount of bandwidth usage to and from the service, since updates are infrequent in this game. Now clients and hosts only use up bandwidth on the Lobby service when it is needed. With polling, we used to send a GET request per client once every 2s. The responses were between ~550 bytes and 900 bytes, so if we suppose an average of 725 bytes and 100 000 concurrent users (CCU), this amounted to around 725B * 30 calls per minute * 100 000 CCU = 2.175 GB per minute. Scaling this to a month would get us 93.96 TB per month. In our case, since the only changes to the lobbies happen when a user connects or disconnects, most of that data was not necessary and can be saved to reduce bandwidth usage. Since the cost of using the Lobby service depends on bandwidth usage, this would also save money on an actual game.
1316
* Simplified reconnection flow by offloading responsibility to ConnectionMethod (#804). Now the ClientReconnectingState uses the ConnectionMethod it is configured with to handle setting up reconnection (i.e. reconnecting to the Lobby before trying to reconnect to the Relay server if it is using Relay and Lobby). It can now also fail early and stop retrying if the lobby doesn't exist anymore.
@@ -27,6 +30,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
2730
* ClientConnectedState now inherits from OnlineState instead of the base ConnectionState (#801)
2831
* UpdateRunner now sends the right value for deltaTime when updating its subscribers (#805)
2932
* Inputs are better sanitized when entering IP address and port (#821). Now all invalid characters are prevented, and UnityTransport's NetworkEndpoint.TryParse is used to verify the validity of the IP address and port that are entered before making the join/host button interactable.
33+
* Fixed failing connection management test (#826). This test had to be ignored previously because there was no mechanism to detect unexpected server shutdowns. With the OnServerStopped callback introduced in NGO 1.4.0, this is no longer an issue.
3034
* Decoupled SceneLoaderWrapper and ConnectionStates (#830). The OnServerStarted and OnClientStarted callbacks available in NGO 1.4.0 allows us to remove the need for an external method to initialize the SceneLoaderWrapper after starting a NetworkingSession.
3135

3236
## [2.0.4] - 2022-12-13

0 commit comments

Comments
 (0)