Repository: microsoft/terminal
File: src/host/ft_host/API_MultipleInflightMessageTests.cpp (C++)

Code before:

        const auto inputHandle = GetStdInputHandle();
        const auto outputHandle = GetStdOutputHandle();

        DWORD originalConsoleMode{};
        VERIFY_WIN32_BOOL_SUCCEEDED(GetConsoleMode(inputHandle, &originalConsoleMode));
        auto restoreMode{ wil::scope_exit([=]() {
            SetConsoleMode(inputHandle, originalConsoleMode);
        }) };
        VERIFY_WIN32_BOOL_SUCCEEDED(SetConsoleMode(inputHandle, ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT));

        // Prime the console with some fake input records.
        const std::array<INPUT_RECORD, 4> inputRecords{
            INPUT_RECORD{ KEY_EVENT, KEY_EVENT_RECORD{ TRUE, 1, 'h', 0, L'h', 0 } },
            INPUT_RECORD{ KEY_EVENT, KEY_EVENT_RECORD{ FALSE, 1, 'h', 0, L'h', 0 } },
            INPUT_RECORD{ KEY_EVENT, KEY_EVENT_RECORD{ TRUE, 1, 'i', 0, L'i', 0 } },
            INPUT_RECORD{ KEY_EVENT, KEY_EVENT_RECORD{ FALSE, 1, 'i', 0, L'i', 0 } },
        };
        DWORD written{};
        VERIFY_WIN32_BOOL_SUCCEEDED(WriteConsoleInput(inputHandle, inputRecords.data(), static_cast<DWORD>(inputRecords.size()), &written));
        VERIFY_ARE_EQUAL(inputRecords.size(), written);

        // !!! LOAD BEARING !!!
        // This buffer needs to be large enough to force API_MSG to heap allocate (!)
        std::array<wchar_t, 129> buffer;
        DWORD read{};

        bool readerThreadLaunched{ false };
        std::condition_variable readerThreadLaunchCV;
        std::mutex readerThreadLaunchMutex;
        std::thread readerThread{ [&]() {
            WEX::TestExecution::DisableVerifyExceptions disableVerifyExceptions{};
            {
                std::scoped_lock lock{ readerThreadLaunchMutex };
                readerThreadLaunched = true;
            }
            readerThreadLaunchCV.notify_all();
            VERIFY_WIN32_BOOL_SUCCEEDED(ReadConsoleW(inputHandle, buffer.data(), static_cast<DWORD>(buffer.size()), &read, nullptr));
        } };

        {
            std::unique_lock lock{ readerThreadLaunchMutex };
            readerThreadLaunchCV.wait(lock, [&]() { return readerThreadLaunched; });
            // should not progress until readerThreadLaunched is set.
        }
        Sleep(50); // Yeah, it's not science.

        std::thread writerThread{ [&]() {
            WEX::TestExecution::DisableVerifyExceptions disableVerifyExceptions{};
            DWORD temp{};
            // !!! LOAD BEARING !!!

Review comment (suggestion):
We could make the `128` a public constant of the API_MSG struct and use it here.

Code after:
        using namespace std::string_view_literals;

        const auto inputHandle = GetStdInputHandle();
        const auto outputHandle = GetStdOutputHandle();

        DWORD originalConsoleMode{};
        VERIFY_WIN32_BOOL_SUCCEEDED(GetConsoleMode(inputHandle, &originalConsoleMode));
        auto restoreMode{ wil::scope_exit([=]() {
            SetConsoleMode(inputHandle, originalConsoleMode);
        }) };
        VERIFY_WIN32_BOOL_SUCCEEDED(SetConsoleMode(inputHandle, ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT));

        // Prime the console with some fake input records.
        const std::array<INPUT_RECORD, 4> inputRecords{
            INPUT_RECORD{ KEY_EVENT, KEY_EVENT_RECORD{ TRUE, 1, 'h', 0, L'h', 0 } },
            INPUT_RECORD{ KEY_EVENT, KEY_EVENT_RECORD{ FALSE, 1, 'h', 0, L'h', 0 } },
            INPUT_RECORD{ KEY_EVENT, KEY_EVENT_RECORD{ TRUE, 1, 'i', 0, L'i', 0 } },
            INPUT_RECORD{ KEY_EVENT, KEY_EVENT_RECORD{ FALSE, 1, 'i', 0, L'i', 0 } },
        };
        DWORD written{};
        VERIFY_WIN32_BOOL_SUCCEEDED(WriteConsoleInput(inputHandle, inputRecords.data(), static_cast<DWORD>(inputRecords.size()), &written));
        VERIFY_ARE_EQUAL(inputRecords.size(), written);

        // !!! LOAD BEARING !!!
        // This buffer needs to be large enough to force API_MSG to heap allocate (!)
        std::array<wchar_t, 129> buffer;
        DWORD read{};

        bool readerThreadLaunched{ false };
        std::condition_variable readerThreadLaunchCV;
        std::mutex readerThreadLaunchMutex;

        std::unique_lock lock{ readerThreadLaunchMutex };

        std::thread readerThread{ [&]() {
            WEX::TestExecution::DisableVerifyExceptions disableVerifyExceptions{};
            {
                std::scoped_lock lock{ readerThreadLaunchMutex };
                readerThreadLaunched = true;
            }
            readerThreadLaunchCV.notify_all();
            VERIFY_WIN32_BOOL_SUCCEEDED(ReadConsoleW(inputHandle, buffer.data(), static_cast<DWORD>(buffer.size()), &read, nullptr));
        } };

        // CV wait should allow readerThread to progress
        readerThreadLaunchCV.wait(lock, [&]() { return readerThreadLaunched; });
        // should not progress until readerThreadLaunched is set.
        Sleep(50); // Yeah, it's not science.

        std::thread writerThread{ [&]() {
            WEX::TestExecution::DisableVerifyExceptions disableVerifyExceptions{};