Skip to content

test: connection approval and invalid NetworkPrefabs within NetworkConfig.NetworkPrefabs #825

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
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
70 changes: 70 additions & 0 deletions com.unity.multiplayer.mlapi/Tests/Runtime/ConnectionApproval.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Text;
using System.Collections;
using UnityEngine;
using UnityEngine.TestTools;
using NUnit.Framework;

namespace MLAPI.RuntimeTests
{

public class ConnectionApprovalTests
{

private Guid m_ValidationToken;

private bool m_IsValidated;

[SetUp]
public void Setup()
{
// Create, instantiate, and host
Assert.IsTrue(NetworkManagerHelper.StartNetworkManager(out _,NetworkManagerHelper.NetworkManagerOperatingMode.None));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NoelStephensUnity @TwoTenPvP I like these helpers to simplify unit tests, but I think we may now have 2 sets of helpers. Is this right? Can we merge them into one?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a good idea.

m_ValidationToken = Guid.NewGuid();
}

[UnityTest]
public IEnumerator ConnectionApproval()
{
NetworkManagerHelper.NetworkManagerObject.ConnectionApprovalCallback += NetworkManagerObject_ConnectionApprovalCallback;
NetworkManagerHelper.NetworkManagerObject.NetworkConfig.ConnectionApproval = true;
NetworkManagerHelper.NetworkManagerObject.NetworkConfig.PlayerPrefab = null;
NetworkManagerHelper.NetworkManagerObject.NetworkConfig.ConnectionData = Encoding.UTF8.GetBytes(m_ValidationToken.ToString());
m_IsValidated = false;
NetworkManagerHelper.NetworkManagerObject.StartHost();

var timeOut = Time.realtimeSinceStartup + 3.0f;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume these 3.0 & 0.01 time values have a rationale to them and are compatible with how things operate on Yamato?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep... it was a "wild guess" of what to realistically expect Yamato's read/write times when under a heavy load (worst case scenario)...so far it seems to be working out ok... :)

var timedOut = false;
while(!m_IsValidated)
{
yield return new WaitForSeconds(0.01f);
if(timeOut < Time.realtimeSinceStartup)
{
timedOut = true;
}
}

//Make sure we didn't time out
Assert.False(timedOut);
Assert.True(m_IsValidated);
}

private void NetworkManagerObject_ConnectionApprovalCallback(byte[] connectionData, ulong clientId, NetworkManager.ConnectionApprovedDelegate callback)
{
var stringGuid = Encoding.UTF8.GetString(connectionData);
if(m_ValidationToken.ToString() == stringGuid)
{
m_IsValidated = true;
}
callback(false, null, m_IsValidated, null, null);
}

[TearDown]
public void TearDown()
{
// Stop, shutdown, and destroy
NetworkManagerHelper.ShutdownNetworkManager();
}

}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,44 @@ namespace MLAPI.RuntimeTests
/// </summary>
public class NetworkPrefabHandlerTests
{
/// <summary>
/// Tests the NetwokConfig NetworkPrefabs initialization during NetworkManager's Init method
/// </summary>
[Test]
public void NetworkConfigInvalidNetworkPrefabTest()
{
var testPrefabObjectName = "NetworkPrefabHandlerTestObject";
Guid baseObjectID = NetworkManagerHelper.AddGameNetworkObject(testPrefabObjectName);
NetworkObject baseObject = NetworkManagerHelper.InstantiatedNetworkObjects[baseObjectID];

// Add null entry
NetworkManagerHelper.NetworkManagerObject.NetworkConfig.NetworkPrefabs.Add(null);

// Add a NetworkPrefab with no prefab
NetworkManagerHelper.NetworkManagerObject.NetworkConfig.NetworkPrefabs.Add(new Configuration.NetworkPrefab());

var validNetworkPrefab = new Configuration.NetworkPrefab();
validNetworkPrefab.Prefab = baseObject.gameObject;

//Add a valid prefab
NetworkManagerHelper.NetworkManagerObject.NetworkConfig.NetworkPrefabs.Add(validNetworkPrefab);
var exceptionOccurred = false;
try
{
NetworkManagerHelper.NetworkManagerObject.StartHost();
}
catch
{
exceptionOccurred = true;
}

Assert.False(exceptionOccurred);
}


[Test]
public void NetworkPrefabHandlerClass()
{
//Only used to create a network object based game asset
Assert.IsTrue(NetworkManagerHelper.StartNetworkManager(out _));
var testPrefabObjectName = "NetworkPrefabHandlerTestObject";

Expand Down Expand Up @@ -110,7 +144,7 @@ public void NetworkPrefabHandlerClass()
public void Setup()
{
//Create, instantiate, and host
NetworkManagerHelper.StartNetworkManager(out _);
NetworkManagerHelper.StartNetworkManager(out _,NetworkManagerHelper.NetworkManagerOperatingMode.None);
}

[TearDown]
Expand Down