<commit_before>﻿using System.Collections.Generic;
using System.Text;
using System.Threading;

using LiteNetLib.Tests.TestUtility;
using LiteNetLib.Utils;

using NUnit.Framework;

namespace LiteNetLib.Tests
{
    [TestFixture]
    [Category("Communication")]
#if !NETCOREAPP2_0
    [Timeout(10000)]
#endif
    public class CommunicationTest
    {
        [SetUp]
        public void Init()
        {
            ManagerStack = new NetManagerStack(DefaultAppKey, DefaultPort);
        }

        [TearDown]
        public void TearDown()
        {
            ManagerStack?.Dispose();
        }

        private const int DefaultPort = 9050;
        private const string DefaultAppKey = "test_server";

        public NetManagerStack ManagerStack { get; set; }

        [Test, MaxTime(2000)]
        public void ConnectionByIpV4()
        {
            var server = ManagerStack.Server(1);
            var client = ManagerStack.Client(1);
            client.Connect("127.0.0.1", DefaultPort, DefaultAppKey);

            while (server.PeersCount != 1)
            {
                Thread.Sleep(15);
                server.PollEvents();
            }

            Assert.AreEqual(server.PeersCount, 1);
            Assert.AreEqual(client.PeersCount, 1);
        }

        [Test, MaxTime(10000)]
        public void ConnectionFailedTest()
        {
            NetManager client = ManagerStack.Client(1);

            var result = false;
            DisconnectInfo disconnectInfo = default(DisconnectInfo);

            ManagerStack.ClientListener(1).PeerConnectedEvent += peer =>
            {
                result = true;
            };
            ManagerStack.ClientListener(1).PeerDisconnectedEvent += (peer, info) => 
            {
                result = true;
                disconnectInfo = info;
            };

            client.Connect("127.0.0.2", DefaultPort, DefaultAppKey);

            while (!result)
            {
                Thread.Sleep(15);
                client.PollEvents();
            }

            Assert.True(result);
            Assert.AreEqual(DisconnectReason.ConnectionFailed, disconnectInfo.Reason);
        }

        [Test, MaxTime(10000)]
        public void NetPeerDisconnectTimeout()
        {
            NetManager client = ManagerStack.Client(1);
            NetManager server = ManagerStack.Server(1);

            //Default 5 sec timeout for local network is too mach, set 1 for test
            server.DisconnectTimeout = 1000;

            NetPeer clientServerPeer = client.Connect("127.0.0.1", DefaultPort, DefaultAppKey);

            while (clientServerPeer.ConnectionState != ConnectionState.Connected)
            {
                Thread.Sleep(15);
                server.PollEvents();
                client.PollEvents();
            }

            Assert.AreEqual(ConnectionState.Connected, clientServerPeer.ConnectionState);
            Assert.True(server.PeersCount == 1);

            ManagerStack.ClientListener(1).PeerDisconnectedEvent += (peer, info) =>
            {
                Assert.AreEqual(clientServerPeer, peer);
                Assert.AreEqual(DisconnectReason.Timeout, info.Reason);
            };

            server.Stop();
            
            Assert.True(server.PeersCount == 0);
            Assert.True(client.PeersCount == 1);

            while (client.PeersCount == 1)
            {
                Thread.Sleep(15);
            }
        }



#if !NETCOREAPP2_0
        //TODO: Timeout attribute not work in netcoreapp
        [Test, MaxTime(10000)]
#endif
        public void NetPeerDisconnectAll()
        {
            NetManager client = ManagerStack.Client(1);
            NetManager client2 = ManagerStack.Client(2);
            NetManager server = ManagerStack.Server(1);

            NetPeer clientServerPeer = client.Connect("127.0.0.1", DefaultPort, DefaultAppKey);
            client2.Connect("127.0.0.1", DefaultPort, DefaultAppKey);

            while (clientServerPeer.ConnectionState != ConnectionState.Connected)
            {
                Thread.Sleep(15);
                server.PollEvents();
                client.PollEvents();
                client2.PollEvents();
            }

            Assert.AreEqual(ConnectionState.Connected, clientServerPeer.ConnectionState);
            Assert.AreEqual(2, server.GetPeersCount(ConnectionState.Connected));

            ManagerStack.ClientListener(1).PeerDisconnectedEvent += (peer, info) =>
            {
                byte[] bytes = info.AdditionalData.GetRemainingBytes();
                Assert.AreEqual(new byte[] { 1, 2, 3, 4 }, bytes);
                Assert.AreEqual(clientServerPeer, peer);
                Assert.AreEqual(DisconnectReason.RemoteConnectionClose, info.Reason);
            };

            server.DisconnectAll(new byte[]{1, 2, 3, 4}, 0, 4);
            
            Assert.AreEqual(0, server.GetPeersCount(ConnectionState.Connected));
            Assert.AreEqual(2, server.GetPeersCount(ConnectionState.ShutdownRequested));

            while (client.GetPeersCount(ConnectionState.Connected) != 0)
            {
                Thread.Sleep(15);
                client.PollEvents();
                server.PollEvents();
            }
           
            Thread.Sleep(100);

            Assert.AreEqual(0, server.GetPeersCount(ConnectionState.ShutdownRequested));
            Assert.AreEqual(0, client.GetPeersCount(ConnectionState.Connected));
            Assert.AreEqual(ConnectionState.Disconnected, clientServerPeer.ConnectionState);
        }

        [Test, MaxTime(2000)]
        public void DisconnectTest()
        {
            var server = ManagerStack.Server(1);<commit_msg>* Refactor<commit_after>﻿using System.Collections.Generic;
using System.Text;
using System.Threading;

using LiteNetLib.Tests.TestUtility;
using LiteNetLib.Utils;

using NUnit.Framework;

namespace LiteNetLib.Tests
{
    [TestFixture]
    [Category("Communication")]
#if !NETCOREAPP2_0
    [Timeout(10000)]
#endif
    public class CommunicationTest
    {
        [SetUp]
        public void Init()
        {
            ManagerStack = new NetManagerStack(DefaultAppKey, DefaultPort);
        }

        [TearDown]
        public void TearDown()
        {
            ManagerStack?.Dispose();
        }

        private const int DefaultPort = 9050;
        private const string DefaultAppKey = "test_server";

        public NetManagerStack ManagerStack { get; set; }

        [Test, MaxTime(2000)]
        public void ConnectionByIpV4()
        {
            var server = ManagerStack.Server(1);
            var client = ManagerStack.Client(1);
            client.Connect("127.0.0.1", DefaultPort, DefaultAppKey);

            while (server.PeersCount != 1)
            {
                Thread.Sleep(15);
                server.PollEvents();
            }

            Assert.AreEqual(server.PeersCount, 1);
            Assert.AreEqual(client.PeersCount, 1);
        }

        [Test, MaxTime(10000)]
        public void ConnectionFailedTest()
        {
            NetManager client = ManagerStack.Client(1);

            var result = false;
            DisconnectInfo disconnectInfo = default(DisconnectInfo);

            ManagerStack.ClientListener(1).PeerConnectedEvent += peer =>
            {
                result = true;
            };
            ManagerStack.ClientListener(1).PeerDisconnectedEvent += (peer, info) => 
            {
                result = true;
                disconnectInfo = info;
            };

            client.Connect("127.0.0.2", DefaultPort, DefaultAppKey);

            while (!result)
            {
                Thread.Sleep(15);
                client.PollEvents();
            }

            Assert.True(result);
            Assert.AreEqual(DisconnectReason.ConnectionFailed, disconnectInfo.Reason);
        }

        [Test, MaxTime(10000)]
        public void NetPeerDisconnectTimeout()
        {
            NetManager client = ManagerStack.Client(1);
            NetManager server = ManagerStack.Server(1);

            //Default 5 sec timeout for local network is too mach, set 1 for test
            server.DisconnectTimeout = 1000;

            NetPeer clientServerPeer = client.Connect("127.0.0.1", DefaultPort, DefaultAppKey);

            while (clientServerPeer.ConnectionState != ConnectionState.Connected)
            {
                Thread.Sleep(15);
                server.PollEvents();
                client.PollEvents();
            }

            Assert.AreEqual(ConnectionState.Connected, clientServerPeer.ConnectionState);
            Assert.True(server.PeersCount == 1);

            ManagerStack.ClientListener(1).PeerDisconnectedEvent += (peer, info) =>
            {
                Assert.AreEqual(clientServerPeer, peer);
                Assert.AreEqual(DisconnectReason.Timeout, info.Reason);
            };

            server.Stop();
            
            Assert.True(server.PeersCount == 0);
            Assert.True(client.PeersCount == 1);

            while (client.PeersCount == 1)
            {
                Thread.Sleep(15);
            }
        }



#if !NETCOREAPP2_0
        //TODO: Timeout attribute not work in netcoreapp
        [Test, MaxTime(10000)]
#endif
        public void NetPeerDisconnectAll()
        {
            NetManager client = ManagerStack.Client(1);
            NetManager client2 = ManagerStack.Client(2);
            NetManager server = ManagerStack.Server(1);

            NetPeer clientServerPeer = client.Connect("127.0.0.1", DefaultPort, DefaultAppKey);
            client2.Connect("127.0.0.1", DefaultPort, DefaultAppKey);

            while (clientServerPeer.ConnectionState != ConnectionState.Connected)
            {
                Thread.Sleep(15);
                server.PollEvents();
                client.PollEvents();
                client2.PollEvents();
            }

            Assert.AreEqual(ConnectionState.Connected, clientServerPeer.ConnectionState);
            Assert.AreEqual(2, server.GetPeersCount(ConnectionState.Connected));

            ManagerStack.ClientListener(1).PeerDisconnectedEvent += (peer, info) =>
            {
                byte[] bytes = info.AdditionalData.GetRemainingBytes();
                Assert.AreEqual(new byte[] { 1, 2, 3, 4 }, bytes);
                Assert.AreEqual(clientServerPeer, peer);
                Assert.AreEqual(DisconnectReason.RemoteConnectionClose, info.Reason);
            };

            server.DisconnectAll(new byte[]{1, 2, 3, 4}, 0, 4);
            
            Assert.AreEqual(0, server.GetPeersCount(ConnectionState.Connected));
            Assert.AreEqual(2, server.GetPeersCount(ConnectionState.ShutdownRequested));

            while (client.GetPeersCount(ConnectionState.Connected) != 0)
            {
                Thread.Sleep(15);
                client.PollEvents();
                server.PollEvents();
            }
           
            //Wait for client 'ShutdownOk' response
            Thread.Sleep(100);

            Assert.AreEqual(0, server.PeersCount);
            Assert.AreEqual(ConnectionState.Disconnected, clientServerPeer.ConnectionState);
        }

        [Test, MaxTime(2000)]
        public void DisconnectTest()
        {
            var server = ManagerStack.Server(1);
{'fraction_non_alphanumeric': 0.10054925584691708, 'fraction_numerical': 0.02241318214032601, 'mean_word_length': 1.8173196905415523, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 10, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7038661', 'n_tokens_mistral': 3110, 'n_tokens_neox': 2847, 'n_words': 521}