Example #1
0
        public IEnumerator UnityTearDown() => UniTask.ToCoroutine(async() =>
        {
            // check active, it might have been stopped by tests
            if (client.Active)
            {
                client.Disconnect();
            }
            if (server.Active)
            {
                server.Stop();
            }

            await AsyncUtil.WaitUntilWithTimeout(() => !client.Active);
            await AsyncUtil.WaitUntilWithTimeout(() => !server.Active);

            Object.DestroyImmediate(playerPrefab);
            Object.DestroyImmediate(serverGo);
            Object.DestroyImmediate(clientGo);
            Object.DestroyImmediate(serverPlayerGO);
            Object.DestroyImmediate(clientPlayerGO);

            TearDownTestObjects();

            ExtraTearDown();
            await ExtraTearDownAsync();

            Console.WriteLine($"[MirageTest] UnityTearDown class:{TestContext.CurrentContext.Test.ClassName} method:{TestContext.CurrentContext.Test.MethodName}");
        });
Example #2
0
        public IEnumerator ShutdownHost() => UniTask.ToCoroutine(async() =>
        {
            // check active, it might have been stopped by tests
            if (client.Active)
            {
                client.Disconnect();
            }
            if (server.Active)
            {
                server.Stop();
            }

            await AsyncUtil.WaitUntilWithTimeout(() => !client.Active);
            await AsyncUtil.WaitUntilWithTimeout(() => !server.Active);

            Object.DestroyImmediate(playerPrefab);
            Object.DestroyImmediate(serverGo);
            Object.DestroyImmediate(clientGo);
            Object.DestroyImmediate(serverPlayerGO);
            Object.DestroyImmediate(clientPlayerGO);

            foreach (GameObject obj in toDestroy)
            {
                if (obj != null)
                {
                    Object.DestroyImmediate(obj);
                }
            }

            ExtraTearDown();
        });
        public IEnumerator ClientSceneMessageInvokeTest() => UniTask.ToCoroutine(async() =>
        {
            int startInvoked = 0;
            int endInvoked   = 0;

            clientSceneManager.ClientChangeScene.AddListener((_, __) => startInvoked++);
            clientSceneManager.ClientSceneChanged.AddListener((_, __) => endInvoked++);
            clientSceneManager.ClientSceneMessage(null, new SceneMessage {
                scenePath = "Assets/Mirror/Tests/Runtime/testScene.unity"
            });

            await AsyncUtil.WaitUntilWithTimeout(() => startInvoked == 1);

            // wait 1/2 a second to see if end invokes itself
            await UniTask.Delay(500);
            Assert.That(startInvoked == 1, "Start should only be called once");
            Assert.That(endInvoked == 0, "Should wait for ready before end is called");

            clientSceneManager.ClientSceneReadyMessage(connectionToServer, new SceneReadyMessage());

            await AsyncUtil.WaitUntilWithTimeout(() => endInvoked == 1);

            Assert.That(clientSceneManager.ActiveScenePath, Is.EqualTo("Assets/Mirror/Tests/Runtime/testScene.unity"));

            Assert.That(startInvoked == 1, "Start should only be called once");
            Assert.That(endInvoked == 1, "End should only be called once");
        });
Example #4
0
        public IEnumerator SetupHost() => UniTask.ToCoroutine(async() =>
        {
            networkManagerGo = new GameObject();
            networkManagerGo.AddComponent <MockTransport>();
            sceneManager        = networkManagerGo.AddComponent <NetworkSceneManager>();
            serverObjectManager = networkManagerGo.AddComponent <ServerObjectManager>();
            clientObjectManager = networkManagerGo.AddComponent <ClientObjectManager>();
            manager             = networkManagerGo.AddComponent <NetworkManager>();
            manager.client      = networkManagerGo.GetComponent <NetworkClient>();
            manager.server      = networkManagerGo.GetComponent <NetworkServer>();
            server = manager.server;
            client = manager.client;
            sceneManager.client        = client;
            sceneManager.server        = server;
            serverObjectManager.server = server;
            serverObjectManager.networkSceneManager = sceneManager;
            clientObjectManager.client = client;
            clientObjectManager.networkSceneManager = sceneManager;

            ExtraSetup();

            // wait for all Start() methods to get invoked
            await UniTask.DelayFrame(1);

            await StartHost();

            playerGO  = new GameObject("playerGO", typeof(Rigidbody));
            identity  = playerGO.AddComponent <NetworkIdentity>();
            component = playerGO.AddComponent <T>();

            serverObjectManager.AddPlayerForConnection(server.LocalConnection, playerGO);

            // wait for client to spawn it
            await AsyncUtil.WaitUntilWithTimeout(() => client.Connection.Identity != null);
        });
Example #5
0
        public IEnumerator ClientDisconnectTest() => UniTask.ToCoroutine(async() =>
        {
            client.Disconnect();

            await AsyncUtil.WaitUntilWithTimeout(() => client._connectState == ConnectState.Disconnected);
            await AsyncUtil.WaitUntilWithTimeout(() => !client.Active);
        });
Example #6
0
        public IEnumerator SpawnObjectsFalseTest() => UniTask.ToCoroutine(async() =>
        {
            server.Disconnect();

            await AsyncUtil.WaitUntilWithTimeout(() => !server.Active);

            Assert.That(serverObjectManager.SpawnObjects(), Is.False);
        });
Example #7
0
        public IEnumerator RemovePlayerForConnectionDestroyTest() => UniTask.ToCoroutine(async() =>
        {
            serverObjectManager.RemovePlayerForConnection(connectionToClient, true);

            await AsyncUtil.WaitUntilWithTimeout(() => !clientIdentity);

            Assert.That(!serverPlayerGO);
        });
Example #8
0
        public IEnumerator ChangeTarget() => UniTask.ToCoroutine(async() =>
        {
            serverComponent.target = serverPlayerGO;

            await AsyncUtil.WaitUntilWithTimeout(() => clientComponent.target != null);

            Assert.That(clientComponent.target, Is.SameAs(clientPlayerGO));
        });
Example #9
0
        public IEnumerator ServerRpcWithNetworkIdentity() => UniTask.ToCoroutine(async() =>
        {
            component.CmdNetworkIdentity(identity);

            await AsyncUtil.WaitUntilWithTimeout(() => component.cmdNi != null);

            Assert.That(component.cmdNi, Is.SameAs(identity));
        });
Example #10
0
        public IEnumerator NetworkManagerModeOfflineHostTest() => UniTask.ToCoroutine(async() =>
        {
            server.StopHost();

            await AsyncUtil.WaitUntilWithTimeout(() => !server.Active && !client.Active);

            Assert.That(manager.NetworkMode == NetworkManagerMode.None);
        });
        public IEnumerator ChangeSceneAdditiveLoadTest() => UniTask.ToCoroutine(async() =>
        {
            sceneManager.ChangeServerScene("Assets/Mirror/Tests/Runtime/testScene.unity", SceneOperation.LoadAdditive);

            await AsyncUtil.WaitUntilWithTimeout(() => SceneManager.GetSceneByName("testScene") != null);

            Assert.That(SceneManager.GetSceneByName("testScene"), Is.Not.Null);
        });
Example #12
0
        public IEnumerator ServerRpcWithSenderOnServer() => UniTask.ToCoroutine(async() =>
        {
            serverComponent.SendWithSender(1);

            await AsyncUtil.WaitUntilWithTimeout(() => serverComponent.cmdArg1 != 0);

            Assert.That(serverComponent.cmdArg1, Is.EqualTo(1));
            Assert.That(serverComponent.cmdSender, Is.Null, "ServerRPC called on server will have no sender");
        });
Example #13
0
        public IEnumerator ServerRpcWithSenderOnClient() => UniTask.ToCoroutine(async() =>
        {
            clientComponent.SendWithSender(1);

            await AsyncUtil.WaitUntilWithTimeout(() => serverComponent.cmdArg1 != 0);

            Assert.That(serverComponent.cmdArg1, Is.EqualTo(1));
            Assert.That(serverComponent.cmdSender, Is.EqualTo(serverPlayer), "ServerRpc called on client will have client's player (server version)");
        });
Example #14
0
        public IEnumerator ClientDisconnectTest() => UniTask.ToCoroutine(async() =>
        {
            var playerCount = server.Players.Count;
            client.Disconnect();

            await AsyncUtil.WaitUntilWithTimeout(() => client._connectState == ConnectState.Disconnected);
            // player could should be 1 less after client disconnects
            await AsyncUtil.WaitUntilWithTimeout(() => server.Players.Count == playerCount - 1);
        });
Example #15
0
        public IEnumerator ServerRpcWithSender() => UniTask.ToCoroutine(async() =>
        {
            playerComponent.SendWithSender(1);

            await AsyncUtil.WaitUntilWithTimeout(() => playerComponent.cmdArg1 != 0);

            Assert.That(playerComponent.cmdArg1, Is.EqualTo(1));
            Assert.That(playerComponent.cmdSender, Is.EqualTo(server.LocalPlayer), "Server Rpc call on host will have localplayer (server version) as sender");
        });
Example #16
0
        public IEnumerator RegisterMessage1() => UniTask.ToCoroutine(async() =>
        {
            var invoked = false;

            ServerMessageHandler.RegisterHandler <WovenTestMessage>(msg => invoked = true);
            clientPlayer.Send(message);

            await AsyncUtil.WaitUntilWithTimeout(() => invoked);
        });
Example #17
0
        public IEnumerator ClientOwnerRpc() => UniTask.ToCoroutine(async() =>
        {
            component.RpcOwnerTest(1, "hello");
            // process spawn message from server
            await AsyncUtil.WaitUntilWithTimeout(() => component.rpcOwnerArg1 != 0);

            Assert.That(component.rpcOwnerArg1, Is.EqualTo(1));
            Assert.That(component.rpcOwnerArg2, Is.EqualTo("hello"));
        });
Example #18
0
        public IEnumerator ServerRpc() => UniTask.ToCoroutine(async() =>
        {
            component.Test(1, "hello");

            await AsyncUtil.WaitUntilWithTimeout(() => component.cmdArg1 != 0);

            Assert.That(component.cmdArg1, Is.EqualTo(1));
            Assert.That(component.cmdArg2, Is.EqualTo("hello"));
        });
Example #19
0
        public IEnumerator ReadyMessageSetsClientReadyTest() => UniTask.ToCoroutine(async() =>
        {
            connectionToServer.Send(new ReadyMessage());

            await AsyncUtil.WaitUntilWithTimeout(() => connectionToClient.IsReady);

            // ready?
            Assert.That(connectionToClient.IsReady, Is.True);
        });
Example #20
0
        public IEnumerator RegisterMessage1() => UniTask.ToCoroutine(async() =>
        {
            bool invoked = false;

            connectionToClient.RegisterHandler <WovenTestMessage>(msg => invoked = true);
            connectionToServer.Send(message);

            await AsyncUtil.WaitUntilWithTimeout(() => invoked);
        });
Example #21
0
        public IEnumerator ReadyMessageSetsClientReadyTest() => UniTask.ToCoroutine(async() =>
        {
            clientPlayer.Send(new SceneReadyMessage());

            await AsyncUtil.WaitUntilWithTimeout(() => serverPlayer.SceneIsReady);

            // ready?
            Assert.That(serverPlayer.SceneIsReady, Is.True);
        });
Example #22
0
        public IEnumerator ClientReadyTest() => UniTask.ToCoroutine(async() =>
        {
            NetworkIdentity readyPlayer = CreateNetworkIdentity();
            readyComp = readyPlayer.gameObject.AddComponent <ObjectReady>();

            serverObjectManager.Spawn(readyPlayer, server.LocalPlayer);
            readyComp.Ready();

            await AsyncUtil.WaitUntilWithTimeout(() => readyComp.IsReady);
        });
Example #23
0
        public IEnumerator ClientConnRpc() => UniTask.ToCoroutine(async() =>
        {
            component.ClientConnRpcTest(manager.Server.LocalConnection, 1, "hello");
            // process spawn message from server
            await AsyncUtil.WaitUntilWithTimeout(() => component.targetRpcArg1 != 0);

            Assert.That(component.targetRpcConn, Is.SameAs(manager.Client.Connection));
            Assert.That(component.targetRpcArg1, Is.EqualTo(1));
            Assert.That(component.targetRpcArg2, Is.EqualTo("hello"));
        });
Example #24
0
        public IEnumerator ClientConnRpc() => UniTask.ToCoroutine(async() =>
        {
            playerComponent.ClientConnRpcTest(manager.Server.LocalPlayer, 1, "hello");
            // process spawn message from server
            await AsyncUtil.WaitUntilWithTimeout(() => playerComponent.targetRpcArg1 != 0);

            Assert.That(playerComponent.targetRpcPlayer, Is.EqualTo(manager.Client.Player));
            Assert.That(playerComponent.targetRpcArg1, Is.EqualTo(1));
            Assert.That(playerComponent.targetRpcArg2, Is.EqualTo("hello"));
        });
        public IEnumerator StopButtonTest() => UniTask.ToCoroutine(async() =>
        {
            networkManagerHud.StopButtonHandler();
            Assert.That(networkManagerHud.OfflineGO.activeSelf, Is.True);
            Assert.That(networkManagerHud.OnlineGO.activeSelf, Is.False);

            await AsyncUtil.WaitUntilWithTimeout(() => !manager.IsNetworkActive);

            Assert.That(manager.IsNetworkActive, Is.False);
        });
Example #26
0
        public IEnumerator ClientConnRpc() => UniTask.ToCoroutine(async() =>
        {
            serverComponent.ClientConnRpcTest(connectionToClient, 1, "hello");
            // process spawn message from server
            await AsyncUtil.WaitUntilWithTimeout(() => clientComponent.targetRpcArg1 != 0);

            Assert.That(clientComponent.targetRpcPlayer, Is.SameAs(connectionToServer));
            Assert.That(clientComponent.targetRpcArg1, Is.EqualTo(1));
            Assert.That(clientComponent.targetRpcArg2, Is.EqualTo("hello"));
        });
Example #27
0
        public IEnumerator IsNetworkActiveStopTest() => UniTask.ToCoroutine(async() =>
        {
            manager.Server.Disconnect();

            await AsyncUtil.WaitUntilWithTimeout(() => !client.Active);

            Assert.That(server.Active, Is.False);
            Assert.That(client.Active, Is.False);
            Assert.That(manager.IsNetworkActive, Is.False);
        });
        public IEnumerator ClientOfflineSceneException() => UniTask.ToCoroutine(async() =>
        {
            client.Disconnect();

            await AsyncUtil.WaitUntilWithTimeout(() => !client.Active);

            Assert.Throws <InvalidOperationException>(() =>
            {
                clientSceneManager.ClientSceneMessage(null, new SceneMessage());
            });
        });
Example #29
0
        public IEnumerator ObjectDestroyTest() => UniTask.ToCoroutine(async() =>
        {
            clientObjectManager.OnObjectDestroy(new ObjectDestroyMessage
            {
                netId = identity.NetId
            });

            await AsyncUtil.WaitUntilWithTimeout(() => identity == null);

            Assert.That(identity == null);
        });
        public IEnumerator ReadyExceptionTest() => UniTask.ToCoroutine(async() =>
        {
            sceneManager.Client.Disconnect();

            await AsyncUtil.WaitUntilWithTimeout(() => !sceneManager.Client.Active);

            Assert.Throws <InvalidOperationException>(() =>
            {
                sceneManager.SetClientReady();
            });
        });