fix: Fallback and Multiplex now disable their transports when they are disabled (#2048)

* test to check if fallbacks disables other transport

* using ondisable to disable other transport

* fixing teardown

* adding test for Multiplex

* using ondisable to disable other transport

* fixing NSubstitute for 2019
This commit is contained in:
James Frowen 2020-06-28 23:49:36 +01:00 committed by GitHub
parent ab1b92f74b
commit 61d44b2d80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 148 additions and 2 deletions

View File

@ -27,6 +27,16 @@ public void Awake()
Debug.Log("FallbackTransport available: " + available.GetType()); Debug.Log("FallbackTransport available: " + available.GetType());
} }
void OnEnable()
{
available.enabled = true;
}
void OnDisable()
{
available.enabled = false;
}
// The client just uses the first transport available // The client just uses the first transport available
Transport GetAvailableTransport() Transport GetAvailableTransport()
{ {

View File

@ -26,6 +26,22 @@ public void Awake()
InitServer(); InitServer();
} }
void OnEnable()
{
foreach (Transport transport in transports)
{
transport.enabled = true;
}
}
void OnDisable()
{
foreach (Transport transport in transports)
{
transport.enabled = false;
}
}
public override bool Available() public override bool Available()
{ {
// available if any of the transports is available // available if any of the transports is available

View File

@ -0,0 +1,105 @@
using NSubstitute;
using NUnit.Framework;
using UnityEngine;
namespace Mirror.Tests.Runtime.TransportEnableTest
{
public class FallbackTransportEnableTest
{
Transport transport1;
MemoryTransport transport2;
FallbackTransport transport;
[SetUp]
public void Setup()
{
GameObject gameObject = new GameObject();
// set inactive so that awake isnt called
gameObject.SetActive(false);
transport1 = Substitute.For<Transport>();
transport2 = gameObject.AddComponent<MemoryTransport>();
transport = gameObject.AddComponent<FallbackTransport>();
transport.transports = new[] { transport1, transport2 };
gameObject.SetActive(true);
}
[TearDown]
public void TearDown()
{
GameObject.DestroyImmediate(transport.gameObject);
}
[Test]
public void DisableShouldDisableAvailableTransport()
{
// make transport2 the active transport
transport1.Available().Returns(false);
transport.Awake();
// starts enabled
Assert.That(transport2.enabled, Is.True);
// disabling FallbackTransport
transport.enabled = false;
Assert.That(transport2.enabled, Is.False);
// enabling FallbackTransport
transport.enabled = true;
Assert.That(transport2.enabled, Is.True);
}
}
public class MultiplexTransportEnableTest
{
MemoryTransport transport1;
MemoryTransport transport2;
MultiplexTransport transport;
[SetUp]
public void Setup()
{
GameObject gameObject = new GameObject();
// set inactive so that awake isnt called
gameObject.SetActive(false);
transport1 = gameObject.AddComponent<MemoryTransport>();
transport2 = gameObject.AddComponent<MemoryTransport>();
transport = gameObject.AddComponent<MultiplexTransport>();
transport.transports = new[] { transport1, transport2 };
gameObject.SetActive(true);
}
[TearDown]
public void TearDown()
{
GameObject.DestroyImmediate(transport.gameObject);
}
[Test]
public void DisableShouldDisableAllTransports()
{
transport.Awake();
// starts enabled
Assert.That(transport1.enabled, Is.True);
Assert.That(transport2.enabled, Is.True);
// disabling MultiplexTransport
transport.enabled = false;
Assert.That(transport1.enabled, Is.False);
Assert.That(transport2.enabled, Is.False);
// enabling MultiplexTransport
transport.enabled = true;
Assert.That(transport1.enabled, Is.True);
Assert.That(transport2.enabled, Is.True);
}
}
}

View File

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

View File

@ -11,8 +11,12 @@
"includePlatforms": [], "includePlatforms": [],
"excludePlatforms": [], "excludePlatforms": [],
"allowUnsafeCode": false, "allowUnsafeCode": false,
"overrideReferences": false, "overrideReferences": true,
"precompiledReferences": [], "precompiledReferences": [
"NSubstitute.dll",
"Castle.Core.dll",
"System.Threading.Tasks.Extensions.dll"
],
"autoReferenced": true, "autoReferenced": true,
"defineConstraints": [] "defineConstraints": []
} }