<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using BitcoinUtilities.Threading;
using NUnit.Framework;

namespace Test.BitcoinUtilities.Threading
{
    [TestFixture]
    public class TestBlockingThreadPool
    {
        [Test]
        public void TestNoBlock()
        {
            MessageLog log = new MessageLog();

            BlockingThreadPool threadPool = new BlockingThreadPool(2, 2);

            for (int i = 0; i < 2; i++)
            {
                log.Clear();

                Assert.That(threadPool.Execute(() =>
                                               {
                                                   log.Log("task 1: started"); // T = 0
                                                   Thread.Sleep(100);
                                                   log.Log("task 1: finished"); // T = 100
                                               }, 100), Is.True);

                Thread.Sleep(10);
                Assert.That(threadPool.Execute(() =>
                                               {
                                                   log.Log("task 2: started"); // T = 10
                                                   Thread.Sleep(100);
                                                   log.Log("task 2: finished"); // T = 110
                                               }, 100), Is.True);

                Thread.Sleep(10);
                log.Log("main: wait started"); // T = 20
                Thread.Sleep(100);
                log.Log("main: wait finished"); // T = 120

                Assert.That(log.GetLog(), Is.EqualTo(new string[]
                                                     {
                                                         "task 1: started",
                                                         "task 2: started",
                                                         "main: wait started",
                                                         "task 1: finished",
                                                         "task 2: finished",
                                                         "main: wait finished"
                                                     }));
            }
        }

        [Test]
        public void TestWithBlock()
        {
            MessageLog log = new MessageLog();

            BlockingThreadPool threadPool = new BlockingThreadPool(2, 2);

            for (int i = 0; i < 2; i++)
            {
                log.Clear();

                Assert.That(threadPool.Execute(() =>
                                               {
                                                   log.Log("task 1: started"); // T = 0
                                                   Thread.Sleep(100);
                                                   log.Log("task 1: finished"); // T = 100
                                               }, 100), Is.True);

                Thread.Sleep(10);
                Assert.That(threadPool.Execute(() =>
                                               {
                                                   log.Log("task 2: started"); // T = 10
                                                   Thread.Sleep(200);
                                                   log.Log("task 2: finished"); // T = 210
                                               }, 100), Is.True);

                Thread.Sleep(10);
                Assert.That(threadPool.Execute(() =>
                                               {
                                                   log.Log("task 3: started");
                                                   Thread.Sleep(100);
                                                   log.Log("task 3: finished");
                                               }, 10), Is.False);<commit_msg>Fixed TestBlockingThreadPool under Release build.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using BitcoinUtilities.Threading;
using NUnit.Framework;

namespace Test.BitcoinUtilities.Threading
{
    [TestFixture]
    public class TestBlockingThreadPool
    {
        [TestFixtureSetUp]
        public void WarmUp()
        {
            //the first thread creation can take considerable time
            Stopwatch sw = Stopwatch.StartNew();
            AutoResetEvent ev = new AutoResetEvent(false);
            BlockingThreadPool threadPool = new BlockingThreadPool(1, 1);
            Assume.That(threadPool.Execute(() => ev.Set(), 10), Is.True);
            ev.WaitOne();
            threadPool.Stop();
            Console.WriteLine("WarmUp: {0}ms", sw.ElapsedMilliseconds);
        }

        [Test]
        public void TestNoBlock()
        {
            BlockingThreadPool threadPool = new BlockingThreadPool(2, 2);
            MessageLog log = new MessageLog();

            for (int i = 0; i < 2; i++)
            {
                log.Clear();

                Assert.That(threadPool.Execute(() =>
                                               {
                                                   log.Log("task 1: started"); // T = 0
                                                   Thread.Sleep(100);
                                                   log.Log("task 1: finished"); // T = 100
                                               }, 100), Is.True);

                Thread.Sleep(10);
                Assert.That(threadPool.Execute(() =>
                                               {
                                                   log.Log("task 2: started"); // T = 10
                                                   Thread.Sleep(100);
                                                   log.Log("task 2: finished"); // T = 110
                                               }, 100), Is.True);

                Thread.Sleep(10);
                log.Log("main: wait started"); // T = 20
                Thread.Sleep(100);
                log.Log("main: wait finished"); // T = 120

                Assert.That(log.GetLog(), Is.EqualTo(new string[]
                                                     {
                                                         "task 1: started",
                                                         "task 2: started",
                                                         "main: wait started",
                                                         "task 1: finished",
                                                         "task 2: finished",
                                                         "main: wait finished"
                                                     }));
            }
        }

        [Test]
        public void TestWithBlock()
        {
            BlockingThreadPool threadPool = new BlockingThreadPool(2, 2);
            MessageLog log = new MessageLog();

            for (int i = 0; i < 2; i++)
            {
                log.Clear();

                Assert.That(threadPool.Execute(() =>
                                               {
                                                   log.Log("task 1: started"); // T = 0
                                                   Thread.Sleep(100);
                                                   log.Log("task 1: finished"); // T = 100
                                               }, 100), Is.True);

                Thread.Sleep(10);
                Assert.That(threadPool.Execute(() =>
                                               {
                                                   log.Log("task 2: started"); // T = 10
                                                   Thread.Sleep(200);
                                                   log.Log("task 2: finished"); // T = 210
                                               }, 100), Is.True);

                Thread.Sleep(10);
                Assert.That(threadPool.Execute(() =>
                                               {
                                                   log.Log("task 3: started");
                                                   Thread.Sleep(100);
                                                   log.Log("task 3: finished");
                                               }, 10), Is.False);
{'fraction_non_alphanumeric': 0.09187236604455147, 'fraction_numerical': 0.02107164358819988, 'mean_word_length': 0.6675366392290705, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 132, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4936816', 'n_tokens_mistral': 1644, 'n_tokens_neox': 1436, 'n_words': 364}