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

namespace ProcessSample.Test.Unit
{
    using System;
    using System.Threading;
    using System.Threading.Tasks;
    using Xunit;

    public class ProcessTest
    {
        public ProcessTest()
        {
        }

        [Fact]
        public void Subscribes_to_Exited_event_on_construction()
        {
            ProcessStub inner = new ProcessStub();
            Assert.Equal(0, inner.ExitedSubscriberCount);
            Assert.False(inner.EnableRaisingEvents);

            ProcessEx process = new ProcessEx(inner);
            Assert.Equal(1, inner.ExitedSubscriberCount);
            Assert.True(inner.EnableRaisingEvents);
        }

        [Fact]
        public void Unsubscribes_from_Exited_event_on_Dispose()
        {
            ProcessStub inner = new ProcessStub();

            using (ProcessEx process = new ProcessEx(inner))
            {
            }

            Assert.Equal(0, inner.ExitedSubscriberCount);
            Assert.False(inner.EnableRaisingEvents);
        }

        [Fact]
        public void WaitForExit_completes_after_exit()
        {
            ProcessStub inner = new ProcessStub();
            ProcessEx process = new ProcessEx(inner);

            Task task = process.WaitForExitAsync(CancellationToken.None);
            Assert.False(task.IsCompleted);

            inner.RaiseExited();

            Assert.Equal(TaskStatus.RanToCompletion, task.Status);
        }

        [Fact]
        public void WaitForExit_completes_sync_if_exited_before_subscribed()
        {
            ProcessStub inner = new ProcessStub();
            inner.HasExited = true;
            ProcessEx process = new ProcessEx(inner);

            Task task = process.WaitForExitAsync(CancellationToken.None);

            Assert.Equal(TaskStatus.RanToCompletion, task.Status);
        }

        [Fact]
        public void Race_with_exit_and_subscribe_does_not_cause_errors()
        {
            ProcessStub inner = new ProcessStub();
            inner.Subscribed += delegate(object sender, EventArgs e)
            {
                inner.RaiseExited();
                inner.HasExited = true;
            };

            ProcessEx process = new ProcessEx(inner);

            Task task = process.WaitForExitAsync(CancellationToken.None);

            Assert.Equal(TaskStatus.RanToCompletion, task.Status);
        }

        [Fact]
        public void WaitForExit_is_canceled_if_token_requests_cancellation()
        {
            ProcessStub inner = new ProcessStub();
            ProcessEx process = new ProcessEx(inner);

            using (CancellationTokenSource cts = new CancellationTokenSource())
            {
                Task task = process.WaitForExitAsync(cts.Token);
                Assert.False(task.IsCompleted);

                cts.Cancel();

                Assert.True(task.IsCanceled);
            }
        }

        [Fact]
        public void WaitForExit_is_canceled_immediately_if_token_is_already_canceled()
        {
            ProcessStub inner = new ProcessStub();
            ProcessEx process = new ProcessEx(inner);

            using (CancellationTokenSource cts = new CancellationTokenSource())
            {
                cts.Cancel();
                Task task = process.WaitForExitAsync(cts.Token);

                Assert.True(task.IsCanceled);
            }
        }
<commit_msg>WaitForExit completes successfully if already exited even if token is already canceled<commit_after>﻿//-----------------------------------------------------------------------
// <copyright file="ProcessTest.cs" company="<NAME>">
// Copyright (c) <NAME>. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace ProcessSample.Test.Unit
{
    using System;
    using System.Threading;
    using System.Threading.Tasks;
    using Xunit;

    public class ProcessTest
    {
        public ProcessTest()
        {
        }

        [Fact]
        public void Subscribes_to_Exited_event_on_construction()
        {
            ProcessStub inner = new ProcessStub();
            Assert.Equal(0, inner.ExitedSubscriberCount);
            Assert.False(inner.EnableRaisingEvents);

            ProcessEx process = new ProcessEx(inner);
            Assert.Equal(1, inner.ExitedSubscriberCount);
            Assert.True(inner.EnableRaisingEvents);
        }

        [Fact]
        public void Unsubscribes_from_Exited_event_on_Dispose()
        {
            ProcessStub inner = new ProcessStub();

            using (ProcessEx process = new ProcessEx(inner))
            {
            }

            Assert.Equal(0, inner.ExitedSubscriberCount);
            Assert.False(inner.EnableRaisingEvents);
        }

        [Fact]
        public void WaitForExit_completes_after_exit()
        {
            ProcessStub inner = new ProcessStub();
            ProcessEx process = new ProcessEx(inner);

            Task task = process.WaitForExitAsync(CancellationToken.None);
            Assert.False(task.IsCompleted);

            inner.RaiseExited();

            Assert.Equal(TaskStatus.RanToCompletion, task.Status);
        }

        [Fact]
        public void WaitForExit_completes_sync_if_exited_before_subscribed()
        {
            ProcessStub inner = new ProcessStub();
            inner.HasExited = true;
            ProcessEx process = new ProcessEx(inner);

            Task task = process.WaitForExitAsync(CancellationToken.None);

            Assert.Equal(TaskStatus.RanToCompletion, task.Status);
        }

        [Fact]
        public void Race_with_exit_and_subscribe_does_not_cause_errors()
        {
            ProcessStub inner = new ProcessStub();
            inner.Subscribed += delegate(object sender, EventArgs e)
            {
                inner.RaiseExited();
                inner.HasExited = true;
            };

            ProcessEx process = new ProcessEx(inner);

            Task task = process.WaitForExitAsync(CancellationToken.None);

            Assert.Equal(TaskStatus.RanToCompletion, task.Status);
        }

        [Fact]
        public void WaitForExit_is_canceled_if_token_requests_cancellation()
        {
            ProcessStub inner = new ProcessStub();
            ProcessEx process = new ProcessEx(inner);

            using (CancellationTokenSource cts = new CancellationTokenSource())
            {
                Task task = process.WaitForExitAsync(cts.Token);
                Assert.False(task.IsCompleted);

                cts.Cancel();

                Assert.True(task.IsCanceled);
            }
        }

        [Fact]
        public void WaitForExit_is_canceled_immediately_if_token_is_already_canceled()
        {
            ProcessStub inner = new ProcessStub();
            ProcessEx process = new ProcessEx(inner);

            using (CancellationTokenSource cts = new CancellationTokenSource())
            {
                cts.Cancel();
                Task task = process.WaitForExitAsync(cts.Token);

                Assert.True(task.IsCanceled);
            }
        }

        [Fact]
        public void WaitForExit_completes_successfully_if_already_exited_even_if_token_is_already_canceled()
        {
            ProcessStub inner = new ProcessStub();
            inner.HasExited = true;
            ProcessEx process = new ProcessEx(inner);

            using (CancellationTokenSource cts = new CancellationTokenSource())
            {
                cts.Cancel();
                Task task = process.WaitForExitAsync(cts.Token);

                Assert.Equal(TaskStatus.RanToCompletion, task.Status);
            }
        }

{'fraction_non_alphanumeric': 0.11673200961173644, 'fraction_numerical': 0.0007588212975844188, 'mean_word_length': 1.71939477303989, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '6720620', 'n_tokens_mistral': 2020, 'n_tokens_neox': 2002, 'n_words': 372}