<commit_before>﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.IO.Pipes;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CommandLine;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.CompilerServer.UnitTests
{
    public class NamedPipeClientConnectionHostTests : IDisposable
    {
        private readonly NamedPipeClientConnectionHost _host;

        public NamedPipeClientConnectionHostTests()
        {
            _host = new NamedPipeClientConnectionHost(ServerUtil.GetPipeName());
        }

        public void Dispose()
        {
            if (_host.IsListening)
            {
                _host.EndListening();
            }

            Assert.True(NamedPipeTestUtil.IsPipeFullyClosed(_host.PipeName));
        }

        private Task<NamedPipeClientStream> ConnectAsync(CancellationToken cancellationToken = default) => BuildServerConnection.TryConnectToServerAsync(
            _host.PipeName,
            timeoutMs: (int)(TimeSpan.FromMinutes(1).TotalMilliseconds),
            cancellationToken);

        [Fact]
        public async Task CallBeforeListen()
        {
            await Assert.ThrowsAsync<InvalidOperationException>(() => _host.GetNextClientConnectionAsync()).ConfigureAwait(false);
        }

        [ConditionalFact(typeof(WindowsOnly), Reason = "https://github.com/dotnet/runtime/issues/40301")]
        public async Task CallAfterComplete()
        {
            _host.BeginListening();
            var task = _host.GetNextClientConnectionAsync();
            using var clientStream = await ConnectAsync().ConfigureAwait(false);
            await task.ConfigureAwait(false);
            Assert.NotNull(_host.GetNextClientConnectionAsync());
            _host.EndListening();
        }

        [Fact]
        public void EndListenCancelsIncompleteTask()
        {
            _host.BeginListening();
            var task = _host.GetNextClientConnectionAsync();
            _host.EndListening();

            Assert.ThrowsAsync<OperationCanceledException>(() => task).ConfigureAwait(false);
        }

        /// <summary>
        /// It is the responsibility of the caller of <see cref="NamedPipeClientConnectionHost.GetNextClientConnectionAsync"/>
        /// to dispose the returned client, not the hosts
        /// </summary>
        [ConditionalFact(typeof(WindowsOnly), Reason = "https://github.com/dotnet/runtime/issues/40301")]
        public async Task EndListenDoesNotDisposeCompletedConnection()
        {
            _host.BeginListening();
            var task = _host.GetNextClientConnectionAsync();
            using var clientStream = await ConnectAsync().ConfigureAwait(false);
            using var namedPipeClientConnection = (NamedPipeClientConnection)(await task.ConfigureAwait(false));
            _host.EndListening();
            Assert.False(namedPipeClientConnection.IsDisposed);
        }

        /// <summary>
        /// Ensure that the host can handle many connections before they are acknowledged / dequeued
        /// by the caller.
        /// </summary>
        [ConditionalFact(typeof(WindowsOnly), Reason = "https://github.com/dotnet/runtime/issues/40301")]
        public async Task ManyConnectsBeforeAcknowledged()<commit_msg>Disable a few Mac tests<commit_after>﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.IO.Pipes;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CommandLine;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.CompilerServer.UnitTests
{
    public class NamedPipeClientConnectionHostTests : IDisposable
    {
        private readonly NamedPipeClientConnectionHost _host;

        public NamedPipeClientConnectionHostTests()
        {
            _host = new NamedPipeClientConnectionHost(ServerUtil.GetPipeName());
        }

        public void Dispose()
        {
            if (_host.IsListening)
            {
                _host.EndListening();
            }

            Assert.True(NamedPipeTestUtil.IsPipeFullyClosed(_host.PipeName));
        }

        private Task<NamedPipeClientStream> ConnectAsync(CancellationToken cancellationToken = default) => BuildServerConnection.TryConnectToServerAsync(
            _host.PipeName,
            timeoutMs: (int)(TimeSpan.FromMinutes(1).TotalMilliseconds),
            cancellationToken);

        [ConditionalFact(typeof(WindowsOrLinuxOnly), Reason = "https://github.com/dotnet/runtime/issues/40301")]
        public async Task CallBeforeListen()
        {
            await Assert.ThrowsAsync<InvalidOperationException>(() => _host.GetNextClientConnectionAsync()).ConfigureAwait(false);
        }

        [ConditionalFact(typeof(WindowsOnly), Reason = "https://github.com/dotnet/runtime/issues/40301")]
        public async Task CallAfterComplete()
        {
            _host.BeginListening();
            var task = _host.GetNextClientConnectionAsync();
            using var clientStream = await ConnectAsync().ConfigureAwait(false);
            await task.ConfigureAwait(false);
            Assert.NotNull(_host.GetNextClientConnectionAsync());
            _host.EndListening();
        }

        [ConditionalFact(typeof(WindowsOrLinuxOnly), Reason = "https://github.com/dotnet/runtime/issues/40301")]
        public void EndListenCancelsIncompleteTask()
        {
            _host.BeginListening();
            var task = _host.GetNextClientConnectionAsync();
            _host.EndListening();

            Assert.ThrowsAsync<OperationCanceledException>(() => task).ConfigureAwait(false);
        }

        /// <summary>
        /// It is the responsibility of the caller of <see cref="NamedPipeClientConnectionHost.GetNextClientConnectionAsync"/>
        /// to dispose the returned client, not the hosts
        /// </summary>
        [ConditionalFact(typeof(WindowsOnly), Reason = "https://github.com/dotnet/runtime/issues/40301")]
        public async Task EndListenDoesNotDisposeCompletedConnection()
        {
            _host.BeginListening();
            var task = _host.GetNextClientConnectionAsync();
            using var clientStream = await ConnectAsync().ConfigureAwait(false);
            using var namedPipeClientConnection = (NamedPipeClientConnection)(await task.ConfigureAwait(false));
            _host.EndListening();
            Assert.False(namedPipeClientConnection.IsDisposed);
        }

        /// <summary>
        /// Ensure that the host can handle many connections before they are acknowledged / dequeued
        /// by the caller.
        /// </summary>
        [ConditionalFact(typeof(WindowsOnly), Reason = "https://github.com/dotnet/runtime/issues/40301")]
        public async Task ManyConnectsBeforeAcknowledged()
{'fraction_non_alphanumeric': 0.09440267335004177, 'fraction_numerical': 0.005847953216374269, 'mean_word_length': 2.747000521648409, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 25, 'https://': 8, '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': '6818888', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1804, 'n_words': 385}