1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
/**
* (c) 2013 by Mega Limited, Auckland, New Zealand
*
* This file is part of MEGAcmd.
*
* MEGAcmd is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @copyright Simplified (2-clause) BSD License.
*
* You should have received a copy of the license along with this
* program.
*/
#include <gtest/gtest.h>
#include "megacmd.h"
#include "megacmdlogger.h"
#include "megacmd_rotating_logger.h"
#include "Instruments.h"
#include "TestUtils.h"
#ifdef __linux__
#include <sched.h>
#endif
#include <chrono>
#include <cassert>
#ifndef UNUSED
#define UNUSED(x) (void)(x)
#endif
#ifdef __linux__
static void setUpUnixSignals()
{
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGPIPE);
pthread_sigmask(SIG_BLOCK, &set, NULL);
}
#endif
int main (int argc, char *argv[])
{
#ifdef __linux__
if (getenv("MEGA_INTEGRATION_TEST_ENFORCE_SINGLE_CPU"))
{
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(1, &set);
if (sched_setaffinity(getpid(), sizeof(set), &set) == -1)
{
std::cerr << "sched_setaffinity error: " << errno << std::endl;
exit(errno);
}
}
setUpUnixSignals();
#endif
testing::InitGoogleTest(&argc, argv);
#ifdef WIN32
megacmd::Instance<megacmd::WindowsConsoleController> windowsConsoleController;
// Set custom gtests event listener to control the output to stdout
::testing::TestEventListeners& listeners =
::testing::UnitTest::GetInstance()->listeners();
auto customListener = new CustomTestEventListener();
customListener->mDefault.reset(listeners.Release(listeners.default_result_printer()));
listeners.Append(customListener);
#endif
using TI = TestInstruments;
std::promise<void> serverWaitingPromise;
TI::Instance().onEventOnce(TI::Event::SERVER_ABOUT_TO_START_WAITING_FOR_PETITIONS,
[&serverWaitingPromise]() {
serverWaitingPromise.set_value();
});
std::thread serverThread([] {
std::vector<char*> args{
(char *)"argv0_INTEGRATION_TESTS",
nullptr
};
megacmd::LogConfig logConfig;
logConfig.mSdkLogLevel = mega::MegaApi::LOG_LEVEL_MAX;
logConfig.mCmdLogLevel = mega::MegaApi::LOG_LEVEL_MAX;
logConfig.mLogToCout = false;
logConfig.mJsonLogs = true;
auto createDefaultStream = [] { return new megacmd::FileRotatingLoggedStream(megacmd::MegaCmdLogger::getDefaultFilePath()); };
megacmd::executeServer(1, args.data(), createDefaultStream, logConfig);
});
auto waitReturn = serverWaitingPromise.get_future().wait_for(std::chrono::seconds(10));
UNUSED(waitReturn);
assert(waitReturn != std::future_status::timeout);
auto exitCode = RUN_ALL_TESTS();
megacmd::stopServer();
serverThread.join();
#ifdef _WIN32
// We use a file to pass the exit code to Jenkins,
// since it fails to get the actual value otherwise
std::ofstream("exit_code.txt") << exitCode;
#endif
return exitCode;
}
|