diff --git a/src/megacmd.cpp b/src/megacmd.cpp index 793a3fe0..82c25424 100644 --- a/src/megacmd.cpp +++ b/src/megacmd.cpp @@ -29,6 +29,11 @@ #include "megacmdplatform.h" #include "megacmdversion.h" +#ifdef MEGACMD_TESTING_CODE + #include "../tests/common/Instruments.h" +#endif + + #define USE_VARARGS #define PREFER_STDARG @@ -4185,6 +4190,10 @@ void megacmd() LOG_info << "Listening to petitions ... "; +#ifdef MEGACMD_TESTING_CODE + TestInstruments::Instance().fireEvent(TestInstruments::Event::SERVER_ABOUT_TO_START_WAITING_FOR_PETITIONS); +#endif + for (;; ) { cm->waitForPetition(); diff --git a/tests/common/Instruments.h b/tests/common/Instruments.h index a7b50b32..6a4cd2af 100644 --- a/tests/common/Instruments.h +++ b/tests/common/Instruments.h @@ -64,7 +64,7 @@ public: // enum class Event { - SOME_MEGACMD_INSTRUMENT_EVENT, + SERVER_ABOUT_TO_START_WAITING_FOR_PETITIONS, }; typedef std::function EventCallback; diff --git a/tests/integration/BasicTests.cpp b/tests/integration/BasicTests.cpp index f2463694..885d2e43 100644 --- a/tests/integration/BasicTests.cpp +++ b/tests/integration/BasicTests.cpp @@ -22,25 +22,37 @@ #include #include "Instruments.h" +#include +#include + class BasicGenericTest : public ::testing::Test { + std::thread mServerThread; void SetUp() override { - std::thread t([](){ + mServerThread = std::thread([](){ char **args = new char*[2]; args[0]=(char *)"argv0_INTEGRATION_TESTS"; args[1] = NULL; megacmd::executeServer(1, args); }); - megacmd::sleepSeconds(2); //TODO: have this properly orchestrated + using TI = TestInstruments; - t.detach(); + std::promise serverWaitingPromise; + TI::Instance().onEventOnce(TI::Event::SERVER_ABOUT_TO_START_WAITING_FOR_PETITIONS, + [&serverWaitingPromise]() { + serverWaitingPromise.set_value(); + }); + + ASSERT_NE(serverWaitingPromise.get_future().wait_for(std::chrono::seconds(10)) + , std::future_status::timeout); } void TearDown() override { megacmd::stopServer(); + mServerThread.join(); } };