<commit_before>﻿using System.Collections.Concurrent;
using System.Diagnostics;

namespace TestRunner.System.Collections.Concurrent
{
    [TestFixture]
    public static class BlockingCollectionTest
    {
        [Test]
        public static void TryTakeFromEmptyBlockingCollectionShouldNotThrow()
        {
            var a = new BlockingCollection<int>();
            a.TryTake(out var _);
        }

        [Test]
        public static void TryTakeFromEmptyBlockingCollectionWithTimeoutShouldWait()
        {
            var a = new BlockingCollection<int>();
            var stopWatch = new Stopwatch();
            stopWatch.Start();
            a.TryTake(out var _, 200);
            stopWatch.Stop();
            Assert.IsTrue(stopWatch.ElapsedMilliseconds >= 200);
        }
    }
}<commit_msg>#44 Add unit test for BlockingCollection.<commit_after>﻿using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading;

namespace TestRunner.System.Collections.Concurrent
{
    [TestFixture]
    public static class BlockingCollectionTest
    {
        [Test]
        public static void TryTakeFromEmptyBlockingCollectionShouldNotThrow()
        {
            var a = new BlockingCollection<int>();
            a.TryTake(out var _);
        }

        [Test]
        public static void TryTakeFromEmptyBlockingCollectionWithTimeoutShouldWait()
        {
            var a = new BlockingCollection<int>();
            var stopWatch = new Stopwatch();
            stopWatch.Start();
            a.TryTake(out var _, 200);
            stopWatch.Stop();
            Assert.IsTrue(stopWatch.ElapsedMilliseconds >= 200);
        }

        private delegate void TestAction();

        private static void GetConsumingEnumerableTestImpl(CancellationToken? cancellationToken)
        {
            var called = 0;
            var ev = new ManualResetEventSlim(false);

            var bc = new BlockingCollection<TestAction>();

            var t = new Thread(() => {
                try
                {
                    ev.Set();

                    if (cancellationToken.HasValue)
                    {
                        foreach (var e in bc.GetConsumingEnumerable(cancellationToken.Value))
                        {
                            e();
                        }
                    }
                    else
                    {
                        foreach (var e in bc.GetConsumingEnumerable())
                        {
                            e();
                        }
                    }
                }
                catch (OperationCanceledException)
                {

                }
            });
            t.Start();

            // Make sure thread is running
            ev.Wait();
            Thread.Sleep(200);

            bc.Add(() => called++);
            bc.Add(() => called++);

            bc.CompleteAdding();
            t.Join();

            Assert.AreEqual(2, called);
        }

        [Test]
        public static void GetConsumingEnumerableShouldBlockWithNoCancellationToken()
        {
            GetConsumingEnumerableTestImpl(null);
        }

        [Test]
        public static void GetConsumingEnumerableShouldBlockWithCancellationTokenNone()
        {
            GetConsumingEnumerableTestImpl(CancellationToken.None);
        }

        [Test]
        public static void GetConsumingEnumerableShouldBlockWithCancellationTokenNotNone()
        {
            var cancellationTokenSource = new CancellationTokenSource();
            GetConsumingEnumerableTestImpl(cancellationTokenSource.Token);
        }
    }
}
{'fraction_non_alphanumeric': 0.08053875755909841, 'fraction_numerical': 0.005222649807586586, 'mean_word_length': 1.5554775280898876, 'pattern_counts': {'":': 0, '<': 8, '<?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': '7249787', 'n_tokens_mistral': 875, 'n_tokens_neox': 842, 'n_words': 174}