-
Notifications
You must be signed in to change notification settings - Fork 450
fix: Adding exception for silent failure for clients getting other player's object #844
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
SamuelBellomo
merged 9 commits into
develop
from
feature/adding-exception-for-client-side-player-object-get
May 21, 2021
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ad8ae40
Adding proper exception for invalid case. This is so users don't have…
SamuelBellomo e2fd839
Adding tests for that exception
SamuelBellomo d783a4e
adding more tests
SamuelBellomo b3c155b
Merge branch 'develop' into feature/adding-exception-for-client-side-…
SamuelBellomo 3566ea0
fixing a few issues when connecting and disconnecting additional clients
SamuelBellomo e2b17b1
some cleanup
SamuelBellomo 7561c34
adding null check and spacing fix
SamuelBellomo 6343644
Merge branch 'develop' into feature/adding-exception-for-client-side-…
SamuelBellomo 0ebc1e6
Merge branch 'develop' into feature/adding-exception-for-client-side-…
0xFA11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
201 changes: 201 additions & 0 deletions
201
com.unity.multiplayer.mlapi/Tests/Runtime/NetworkSpawnManagerTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
using System; | ||
using System.Collections; | ||
using MLAPI.Exceptions; | ||
using NUnit.Framework; | ||
using UnityEngine; | ||
using UnityEngine.TestTools; | ||
using Object = System.Object; | ||
|
||
namespace MLAPI.RuntimeTests | ||
{ | ||
public class NetworkSpawnManagerTests | ||
{ | ||
private NetworkManager m_ServerNetworkManager; | ||
private NetworkManager[] m_ClientNetworkManagers; | ||
private GameObject m_PlayerPrefab; | ||
private int m_OriginalTargetFrameRate; | ||
|
||
private ulong serverSideClientId => m_ServerNetworkManager.ServerClientId; | ||
private ulong clientSideClientId => m_ClientNetworkManagers[0].LocalClientId; | ||
private ulong otherClientSideClientId => m_ClientNetworkManagers[1].LocalClientId; | ||
|
||
[UnitySetUp] | ||
public IEnumerator Setup() | ||
{ | ||
// Just always track the current target frame rate (will be re-applied upon TearDown) | ||
m_OriginalTargetFrameRate = Application.targetFrameRate; | ||
|
||
// Since we use frame count as a metric, we need to assure it runs at a "common update rate" | ||
// between platforms (i.e. Ubuntu seems to run at much higher FPS when set to -1) | ||
if (Application.targetFrameRate < 0 || Application.targetFrameRate > 120) | ||
{ | ||
Application.targetFrameRate = 120; | ||
} | ||
|
||
// Create multiple NetworkManager instances | ||
if (!MultiInstanceHelpers.Create(2, out NetworkManager server, out NetworkManager[] clients)) | ||
{ | ||
Debug.LogError("Failed to create instances"); | ||
Assert.Fail("Failed to create instances"); | ||
} | ||
|
||
m_ServerNetworkManager = server; | ||
m_ClientNetworkManagers = clients; | ||
|
||
// Create playerPrefab | ||
m_PlayerPrefab = new GameObject("Player"); | ||
NetworkObject networkObject = m_PlayerPrefab.AddComponent<NetworkObject>(); | ||
|
||
// Make it a prefab | ||
MultiInstanceHelpers.MakeNetworkedObjectTestPrefab(networkObject); | ||
|
||
// Set the player prefab | ||
server.NetworkConfig.PlayerPrefab = m_PlayerPrefab; | ||
|
||
for (int i = 0; i < clients.Length; i++) | ||
{ | ||
clients[i].NetworkConfig.PlayerPrefab = m_PlayerPrefab; | ||
} | ||
|
||
// Start the instances | ||
if (!MultiInstanceHelpers.Start(true, server, clients)) | ||
{ | ||
Debug.LogError("Failed to start instances"); | ||
Assert.Fail("Failed to start instances"); | ||
} | ||
|
||
// Wait for connection on client side | ||
for (int i = 0; i < clients.Length; i++) | ||
{ | ||
yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForClientConnected(clients[i])); | ||
} | ||
|
||
// Wait for connection on server side | ||
yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForClientsConnectedToServer(server, clientCount: 3)); | ||
} | ||
|
||
[Test] | ||
public void TestServerCanAccessItsOwnPlayer() | ||
{ | ||
// server can access its own player | ||
var serverSideServerPlayerObject = m_ServerNetworkManager.SpawnManager.GetPlayerNetworkObject(serverSideClientId); | ||
Assert.NotNull(serverSideServerPlayerObject); | ||
Assert.AreEqual(serverSideClientId, serverSideServerPlayerObject.OwnerClientId); | ||
} | ||
|
||
[Test] | ||
public void TestServerCanAccessOtherPlayers() | ||
{ | ||
// server can access other players | ||
var serverSideClientPlayerObject = m_ServerNetworkManager.SpawnManager.GetPlayerNetworkObject(clientSideClientId); | ||
Assert.NotNull(serverSideClientPlayerObject); | ||
Assert.AreEqual(clientSideClientId, serverSideClientPlayerObject.OwnerClientId); | ||
|
||
var serverSideOtherClientPlayerObject = m_ServerNetworkManager.SpawnManager.GetPlayerNetworkObject(otherClientSideClientId); | ||
Assert.NotNull(serverSideOtherClientPlayerObject); | ||
Assert.AreEqual(otherClientSideClientId, serverSideOtherClientPlayerObject.OwnerClientId); | ||
} | ||
|
||
[Test] | ||
public void TestClientCantAccessServerPlayer() | ||
{ | ||
// client can't access server player | ||
Assert.Throws<NotServerException>(() => | ||
{ | ||
m_ClientNetworkManagers[0].SpawnManager.GetPlayerNetworkObject(serverSideClientId); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void TestClientCanAccessOwnPlayer() | ||
{ | ||
// client can access own player | ||
var clientSideClientPlayerObject = m_ClientNetworkManagers[0].SpawnManager.GetPlayerNetworkObject(clientSideClientId); | ||
Assert.NotNull(clientSideClientPlayerObject); | ||
Assert.AreEqual(clientSideClientId, clientSideClientPlayerObject.OwnerClientId); | ||
} | ||
|
||
[Test] | ||
public void TestClientCantAccessOtherPlayer() | ||
{ | ||
// client can't access other player | ||
Assert.Throws<NotServerException>(() => | ||
{ | ||
m_ClientNetworkManagers[0].SpawnManager.GetPlayerNetworkObject(otherClientSideClientId); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void TestServerGetsNullValueIfInvalidId() | ||
{ | ||
// server gets null value if invalid id | ||
var nullPlayer = m_ServerNetworkManager.SpawnManager.GetPlayerNetworkObject(9999); | ||
Assert.Null(nullPlayer); | ||
} | ||
|
||
[Test] | ||
public void TestServerCanUseGetLocalPlayerObject() | ||
{ | ||
// test server can use GetLocalPlayerObject | ||
var serverSideServerPlayerObject = m_ServerNetworkManager.SpawnManager.GetLocalPlayerObject(); | ||
Assert.NotNull(serverSideServerPlayerObject); | ||
Assert.AreEqual(serverSideClientId, serverSideServerPlayerObject.OwnerClientId); | ||
} | ||
|
||
[Test] | ||
public void TestClientCanUseGetLocalPlayerObject() | ||
{ | ||
// test client can use GetLocalPlayerObject | ||
var clientSideClientPlayerObject = m_ClientNetworkManagers[0].SpawnManager.GetLocalPlayerObject(); | ||
Assert.NotNull(clientSideClientPlayerObject); | ||
Assert.AreEqual(clientSideClientId, clientSideClientPlayerObject.OwnerClientId); | ||
} | ||
|
||
[UnityTest] | ||
public IEnumerator TestConnectAndDisconnect() | ||
{ | ||
// test when client connects, player object is now available | ||
|
||
// connect new client | ||
if (!MultiInstanceHelpers.CreateNewClients(1, out NetworkManager[] clients)) | ||
{ | ||
Debug.LogError("Failed to create instances"); | ||
Assert.Fail("Failed to create instances"); | ||
} | ||
var newClientNetworkManager = clients[0]; | ||
newClientNetworkManager.NetworkConfig.PlayerPrefab = m_PlayerPrefab; | ||
newClientNetworkManager.StartClient(); | ||
yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForClientConnected(newClientNetworkManager)); | ||
yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForCondition(() => m_ServerNetworkManager.ConnectedClients.ContainsKey(newClientNetworkManager.LocalClientId))); | ||
var newClientLocalClientId = newClientNetworkManager.LocalClientId; | ||
|
||
// test new client can get that itself locally | ||
var newPlayerObject = newClientNetworkManager.SpawnManager.GetLocalPlayerObject(); | ||
Assert.NotNull(newPlayerObject); | ||
Assert.AreEqual(newClientLocalClientId, newPlayerObject.OwnerClientId); | ||
// test server can get that new client locally | ||
var serverSideNewClientPlayer = m_ServerNetworkManager.SpawnManager.GetPlayerNetworkObject(newClientLocalClientId); | ||
Assert.NotNull(serverSideNewClientPlayer); | ||
Assert.AreEqual(newClientLocalClientId, serverSideNewClientPlayer.OwnerClientId); | ||
|
||
// test when client disconnects, player object no longer available. | ||
var nbConnectedClients = m_ServerNetworkManager.ConnectedClients.Count; | ||
MultiInstanceHelpers.StopOneClient(newClientNetworkManager); | ||
yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForCondition(() => m_ServerNetworkManager.ConnectedClients.Count == nbConnectedClients - 1)); | ||
serverSideNewClientPlayer = m_ServerNetworkManager.SpawnManager.GetPlayerNetworkObject(newClientLocalClientId); | ||
Assert.Null(serverSideNewClientPlayer); | ||
} | ||
|
||
[UnityTearDown] | ||
public IEnumerator Teardown() | ||
{ | ||
// Shutdown and clean up both of our NetworkManager instances | ||
MultiInstanceHelpers.Destroy(); | ||
UnityEngine.Object.Destroy(m_PlayerPrefab); | ||
|
||
// Set the application's target frame rate back to its original value | ||
Application.targetFrameRate = m_OriginalTargetFrameRate; | ||
yield return new WaitForSeconds(0); // wait for next frame so everything is destroyed, so following tests can execute from clean environment | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
com.unity.multiplayer.mlapi/Tests/Runtime/NetworkSpawnManagerTests.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unit tests by a chance?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added