From b5a4988331a51a5801783dbf29ce8a86f466653e Mon Sep 17 00:00:00 2001 From: vis2k Date: Wed, 22 Jan 2020 11:55:56 +0100 Subject: [PATCH] FallbackTransportTest from paul --- Assets/Mirror/Tests/FallbackTransportTest.cs | 170 ++++++++++++++++++ .../Tests/FallbackTransportTest.cs.meta | 11 ++ 2 files changed, 181 insertions(+) create mode 100644 Assets/Mirror/Tests/FallbackTransportTest.cs create mode 100644 Assets/Mirror/Tests/FallbackTransportTest.cs.meta diff --git a/Assets/Mirror/Tests/FallbackTransportTest.cs b/Assets/Mirror/Tests/FallbackTransportTest.cs new file mode 100644 index 000000000..68a26a398 --- /dev/null +++ b/Assets/Mirror/Tests/FallbackTransportTest.cs @@ -0,0 +1,170 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using NSubstitute; +using NUnit.Framework; +using UnityEngine; +using UnityEngine.Events; + +namespace Mirror.Tests +{ + public class FallbackTransportTest + { + + Transport transport1; + Transport transport2; + FallbackTransport transport; + + [SetUp] + public void SetupMultipex() + { + transport1 = Substitute.For(); + transport2 = Substitute.For(); + + var gameObject = new GameObject(); + + transport = gameObject.AddComponent(); + transport.transports = new[] { transport1, transport2 }; + } + + #region Client tests + // A Test behaves as an ordinary method + [Test] + public void TestAvailable() + { + transport1.Available().Returns(true); + transport2.Available().Returns(false); + transport.Awake(); + + Assert.That(transport.Available()); + } + + // A Test behaves as an ordinary method + [Test] + public void TestNotAvailable() + { + transport1.Available().Returns(false); + transport2.Available().Returns(false); + + Assert.Throws(() => transport.Awake()); + + } + + // A Test behaves as an ordinary method + [Test] + public void TestConnect() + { + transport1.Available().Returns(false); + transport2.Available().Returns(true); + transport.Awake(); + + transport.ClientConnect("some.server.com"); + + transport1.DidNotReceive().ClientConnect(Arg.Any()); + transport2.Received().ClientConnect("some.server.com"); + } + + [Test] + public void TestConnected() + { + transport1.Available().Returns(true); + transport.Awake(); + transport.ClientConnect("some.server.com"); + + transport1.ClientConnected().Returns(true); + + Assert.That(transport.ClientConnected()); + } + + [Test] + public void TestDisconnect() + { + transport1.Available().Returns(true); + transport.Awake(); + transport.ClientConnect("some.server.com"); + + transport.ClientDisconnect(); + + transport1.Received().ClientDisconnect(); + } + + [Test] + public void TestClientSend() + { + transport1.Available().Returns(true); + transport.Awake(); + + transport.ClientConnect("some.server.com"); + + byte[] data = { 1, 2, 3 }; + var segment = new ArraySegment(data); + + transport.ClientSend(3, segment); + + transport1.Received().ClientSend(3, segment); + } + + [Test] + public void TestClient1Connected() + { + transport1.Available().Returns(true); + transport2.Available().Returns(true); + + UnityAction callback = Substitute.For(); + transport.Awake(); + transport.OnClientConnected.AddListener(callback); + transport1.OnClientConnected.Invoke(); + callback.Received().Invoke(); + } + + [Test] + public void TestClient2Connected() + { + transport1.Available().Returns(true); + transport2.Available().Returns(true); + + UnityAction callback = Substitute.For(); + transport.Awake(); + transport.OnClientConnected.AddListener(callback); + transport2.OnClientConnected.Invoke(); + callback.Received().Invoke(); + } + + #endregion + + #region Server tests + + [Test] + public void TestServerConnected() + { + byte[] data = { 1, 2, 3 }; + var segment = new ArraySegment(data); + + transport1.Available().Returns(true); + transport2.Available().Returns(true); + transport.Awake(); + + // on connect, send a message back + void SendMessage(int connectionId) + { + var connectionIds = new List(new[] { connectionId }); + transport.ServerSend(connectionIds, 5, segment); + } + + transport.OnServerConnected.AddListener(SendMessage); + + transport1.OnServerConnected.Invoke(1); + + int[] expectedIds = { 1 }; + + transport1.Received().ServerSend( + Arg.Is>(x => x.SequenceEqual(expectedIds)), + 5, + segment); + } + + + #endregion + + } +} diff --git a/Assets/Mirror/Tests/FallbackTransportTest.cs.meta b/Assets/Mirror/Tests/FallbackTransportTest.cs.meta new file mode 100644 index 000000000..cf293731f --- /dev/null +++ b/Assets/Mirror/Tests/FallbackTransportTest.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ea6c7ecc5045248278e238f5d15a8416 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: