<commit_before>using System;
using System.Threading;
using Microsoft.AspNetCore.Server.Kestrel.Internal.Networking;

namespace Microsoft.AspNetCore.Server.KestrelTests.TestHelpers
{
    public class MockLibuv : Libuv
    {
        private UvAsyncHandle _postHandle;
        private uv_async_cb _onPost;

        private bool _stopLoop;
        private readonly ManualResetEventSlim _loopWh = new ManualResetEventSlim();

        unsafe public MockLibuv()
            : base(onlyForTesting: true)
        {
            OnWrite = (socket, buffers, triggerCompleted) =>
            {
                triggerCompleted(0);
                return 0;
            };

            _uv_write = UvWrite;

            _uv_async_send = postHandle =>
            {
                PostCount++;
                _loopWh.Set();

                return 0;
            };

            _uv_async_init = (loop, postHandle, callback) =>
            {
                _postHandle = postHandle;
                _onPost = callback;

                return 0;
            };

            _uv_run = (loopHandle, mode) =>
            {
                while (!_stopLoop)
                {
                    _loopWh.Wait();
                    _loopWh.Reset();
                    _onPost(_postHandle.InternalGetHandle());
                }

                return 0;
            };

            _uv_ref = handle => { };
            _uv_unref = handle =>
            {
                _stopLoop = true;
                _loopWh.Set();
            };

            _uv_stop = handle =>
            {
                _stopLoop = true;
                _loopWh.Set();
            };

            _uv_req_size = reqType => IntPtr.Size;
            _uv_loop_size = () => IntPtr.Size;
            _uv_handle_size = handleType => IntPtr.Size;
            _uv_loop_init = loop => 0;
            _uv_tcp_init = (loopHandle, tcpHandle) => 0;
            _uv_close = (handle, callback) => callback(handle);
            _uv_loop_close = handle => 0;
            _uv_walk = (loop, callback, ignore) => 0;
            _uv_err_name = errno => IntPtr.Zero;
            _uv_strerror = errno => IntPtr.Zero;
            _uv_read_start = UvReadStart;
            _uv_read_stop = handle => 0;
        }

        public Func<UvStreamHandle, int, Action<int>, int> OnWrite { get; set; }
<commit_msg>Trace call to _uv_unsafe_async_send in MockLibuv.<commit_after>// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Threading;
using Microsoft.AspNetCore.Server.Kestrel.Internal.Networking;

namespace Microsoft.AspNetCore.Server.KestrelTests.TestHelpers
{
    public class MockLibuv : Libuv
    {
        private UvAsyncHandle _postHandle;
        private uv_async_cb _onPost;

        private bool _stopLoop;
        private readonly ManualResetEventSlim _loopWh = new ManualResetEventSlim();

        private readonly string _stackTrace;

        unsafe public MockLibuv()
            : base(onlyForTesting: true)
        {
            _stackTrace = Environment.StackTrace;

            OnWrite = (socket, buffers, triggerCompleted) =>
            {
                triggerCompleted(0);
                return 0;
            };

            _uv_write = UvWrite;

            _uv_async_send = postHandle =>
            {
                PostCount++;
                _loopWh.Set();

                return 0;
            };

            _uv_async_init = (loop, postHandle, callback) =>
            {
                _postHandle = postHandle;
                _onPost = callback;

                return 0;
            };

            _uv_run = (loopHandle, mode) =>
            {
                while (!_stopLoop)
                {
                    _loopWh.Wait();
                    _loopWh.Reset();
                    _onPost(_postHandle.InternalGetHandle());
                }

                return 0;
            };

            _uv_ref = handle => { };
            _uv_unref = handle =>
            {
                _stopLoop = true;
                _loopWh.Set();
            };

            _uv_stop = handle =>
            {
                _stopLoop = true;
                _loopWh.Set();
            };

            _uv_req_size = reqType => IntPtr.Size;
            _uv_loop_size = () => IntPtr.Size;
            _uv_handle_size = handleType => IntPtr.Size;
            _uv_loop_init = loop => 0;
            _uv_tcp_init = (loopHandle, tcpHandle) => 0;
            _uv_close = (handle, callback) => callback(handle);
            _uv_loop_close = handle => 0;
            _uv_walk = (loop, callback, ignore) => 0;
            _uv_err_name = errno => IntPtr.Zero;
            _uv_strerror = errno => IntPtr.Zero;
            _uv_read_start = UvReadStart;
            _uv_read_stop = handle => 0;
            _uv_unsafe_async_send = handle =>
            {
                throw new Exception($"Why is this getting called?{Environment.NewLine}{_stackTrace}");
            };
        }

        public Func<UvStreamHandle, int, Action<int>, int> OnWrite { get; set; }

{'fraction_non_alphanumeric': 0.08988981248791804, 'fraction_numerical': 0.004252851343514402, 'mean_word_length': 1.3909426987060998, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 44, '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': '2371579', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1374, 'n_words': 294}