|
| 1 | + |
| 2 | +#include "stdafx.h" |
| 3 | + |
| 4 | +#include "MidiEndpointCreationThreadTests.h" |
| 5 | + |
| 6 | + |
| 7 | +#include <wil\resource.h> |
| 8 | + |
| 9 | + |
| 10 | +#define NUM_MESSAGES_TO_TRANSMIT 10 |
| 11 | + |
| 12 | +_Use_decl_annotations_ |
| 13 | +void MidiEndpointCreationThreadTests::ReceiveThreadWorker(MidiSession session, winrt::hstring endpointId) |
| 14 | +{ |
| 15 | + wil::unique_event_nothrow allMessagesReceived; |
| 16 | + allMessagesReceived.create(); |
| 17 | + |
| 18 | + uint32_t countMessagesReceived = 0; |
| 19 | + |
| 20 | + auto connectionThreadId = GetCurrentThreadId(); |
| 21 | + std::cout << "Receiver: connection thread: " << connectionThreadId << std::endl; |
| 22 | + |
| 23 | + // create the connection |
| 24 | + auto connection = session.CreateEndpointConnection(endpointId); |
| 25 | + |
| 26 | + auto MessageReceivedHandler = [&](IMidiMessageReceivedEventSource const& sender, MidiMessageReceivedEventArgs const& args) |
| 27 | + { |
| 28 | + UNREFERENCED_PARAMETER(sender); |
| 29 | + UNREFERENCED_PARAMETER(args); |
| 30 | + |
| 31 | + auto callbackThreadId = GetCurrentThreadId(); |
| 32 | + std::cout << "Message received on callback thread: " << callbackThreadId << std::endl; |
| 33 | + |
| 34 | + countMessagesReceived++; |
| 35 | + |
| 36 | + // this will not be true. The thread receiving messages is a high-priority thread the application does not control. |
| 37 | + //VERIFY_ARE_EQUAL(connectionThreadId, callbackThreadId); |
| 38 | + |
| 39 | + if (countMessagesReceived >= NUM_MESSAGES_TO_TRANSMIT) |
| 40 | + { |
| 41 | + allMessagesReceived.SetEvent(); |
| 42 | + } |
| 43 | + |
| 44 | + }; |
| 45 | + |
| 46 | + auto revoke = connection.MessageReceived(MessageReceivedHandler); |
| 47 | + std::cout << "Receiver: Event handler created." << std::endl; |
| 48 | + |
| 49 | + connection.Open(); |
| 50 | + std::cout << "Receiver: Connection opened." << std::endl; |
| 51 | + |
| 52 | + m_receiverReady.SetEvent(); |
| 53 | + |
| 54 | + std::cout << "Receiver: Waiting for messages." << std::endl; |
| 55 | + |
| 56 | + if (!allMessagesReceived.wait(10000)) |
| 57 | + { |
| 58 | + std::cout << "Receiver: Failure waiting for receiver to connect." << std::endl; |
| 59 | + |
| 60 | + VERIFY_FAIL(); |
| 61 | + } |
| 62 | + |
| 63 | + std::cout << "Receiver: " << countMessagesReceived << " messages received." << std::endl; |
| 64 | + |
| 65 | + m_receiveComplete.SetEvent(); |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +_Use_decl_annotations_ |
| 70 | +void MidiEndpointCreationThreadTests::SendThreadWorker(MidiSession session, winrt::hstring endpointId) |
| 71 | +{ |
| 72 | + auto threadId = GetCurrentThreadId(); |
| 73 | + |
| 74 | + std::cout << "Sender thread: " << threadId << std::endl; |
| 75 | + |
| 76 | + // create the connection |
| 77 | + auto connection = session.CreateEndpointConnection(endpointId); |
| 78 | + connection.Open(); |
| 79 | + |
| 80 | + for (uint32_t i = 0; i < NUM_MESSAGES_TO_TRANSMIT; i++) |
| 81 | + { |
| 82 | + connection.SendMessageWords(MidiClock::TimestampConstantSendImmediately(), 0x21234567); |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + std::cout << "Sender: " << NUM_MESSAGES_TO_TRANSMIT << " messages sent" << std::endl; |
| 87 | + |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +void MidiEndpointCreationThreadTests::TestCreateNewSessionMultithreaded() |
| 93 | +{ |
| 94 | + m_receiveComplete.create(); |
| 95 | + m_receiverReady.create(); |
| 96 | + |
| 97 | + auto sessionThreadId = GetCurrentThreadId(); |
| 98 | + |
| 99 | + std::cout << "Session thread: " << sessionThreadId << std::endl; |
| 100 | + |
| 101 | + // create session on this thread |
| 102 | + |
| 103 | + auto session = MidiSession::CreateSession(L"Multi-threaded Test"); |
| 104 | + |
| 105 | + // create loopback A on thread A |
| 106 | + std::thread workerThreadA(&MidiEndpointCreationThreadTests::ReceiveThreadWorker, this, session, MidiEndpointDeviceInformation::DiagnosticsLoopbackAEndpointId()); |
| 107 | + workerThreadA.detach(); |
| 108 | + |
| 109 | + if (!m_receiverReady.wait(10000)) |
| 110 | + { |
| 111 | + std::cout << "Session: Failure waiting for receiver to connect." << std::endl; |
| 112 | + |
| 113 | + VERIFY_FAIL(); |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + std::cout << "Session: Receiver ready notification received." << std::endl; |
| 118 | + } |
| 119 | + |
| 120 | + // create loopback B on thread B |
| 121 | + std::thread workerThreadB(&MidiEndpointCreationThreadTests::SendThreadWorker, this, session, MidiEndpointDeviceInformation::DiagnosticsLoopbackBEndpointId()); |
| 122 | + workerThreadB.detach(); |
| 123 | + |
| 124 | + if (!m_receiveComplete.wait(20000)) |
| 125 | + { |
| 126 | + std::cout << "Session: Failure waiting for all messages to be received." << std::endl; |
| 127 | + |
| 128 | + VERIFY_FAIL(); |
| 129 | + } |
| 130 | + else |
| 131 | + { |
| 132 | + std::cout << "Session: Message receive complete notification received." << std::endl; |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | +} |
| 137 | + |
| 138 | + |
| 139 | + |
0 commit comments