<commit_before>﻿using System;
using System.Net;
using System.Threading;

using NUnit.Framework;

using Criteo.Memcache.Headers;
using Criteo.Memcache.Transport;

using Criteo.Memcache.UTest.Mocks;

namespace Criteo.Memcache.UTest.Tests
{
    [TestFixture]
    public class SynchTransportTest
    {
        private IPAddress LOCALHOST = new IPAddress(new byte[] { 127, 0, 0, 1 });

        [Test]
        public void MemcacheTransportSynchronousThreadedTest()
        {
            var endPoint = new IPEndPoint(LOCALHOST, 11213);
            using (ISynchronousTransport transport = new MemcacheSocketSynchronous(endPoint, null, null, 0, 0, _ => { }, true))
            {
                MemcacheSocketSynchronousTest(transport, endPoint);
            }
        }

        [Test]
        public void MemcacheTransportSynchronousAsyncTest()
        {
            var endPoint = new IPEndPoint(LOCALHOST, 11214);
            using (ISynchronousTransport transport = new MemcacheSocketSynchronous(endPoint, null, null, 0, 0, _ => { }, false))
            {
                MemcacheSocketSynchronousTest(transport, endPoint);
            }
        }

        public void MemcacheSocketSynchronousTest(ISynchronousTransport transport, IPEndPoint endPoint)
        {

            using (var serverMock = new ServerMock(endPoint))
            {
                // random header
                var requestHeader = new MemcacheRequestHeader
                {
                    Cas = 1,
                    DataType = 2,
                    ExtraLength = 5,
                    KeyLength = 3,
                    Opaque = 42,
                    Opcode = Opcode.Increment,
                    TotalBodyLength = 12,
                };
                // body with the size defined in the header
                var requestBody = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };

                // build the request buffer
                var requestBuffer = new byte[MemcacheRequestHeader.SIZE + requestHeader.TotalBodyLength];
                requestHeader.ToData(requestBuffer);
                Array.Copy(requestBody, 0, requestBuffer, MemcacheRequestHeader.SIZE, requestHeader.TotalBodyLength);

                // build the response header
                var responseHeader = new MemcacheResponseHeader
                {
                    Cas = 8,
                    DataType = 12,
                    ExtraLength = 3,
                    KeyLength = 5,
                    // must be the same or it will crash : TODO add a test to ensure we detect this fail
                    Opaque = requestHeader.Opaque,
                    Status = Status.UnknownCommand,
                    Opcode = Opcode.Prepend,
                    TotalBodyLength = 15,
                };
                // body with the size defined in the header
                var responseExtra = new byte[] { 1, 2, 3 };
                var responseMessage = new byte[] { 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
                
                // build the request buffer
                var responseBody = new byte[responseHeader.TotalBodyLength];
                Array.Copy(responseExtra, 0, responseBody, 0, responseHeader.ExtraLength);
                Array.Copy(responseMessage, 0, responseBody, responseHeader.ExtraLength, responseHeader.TotalBodyLength - responseHeader.ExtraLength);

                // set the things to answer to the server
                serverMock.ResponseBody = responseBody;
                serverMock.ResponseHeader = responseHeader;

                var request = new RequestMock
                {
                    QueryBuffer = requestBuffer,
                    RequestId = requestHeader.Opaque
                    /*ResponseHeader = responseHeader,
                    Message = responseMessage,
                    Extra = responseExtra,*/
                };

                // wait for the transport to be alive
                Thread.Sleep(1000);

                Assert.IsTrue(transport.TrySend(request));

                Assert.IsTrue(request.Mutex.Wait(TimeSpan.FromSeconds(10)), "The request has not been completed on less than 10 sec");

                // and now, assert that we sent what we had to send and we received what the server sent
                Assert.AreEqual(requestHeader, serverMock.LastReceivedHeader, "Sent header differ from header received by the server");
                CollectionAssert.AreEqual(requestBody, serverMock.LastReceivedBody, "Sent body is different than received by the server");

                Assert.AreEqual(responseHeader, request.ResponseHeader, "Received header differ from header sent by the server");
                CollectionAssert.AreEqual(responseExtra, request.Extra, "Received extra is different than sent by the server");
                CollectionAssert.AreEqual(responseMessage, request.Message, "Received message is different than sent by the server");
                //Thread.Sleep(1000);
            }
        }
    }
}<commit_msg>Fixed random fail on tests<commit_after>﻿using System;
using System.Net;
using System.Threading;

using NUnit.Framework;

using Criteo.Memcache.Headers;
using Criteo.Memcache.Transport;

using Criteo.Memcache.UTest.Mocks;

namespace Criteo.Memcache.UTest.Tests
{
    [TestFixture]
    public class SynchTransportTest
    {
        private IPAddress LOCALHOST = new IPAddress(new byte[] { 127, 0, 0, 1 });

        [Test]
        public void MemcacheTransportSynchronousThreadedTest()
        {
            var endPoint = new IPEndPoint(LOCALHOST, 11213);
            MemcacheSocketSynchronousTest(() => new MemcacheSocketSynchronous(endPoint, null, null, 0, 0, _ => { }, true), endPoint);
        }

        [Test]
        public void MemcacheTransportSynchronousAsyncTest()
        {
            var endPoint = new IPEndPoint(LOCALHOST, 11214);
            MemcacheSocketSynchronousTest(() => new MemcacheSocketSynchronous(endPoint, null, null, 0, 0, _ => { }, false), endPoint);
        }

        public void MemcacheSocketSynchronousTest(Func<ISynchronousTransport> transportFactory, IPEndPoint endPoint)
        {

            using (var serverMock = new ServerMock(endPoint))
            {
                // random header
                var requestHeader = new MemcacheRequestHeader
                {
                    Cas = 1,
                    DataType = 2,
                    ExtraLength = 5,
                    KeyLength = 3,
                    Opaque = 42,
                    Opcode = Opcode.Increment,
                    TotalBodyLength = 12,
                };
                // body with the size defined in the header
                var requestBody = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };

                // build the request buffer
                var requestBuffer = new byte[MemcacheRequestHeader.SIZE + requestHeader.TotalBodyLength];
                requestHeader.ToData(requestBuffer);
                Array.Copy(requestBody, 0, requestBuffer, MemcacheRequestHeader.SIZE, requestHeader.TotalBodyLength);

                // build the response header
                var responseHeader = new MemcacheResponseHeader
                {
                    Cas = 8,
                    DataType = 12,
                    ExtraLength = 3,
                    KeyLength = 5,
                    // must be the same or it will crash : TODO add a test to ensure we detect this fail
                    Opaque = requestHeader.Opaque,
                    Status = Status.UnknownCommand,
                    Opcode = Opcode.Prepend,
                    TotalBodyLength = 15,
                };
                // body with the size defined in the header
                var responseExtra = new byte[] { 1, 2, 3 };
                var responseMessage = new byte[] { 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
                
                // build the request buffer
                var responseBody = new byte[responseHeader.TotalBodyLength];
                Array.Copy(responseExtra, 0, responseBody, 0, responseHeader.ExtraLength);
                Array.Copy(responseMessage, 0, responseBody, responseHeader.ExtraLength, responseHeader.TotalBodyLength - responseHeader.ExtraLength);

                // set the things to answer to the server
                serverMock.ResponseBody = responseBody;
                serverMock.ResponseHeader = responseHeader;

                var request = new RequestMock
                {
                    QueryBuffer = requestBuffer,
                    RequestId = requestHeader.Opaque
                    /*ResponseHeader = responseHeader,
                    Message = responseMessage,
                    Extra = responseExtra,*/
                };

                using (var transport = transportFactory())
                {
                    Assert.IsTrue(transport.TrySend(request));

                    Assert.IsTrue(request.Mutex.Wait(TimeSpan.FromSeconds(10)), "The request has not been completed on less than 10 sec");

                    // and now, assert that we sent what we had to send and we received what the server sent
                    Assert.AreEqual(requestHeader, serverMock.LastReceivedHeader, "Sent header differ from header received by the server");
                    CollectionAssert.AreEqual(requestBody, serverMock.LastReceivedBody, "Sent body is different than received by the server");

                    Assert.AreEqual(responseHeader, request.ResponseHeader, "Received header differ from header sent by the server");
                    CollectionAssert.AreEqual(responseExtra, request.Extra, "Received extra is different than sent by the server");
                    CollectionAssert.AreEqual(responseMessage, request.Message, "Received message is different than sent by the server");
                    //Thread.Sleep(1000);
                }
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.07465242796695547, 'fraction_numerical': 0.017126737860165223, 'mean_word_length': 1.704167801688913, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1555533', 'n_tokens_mistral': 2585, 'n_tokens_neox': 2277, 'n_words': 704}