<commit_before>﻿//-----------------------------------------------------------------------
// <copyright file="LoopingSchedulerTest.cs" company="<NAME>">
// Copyright (c) <NAME>. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace EventHandlerSample.Test.Unit
{
    using System;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using FluentAssertions;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class LoopingSchedulerTest
    {
        public LoopingSchedulerTest()
        {
        }

        [TestMethod]
        public void RunThrowsOnFirstException()
        {
            Exception exception = new InvalidOperationException("Expected.");
            TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
            tcs.SetException(exception);
            LoopingScheduler scheduler = new LoopingScheduler(() => tcs.Task);
            
            Task task = scheduler.RunAsync(TimeSpan.MaxValue);

            AssertTaskThrows(task, exception);
        }

        [TestMethod]
        public void RunExecutesTasksUntilException()
        {
            Exception exception = new InvalidOperationException("Expected.");
            TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
            tcs.SetException(exception);
            Task[] tasks = new Task[]
            {
                Task.FromResult(false),
                Task.FromResult(true),
                tcs.Task
            };

            int invokeCount = 0;
            LoopingScheduler scheduler = new LoopingScheduler(() => tasks[invokeCount++]);

            Task task = scheduler.RunAsync(TimeSpan.MaxValue);

            task.IsCompleted.Should().BeTrue();
            task.Exception.Should().NotBeNull();
            task.Exception.InnerExceptions.Should().HaveCount(1).And.Contain(exception);
            invokeCount.Should().Be(3);
        }

        [TestMethod]
        public void RunRaisesPausedAfterFinitePauseInterval()
        {
            Exception exception = new InvalidOperationException("Expected.");
            int invokeCount = 0;
            LoopingScheduler scheduler = new LoopingScheduler(() => Task.FromResult(++invokeCount));
            scheduler.GetElapsed = () => TimeSpan.FromSeconds(invokeCount);
            scheduler.Paused += delegate
            {
                throw exception;
            };

            Task task = scheduler.RunAsync(TimeSpan.FromSeconds(1.0d));

            task.IsCompleted.Should().BeTrue();
            task.Exception.Should().NotBeNull();
            task.Exception.InnerExceptions.Should().HaveCount(1).And.Contain(exception);
            invokeCount.Should().Be(1);
        }

        [TestMethod]
        public void RunRaisesPausedAfterFinitePauseIntervalTwoIterationsLater()
        {
            Exception exception = new InvalidOperationException("Expected.");
            int invokeCount = 0;
            LoopingScheduler scheduler = new LoopingScheduler(() => Task.FromResult(++invokeCount));
            scheduler.GetElapsed = () => TimeSpan.FromSeconds(invokeCount);
            scheduler.Paused += delegate
            {
                throw exception;
            };

            Task task = scheduler.RunAsync(TimeSpan.FromSeconds(2.0d));

            task.IsCompleted.Should().BeTrue();
            task.Exception.Should().NotBeNull();
            task.Exception.InnerExceptions.Should().HaveCount(1).And.Contain(exception);
            invokeCount.Should().Be(2);
        }

        [TestMethod]
        public void RunRaisesPausedAfterFinitePauseIntervalRelativeToNonZeroElapsedTwoIterationsLater()
        {
            Exception exception = new InvalidOperationException("Expected.");
            int invokeCount = 0;
            LoopingScheduler scheduler = new LoopingScheduler(() => Task.FromResult(++invokeCount));
            scheduler.GetElapsed = () => TimeSpan.FromSeconds(1 + invokeCount);
            scheduler.Paused += delegate
            {
                throw exception;
            };

            Task task = scheduler.RunAsync(TimeSpan.FromSeconds(2.0d));

            task.IsCompleted.Should().BeTrue();
            task.Exception.Should().NotBeNull();
            task.Exception.InnerExceptions.Should().HaveCount(1).And.Contain(exception);
            invokeCount.Should().Be(2);
        }

        [TestMethod]
        public void RunRaisesPausedTwiceOnTwoElapsedPauseIntervalsOfTwoSeconds()
        {
            Exception exception = new InvalidOperationException("Expected.");
            int pauseInvokeCount = 0;
            int invokeCount = 0;
            LoopingScheduler scheduler = new LoopingScheduler(() => Task.FromResult(++invokeCount));
            scheduler.GetElapsed = () => TimeSpan.FromSeconds(invokeCount);
            scheduler.Paused += delegate
            {
                if (++pauseInvokeCount == 2)
                {
                    throw exception;
                }
            };

            Task task = scheduler.RunAsync(TimeSpan.FromSeconds(2.0d));

            task.IsCompleted.Should().BeTrue();
            task.Exception.Should().NotBeNull();
            task.Exception.InnerExceptions.Should().HaveCount(1).And.Contain(exception);
            invokeCount.Should().Be(4);
            pauseInvokeCount.Should().Be(2);
        }

        private static void AssertTaskThrows(Task task, Exception expected)
        {
            task.IsCompleted.Should().BeTrue();
            task.Exception.Should().NotBeNull();
            task.Exception.InnerExceptions.Should().HaveCount(1).And.Contain(expected);
        }
    }
}<commit_msg>Refactor remaining task throws assertions<commit_after>﻿//-----------------------------------------------------------------------
// <copyright file="LoopingSchedulerTest.cs" company="<NAME>">
// Copyright (c) <NAME>. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace EventHandlerSample.Test.Unit
{
    using System;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using FluentAssertions;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class LoopingSchedulerTest
    {
        public LoopingSchedulerTest()
        {
        }

        [TestMethod]
        public void RunThrowsOnFirstException()
        {
            Exception exception = new InvalidOperationException("Expected.");
            TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
            tcs.SetException(exception);
            LoopingScheduler scheduler = new LoopingScheduler(() => tcs.Task);
            
            Task task = scheduler.RunAsync(TimeSpan.MaxValue);

            AssertTaskThrows(task, exception);
        }

        [TestMethod]
        public void RunExecutesTasksUntilException()
        {
            Exception exception = new InvalidOperationException("Expected.");
            TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
            tcs.SetException(exception);
            Task[] tasks = new Task[]
            {
                Task.FromResult(false),
                Task.FromResult(true),
                tcs.Task
            };

            int invokeCount = 0;
            LoopingScheduler scheduler = new LoopingScheduler(() => tasks[invokeCount++]);

            Task task = scheduler.RunAsync(TimeSpan.MaxValue);

            AssertTaskThrows(task, exception);
            invokeCount.Should().Be(3);
        }

        [TestMethod]
        public void RunRaisesPausedAfterFinitePauseInterval()
        {
            Exception exception = new InvalidOperationException("Expected.");
            int invokeCount = 0;
            LoopingScheduler scheduler = new LoopingScheduler(() => Task.FromResult(++invokeCount));
            scheduler.GetElapsed = () => TimeSpan.FromSeconds(invokeCount);
            scheduler.Paused += delegate
            {
                throw exception;
            };

            Task task = scheduler.RunAsync(TimeSpan.FromSeconds(1.0d));

            AssertTaskThrows(task, exception);
            invokeCount.Should().Be(1);
        }

        [TestMethod]
        public void RunRaisesPausedAfterFinitePauseIntervalTwoIterationsLater()
        {
            Exception exception = new InvalidOperationException("Expected.");
            int invokeCount = 0;
            LoopingScheduler scheduler = new LoopingScheduler(() => Task.FromResult(++invokeCount));
            scheduler.GetElapsed = () => TimeSpan.FromSeconds(invokeCount);
            scheduler.Paused += delegate
            {
                throw exception;
            };

            Task task = scheduler.RunAsync(TimeSpan.FromSeconds(2.0d));

            AssertTaskThrows(task, exception);
            invokeCount.Should().Be(2);
        }

        [TestMethod]
        public void RunRaisesPausedAfterFinitePauseIntervalRelativeToNonZeroElapsedTwoIterationsLater()
        {
            Exception exception = new InvalidOperationException("Expected.");
            int invokeCount = 0;
            LoopingScheduler scheduler = new LoopingScheduler(() => Task.FromResult(++invokeCount));
            scheduler.GetElapsed = () => TimeSpan.FromSeconds(1 + invokeCount);
            scheduler.Paused += delegate
            {
                throw exception;
            };

            Task task = scheduler.RunAsync(TimeSpan.FromSeconds(2.0d));

            AssertTaskThrows(task, exception);
            invokeCount.Should().Be(2);
        }

        [TestMethod]
        public void RunRaisesPausedTwiceOnTwoElapsedPauseIntervalsOfTwoSeconds()
        {
            Exception exception = new InvalidOperationException("Expected.");
            int pauseInvokeCount = 0;
            int invokeCount = 0;
            LoopingScheduler scheduler = new LoopingScheduler(() => Task.FromResult(++invokeCount));
            scheduler.GetElapsed = () => TimeSpan.FromSeconds(invokeCount);
            scheduler.Paused += delegate
            {
                if (++pauseInvokeCount == 2)
                {
                    throw exception;
                }
            };

            Task task = scheduler.RunAsync(TimeSpan.FromSeconds(2.0d));

            AssertTaskThrows(task, exception);
            invokeCount.Should().Be(4);
            pauseInvokeCount.Should().Be(2);
        }

        private static void AssertTaskThrows(Task task, Exception expected)
        {
            task.IsCompleted.Should().BeTrue();
            task.Exception.Should().NotBeNull();
            task.Exception.InnerExceptions.Should().HaveCount(1).And.Contain(expected);
        }
    }
}
{'fraction_non_alphanumeric': 0.1282098822656902, 'fraction_numerical': 0.004727913228886623, 'mean_word_length': 1.9188311688311688, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 39, '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': '2773933', 'n_tokens_mistral': 2670, 'n_tokens_neox': 2573, 'n_words': 441}