Skip to content

Commit

Permalink
fix: Fallback and Multiplex now disable their transports when they ar…
Browse files Browse the repository at this point in the history
…e 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
  • Loading branch information
James-Frowen committed Jun 28, 2020
1 parent ab1b92f commit 61d44b2
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Assets/Mirror/Runtime/Transport/FallbackTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public void Awake()
Debug.Log("FallbackTransport available: " + available.GetType());
}

void OnEnable()
{
available.enabled = true;
}

void OnDisable()
{
available.enabled = false;
}

// The client just uses the first transport available
Transport GetAvailableTransport()
{
Expand Down
16 changes: 16 additions & 0 deletions Assets/Mirror/Runtime/Transport/MultiplexTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ public void Awake()
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()
{
// available if any of the transports is available
Expand Down
105 changes: 105 additions & 0 deletions Assets/Mirror/Tests/Runtime/FallbackTransportEnableTest.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}

}
11 changes: 11 additions & 0 deletions Assets/Mirror/Tests/Runtime/FallbackTransportEnableTest.cs.meta

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

8 changes: 6 additions & 2 deletions Assets/Mirror/Tests/Runtime/Mirror.Tests.Runtime.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"overrideReferences": true,
"precompiledReferences": [
"NSubstitute.dll",
"Castle.Core.dll",
"System.Threading.Tasks.Extensions.dll"
],
"autoReferenced": true,
"defineConstraints": []
}

0 comments on commit 61d44b2

Please sign in to comment.