Removing host setup from networkserver test (#1868)

* Removing host setup

Only set up the minimum in order to test a feature

* moving to new file

* inverting if

* removing extra  line

* fixing test for headless
This commit is contained in:
James Frowen 2020-05-09 21:53:29 +01:00 committed by GitHub
parent b069a6ce27
commit f5d02906c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 117 additions and 27 deletions

View File

@ -6,8 +6,31 @@
namespace Mirror.Tests.Runtime namespace Mirror.Tests.Runtime
{ {
[TestFixture] [TestFixture]
public class NetworkServerRuntimeTest : HostSetup public class NetworkServerRuntimeTest
{ {
[UnitySetUp]
public IEnumerator UnitySetUp()
{
Transport.activeTransport = new GameObject().AddComponent<MemoryTransport>();
// start server and wait 1 frame
NetworkServer.Listen(1);
yield return null;
}
[TearDown]
public void TearDown()
{
if (Transport.activeTransport != null)
{
GameObject.Destroy(Transport.activeTransport.gameObject);
}
if (NetworkServer.active)
{
NetworkServer.Shutdown();
}
}
[UnityTest] [UnityTest]
public IEnumerator DestroyPlayerForConnectionTest() public IEnumerator DestroyPlayerForConnectionTest()
{ {
@ -50,31 +73,5 @@ public IEnumerator RemovePlayerForConnectionTest()
NetworkServer.AddPlayerForConnection(conn, player); NetworkServer.AddPlayerForConnection(conn, player);
Assert.That(conn.identity != null, "identity should not be null"); Assert.That(conn.identity != null, "identity should not be null");
} }
[UnityTest]
public IEnumerator DisconnectTimeoutTest()
{
// Set high ping frequency so no NetworkPingMessage is generated
NetworkTime.PingFrequency = 5f;
// Set a short timeout for this test and enable disconnectInactiveConnections
NetworkServer.disconnectInactiveTimeout = 1f;
NetworkServer.disconnectInactiveConnections = true;
GameObject remotePlayer = new GameObject("RemotePlayer", typeof(NetworkIdentity));
NetworkConnectionToClient remoteConnection = new NetworkConnectionToClient(1);
NetworkServer.OnConnected(remoteConnection);
NetworkServer.AddPlayerForConnection(remoteConnection, remotePlayer);
// There's a host player from HostSetup + remotePlayer
Assert.That(NetworkServer.connections.Count, Is.EqualTo(2));
// wait 2 seconds for remoteConnection to timeout as idle
yield return new WaitForSeconds(2f);
// host client connection should still be alive
Assert.That(NetworkServer.connections.Count, Is.EqualTo(1));
Assert.That(NetworkServer.localConnection, Is.Not.Null);
}
} }
} }

View File

@ -0,0 +1,82 @@
using System.Collections;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
namespace Mirror.Tests.Runtime
{
[TestFixture]
public class NetworkServerWithHostRuntimeTest
{
[UnitySetUp]
public IEnumerator UnitySetUp()
{
Transport.activeTransport = new GameObject().AddComponent<MemoryTransport>();
// start server and wait 1 frame
NetworkServer.Listen(1);
yield return null;
// connection host and wait 1 frame
NetworkClient.ConnectHost();
NetworkClient.ConnectLocalServer();
yield return null;
}
[TearDown]
public void TearDown()
{
NetworkClient.DisconnectLocalServer();
NetworkClient.Disconnect();
NetworkClient.Shutdown();
if (NetworkServer.active)
{
NetworkServer.Shutdown();
}
if (Transport.activeTransport != null)
{
GameObject.Destroy(Transport.activeTransport.gameObject);
}
}
[UnityTest]
public IEnumerator DisconnectTimeoutTest()
{
// Set high ping frequency so no NetworkPingMessage is generated
NetworkTime.PingFrequency = 5f;
// Set a short timeout for this test and enable disconnectInactiveConnections
NetworkServer.disconnectInactiveTimeout = 1f;
NetworkServer.disconnectInactiveConnections = true;
GameObject remotePlayer = new GameObject("RemotePlayer", typeof(NetworkIdentity));
const int remoteConnectionId = 1;
const int localConnectionId = 0;
NetworkConnectionToClient remoteConnection = new NetworkConnectionToClient(remoteConnectionId);
NetworkServer.OnConnected(remoteConnection);
NetworkServer.AddPlayerForConnection(remoteConnection, remotePlayer);
// There's a host player from HostSetup + remotePlayer
Assert.That(NetworkServer.connections.Count, Is.EqualTo(2));
Assert.IsTrue(NetworkServer.connections.ContainsKey(localConnectionId));
Assert.IsTrue(NetworkServer.connections.ContainsKey(remoteConnectionId));
// wait 2 seconds for remoteConnection to timeout as idle
float endTime = Time.time + 2f;
while (Time.time < endTime)
{
yield return null;
NetworkServer.Update();
}
// host client connection should still be alive
Assert.That(NetworkServer.connections.Count, Is.EqualTo(1));
Assert.That(NetworkServer.localConnection, Is.Not.Null);
Assert.IsTrue(NetworkServer.connections.ContainsKey(localConnectionId));
Assert.IsFalse(NetworkServer.connections.ContainsKey(remoteConnectionId));
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b913e4ff0bb966d4982795271eeedeaf
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: