<commit_before>﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using OTransport;
using OTransport.NetworkChannel.UDP;
using OTransport.Test.Utilities;
using OTransport.tests;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Test
{
    [TestClass]
    public class UDPNetworkChannel_Server
    {
        UDPServerChannel udpServer = null;
        UDPClientChannel udpClient = null;
        UDPClientChannel udpClient2 = null;

        [TestCleanup]
        public void CleanUpServer()
        {
            if (udpServer != null)
                udpServer.Stop();
            if (udpClient != null)
                udpClient.Stop();
            if (udpClient2 != null)
                udpClient2.Stop();

            UDPObjectTransportChannel.TearDown();
        }
        [TestMethod]
        public void UDPServer_WhenClientConnects_CallbackFunctionCalled()
        {
            bool connected = false;
            udpServer = new UDPServerChannel("127.0.0.1", 0,32);
            udpServer.OnClientConnect(c => connected = true);

            udpClient = new UDPClientChannel("127.0.0.1", udpServer.Port);

            Utilities.WaitFor(ref connected);
            Assert.IsTrue(connected);
        }
        [TestMethod]
        public void UDPServer_ReceivesObjects_CorrectObjectReceived()
        {
            //Arrange
            MockObjectMessage receivedObject = null;
            var connectTransports = UDPObjectTransportChannel.GetConnectObjectTransports();
            var server = connectTransports.Item1;
            var client = connectTransports.Item2;

            //Act
            server.Receive<MockObjectMessage>(o =>
            {
                receivedObject = o;

            }).Execute();

            client.Send(new MockObjectMessage()
            {
                Property1_string = "hello world!",
                Property2_int = 123,
                Property3_decimal = 12.3M
            }).Execute();

            Utilities.WaitFor(ref receivedObject);
            //Assert
            Assert.AreEqual("hello world!", receivedObject.Property1_string);
            Assert.AreEqual(123, receivedObject.Property2_int);
            Assert.AreEqual(12.3M, receivedObject.Property3_decimal);
        }

        [TestMethod]
        public void UDPServer_SendObject_CorrectObjectSent()
        {
            //Arrange
            MockObjectMessage receivedObject = null;
            udpServer = new UDPServerChannel("127.0.0.1", 0,32);

            Client client = null;

            ObjectTransport serverObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(udpServer);
            serverObjectTransport.OnClientConnect(c => client = c);

            udpClient = new UDPClientChannel("127.0.0.1", udpServer.Port);
            ObjectTransport clientObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(udpClient);

            Utilities.WaitFor(ref client);

            //Act

            clientObjectTransport.Receive<MockObjectMessage>(o =>
            receivedObject = o
            ).Execute();

            serverObjectTransport.Send(new MockObjectMessage()
            {
                Property1_string = "hello world!",
                Property2_int = 123,
                Property3_decimal = 12.3M

            })
            .To(client)
            .Execute();

            Utilities.WaitFor(ref receivedObject);
            //Assert
            Assert.AreEqual("hello world!", receivedObject.Property1_string);
            Assert.AreEqual(123, receivedObject.Property2_int);
            Assert.AreEqual(12.3M, receivedObject.Property3_decimal);
        }

        [TestMethod]
        public void UDPServer_Disconnect1Client_1ClientDisconnected()
        {
            //Arrange
            Client disconnectedClient = null;

            udpServer = new UDPServerChannel("127.0.0.1", 0,32);
            ObjectTransport serverObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(udpServer);
            serverObjectTransport.OnClientDisconnect(c => disconnectedClient = c);

            udpClient = new UDPClientChannel("127.0.0.1", udpServer.Port);
            ObjectTransport clientObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(udpClient);

            udpClient2 = new UDPClientChannel("127.0.0.1", udpServer.Port);
            ObjectTransport clientObjectTransport2 = TestObjectTransportFactory.CreateNewObjectTransport(udpClient2);

            Utilities.WaitFor(() => clientObjectTransport.GetConnectedClients().Count() == 2);

            //Act

            var client = serverObjectTransport.GetConnectedClients().First();
            serverObjectTransport.DisconnectClient(client);

            Utilities.WaitFor(ref disconnectedClient);

            //Assert
            Assert.AreEqual(1,serverObjectTransport.GetConnectedClients().Count());
        }

        [TestMethod]
        public void UDPServer_ClientDisconnects_CallbackCalled()
        {
            //Arrange
            Client clientConnect = null;
            Client clientDisconnect = null;

            udpServer = new UDPServerChannel("127.0.0.1", 0,32);
            ObjectTransport serverObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(udpServer);
            serverObjectTransport.OnClientConnect(c => clientConnect = c);
            serverObjectTransport.OnClientDisconnect(c => clientDisconnect = c);

            udpClient = new UDPClientChannel("127.0.0.1", udpServer.Port);<commit_msg>Added unit test for client disconnect<commit_after>﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using OTransport;
using OTransport.NetworkChannel.UDP;
using OTransport.Test.Utilities;
using OTransport.tests;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Test
{
    [TestClass]
    public class UDPNetworkChannel_Server
    {
        UDPServerChannel udpServer = null;
        UDPClientChannel udpClient = null;
        UDPClientChannel udpClient2 = null;

        [TestCleanup]
        public void CleanUpServer()
        {
            if (udpServer != null)
                udpServer.Stop();
            if (udpClient != null)
                udpClient.Stop();
            if (udpClient2 != null)
                udpClient2.Stop();

            UDPObjectTransportChannel.TearDown();
        }
        [TestMethod]
        public void UDPServer_WhenClientConnects_CallbackFunctionCalled()
        {
            bool connected = false;
            udpServer = new UDPServerChannel("127.0.0.1", 0,32);
            udpServer.OnClientConnect(c => connected = true);

            udpClient = new UDPClientChannel("127.0.0.1", udpServer.Port);

            Utilities.WaitFor(ref connected);
            Assert.IsTrue(connected);
        }
        [TestMethod]
        public void UDPServer_ReceivesObjects_CorrectObjectReceived()
        {
            //Arrange
            MockObjectMessage receivedObject = null;
            var connectTransports = UDPObjectTransportChannel.GetConnectObjectTransports();
            var server = connectTransports.Item1;
            var client = connectTransports.Item2;

            //Act
            server.Receive<MockObjectMessage>(o =>
            {
                receivedObject = o;

            }).Execute();

            client.Send(new MockObjectMessage()
            {
                Property1_string = "hello world!",
                Property2_int = 123,
                Property3_decimal = 12.3M
            }).Execute();

            Utilities.WaitFor(ref receivedObject);
            //Assert
            Assert.AreEqual("hello world!", receivedObject.Property1_string);
            Assert.AreEqual(123, receivedObject.Property2_int);
            Assert.AreEqual(12.3M, receivedObject.Property3_decimal);
        }

        [TestMethod]
        public void UDPServer_SendObject_CorrectObjectSent()
        {
            //Arrange
            MockObjectMessage receivedObject = null;
            udpServer = new UDPServerChannel("127.0.0.1", 0,32);

            Client client = null;

            ObjectTransport serverObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(udpServer);
            serverObjectTransport.OnClientConnect(c => client = c);

            udpClient = new UDPClientChannel("127.0.0.1", udpServer.Port);
            ObjectTransport clientObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(udpClient);

            Utilities.WaitFor(ref client);

            //Act

            clientObjectTransport.Receive<MockObjectMessage>(o =>
            receivedObject = o
            ).Execute();

            serverObjectTransport.Send(new MockObjectMessage()
            {
                Property1_string = "hello world!",
                Property2_int = 123,
                Property3_decimal = 12.3M

            })
            .To(client)
            .Execute();

            Utilities.WaitFor(ref receivedObject);
            //Assert
            Assert.AreEqual("hello world!", receivedObject.Property1_string);
            Assert.AreEqual(123, receivedObject.Property2_int);
            Assert.AreEqual(12.3M, receivedObject.Property3_decimal);
        }

        [TestMethod]
        public void UDPServerWith2Clients_Disconnect1Client_1ClientDisconnected()
        {
            //Arrange
            Client disconnectedClient = null;

            udpServer = new UDPServerChannel("127.0.0.1", 0,32);
            ObjectTransport serverObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(udpServer);
            serverObjectTransport.OnClientDisconnect(c => disconnectedClient = c);

            udpClient = new UDPClientChannel("127.0.0.1", udpServer.Port);
            ObjectTransport clientObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(udpClient);

            udpClient2 = new UDPClientChannel("127.0.0.1", udpServer.Port);
            ObjectTransport clientObjectTransport2 = TestObjectTransportFactory.CreateNewObjectTransport(udpClient2);

            Utilities.WaitFor(() => serverObjectTransport.GetConnectedClients().Count() == 2);

            //Act

            var FirstClient = serverObjectTransport.GetConnectedClients().First();
            serverObjectTransport.DisconnectClient(FirstClient);

            Utilities.WaitFor(ref disconnectedClient);

            //Assert
            Client LastClient = serverObjectTransport.GetConnectedClients().First();

            Assert.AreEqual(1,serverObjectTransport.GetConnectedClients().Count());
            Assert.AreNotEqual(client.Port,LastClient.Port);
        }

        [TestMethod]
        public void UDPServer_ClientDisconnects_CallbackCalled()
        {
            //Arrange
            Client clientConnect = null;
            Client clientDisconnect = null;

            udpServer = new UDPServerChannel("127.0.0.1", 0,32);
            ObjectTransport serverObjectTransport = TestObjectTransportFactory.CreateNewObjectTransport(udpServer);
            serverObjectTransport.OnClientConnect(c => clientConnect = c);
            serverObjectTransport.OnClientDisconnect(c => clientDisconnect = c);

            udpClient = new UDPClientChannel("127.0.0.1", udpServer.Port);
{'fraction_non_alphanumeric': 0.08687615526802218, 'fraction_numerical': 0.02015667634891295, 'mean_word_length': 2.236115066932498, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 18, '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': '2889670', 'n_tokens_mistral': 3011, 'n_tokens_neox': 2840, 'n_words': 494}