<commit_before>﻿using System.IO;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using Should;

namespace MemoryMessagePipe.Tests
{
    [TestFixture]
    public class Tests
    {
        [Test]
        public void Should_have_ability_to_send_multiple_messages()
        {
            const string mmfName = "Local\\test";
            const string message1 = "message1";
            const string message2 = "message2";

            var task1 = new Task(() =>
            {
                using (var messageSender = new MemoryMappedFileMessageSender(mmfName))
                {
                    messageSender.SendMessage(x => WriteString(x, message1));
                    messageSender.SendMessage(x => WriteString(x, message2));
                }
            });

            string result1 = null, result2 = null;

            var task2 = new Task(() =>
            {
                using (var messageReceiver = new MemoryMappedFileMessageReceiver(mmfName))
                {
                    result1 = messageReceiver.ReceiveMessage(ReadString);
                    result2 = messageReceiver.ReceiveMessage(ReadString);
                }
            });

            task1.Start();
            task2.Start();

            task1.Wait();
            task2.Wait();

            result1.ShouldEqual(message1);
            result2.ShouldEqual(message2);
        }

        [Test]
        public void Should_be_able_to_cancel_message_reception()
        {
            var message = "not null";

            var messageReceiver = new MemoryMappedFileMessageReceiver("test");

            var task = new Task(() => message = messageReceiver.ReceiveMessage(ReadString));

            task.Start();

            messageReceiver.Dispose();

            message.ShouldBeNull();
        }

        private static void WriteString(Stream stream, string str)
        {
            var bytes = Encoding.UTF8.GetBytes(str);

            stream.Write(bytes, 0, bytes.Length);
        }

        private static string ReadString(Stream stream)<commit_msg>added wait to avoid race condition<commit_after>﻿using System.IO;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using Should;

namespace MemoryMessagePipe.Tests
{
    [TestFixture]
    public class Tests
    {
        [Test]
        public void Should_have_ability_to_send_multiple_messages()
        {
            const string mmfName = "Local\\test";
            const string message1 = "message1";
            const string message2 = "message2";

            var task1 = new Task(() =>
            {
                using (var messageSender = new MemoryMappedFileMessageSender(mmfName))
                {
                    messageSender.SendMessage(x => WriteString(x, message1));
                    messageSender.SendMessage(x => WriteString(x, message2));
                }
            });

            string result1 = null, result2 = null;

            var task2 = new Task(() =>
            {
                using (var messageReceiver = new MemoryMappedFileMessageReceiver(mmfName))
                {
                    result1 = messageReceiver.ReceiveMessage(ReadString);
                    result2 = messageReceiver.ReceiveMessage(ReadString);
                }
            });

            task1.Start();
            task2.Start();

            task1.Wait();
            task2.Wait();

            result1.ShouldEqual(message1);
            result2.ShouldEqual(message2);
        }

        [Test]
        public void Should_be_able_to_cancel_message_reception()
        {
            var message = "not null";

            var messageReceiver = new MemoryMappedFileMessageReceiver("test");

            var task = new Task(() => message = messageReceiver.ReceiveMessage(ReadString));

            task.Start();

            messageReceiver.Dispose();

            task.Wait();

            message.ShouldBeNull();
        }

        private static void WriteString(Stream stream, string str)
        {
            var bytes = Encoding.UTF8.GetBytes(str);

            stream.Write(bytes, 0, bytes.Length);
        }

        private static string ReadString(Stream stream)
{'fraction_non_alphanumeric': 0.09123477430140912, 'fraction_numerical': 0.010508717458801052, 'mean_word_length': 1.717715768981181, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '4423644', 'n_tokens_mistral': 1067, 'n_tokens_neox': 1008, 'n_words': 225}