-
Notifications
You must be signed in to change notification settings - Fork 450
test: add utils for multi instance tests #914
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 4 commits into
develop
from
test/adding-utils-for-multi-instance-tests
Jun 29, 2021
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
103 changes: 103 additions & 0 deletions
103
com.unity.multiplayer.mlapi/Tests/Runtime/BaseMultiInstanceTest.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,103 @@ | ||
using System; | ||
using System.Collections; | ||
using NUnit.Framework; | ||
using UnityEngine; | ||
using UnityEngine.TestTools; | ||
using Object = UnityEngine.Object; | ||
|
||
namespace MLAPI.RuntimeTests | ||
{ | ||
public abstract class BaseMultiInstanceTest | ||
{ | ||
protected GameObject m_PlayerPrefab; | ||
protected NetworkManager m_ServerNetworkManager; | ||
protected NetworkManager[] m_ClientNetworkManagers; | ||
|
||
protected abstract int NbClients { get; } | ||
|
||
[UnitySetUp] | ||
public virtual IEnumerator Setup() | ||
{ | ||
yield return StartSomeClientsAndServerWithPlayers(true, NbClients, _ => { }); | ||
} | ||
|
||
[UnityTearDown] | ||
public virtual IEnumerator Teardown() | ||
{ | ||
// Shutdown and clean up both of our NetworkManager instances | ||
try | ||
{ | ||
MultiInstanceHelpers.Destroy(); | ||
} | ||
catch (Exception e) { throw e; } | ||
finally | ||
{ | ||
if (m_PlayerPrefab != null) | ||
{ | ||
Object.Destroy(m_PlayerPrefab); | ||
m_PlayerPrefab = null; | ||
} | ||
} | ||
|
||
// wait for next frame so everything is destroyed, so following tests can execute from clean environment | ||
int nextFrameNumber = Time.frameCount + 1; | ||
yield return new WaitUntil(() => Time.frameCount >= nextFrameNumber); | ||
} | ||
|
||
/// <summary> | ||
/// Utility to spawn some clients and a server and set them up | ||
/// </summary> | ||
/// <param name="nbClients"></param> | ||
/// <param name="updatePlayerPrefab">Update the prefab with whatever is needed before players spawn</param> | ||
/// <returns></returns> | ||
public IEnumerator StartSomeClientsAndServerWithPlayers(bool useHost, int nbClients, Action<GameObject> updatePlayerPrefab) | ||
{ | ||
// Create multiple NetworkManager instances | ||
if (!MultiInstanceHelpers.Create(nbClients, out NetworkManager server, out NetworkManager[] clients)) | ||
{ | ||
Debug.LogError("Failed to create instances"); | ||
Assert.Fail("Failed to create instances"); | ||
} | ||
|
||
m_ClientNetworkManagers = clients; | ||
m_ServerNetworkManager = server; | ||
|
||
// Create playerPrefab | ||
m_PlayerPrefab = new GameObject("Player"); | ||
NetworkObject networkObject = m_PlayerPrefab.AddComponent<NetworkObject>(); | ||
|
||
/* | ||
* Normally we would only allow player prefabs to be set to a prefab. Not runtime created objects. | ||
* In order to prevent having a Resource folder full of a TON of prefabs that we have to maintain, | ||
* MultiInstanceHelper has a helper function that lets you mark a runtime created object to be | ||
* treated as a prefab by the MLAPI. That's how we can get away with creating the player prefab | ||
* at runtime without it being treated as a SceneObject or causing other conflicts with the MLAPI. | ||
*/ | ||
// Make it a prefab | ||
MultiInstanceHelpers.MakeNetworkedObjectTestPrefab(networkObject); | ||
|
||
updatePlayerPrefab(m_PlayerPrefab); // update player prefab with whatever is needed before players are spawned | ||
|
||
// 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(useHost, server, clients)) | ||
{ | ||
Debug.LogError("Failed to start instances"); | ||
Assert.Fail("Failed to start instances"); | ||
} | ||
|
||
// Wait for connection on client side | ||
yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForClientsConnected(clients)); | ||
|
||
// Wait for connection on server side | ||
yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForClientsConnectedToServer(server, clientCount: useHost ? nbClients + 1 : nbClients)); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
com.unity.multiplayer.mlapi/Tests/Runtime/BaseMultiInstanceTest.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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.
Uh oh!
There was an error while loading. Please reload this page.