JD2022-TU1/main/extern/UbiServices/custom/tests/fixture/memoryCheckFixture.h

190 lines
5.5 KiB
C++

////////////////////////////////////////////////////////////////////////////////
// (c) Copyright, Ubisoft Entertainment Inc. || Created by Harbour //
////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "tests/config.h"
#include "tests/common/helpers.h"
#include <ubiservices/core/configs/config.h>
#include <ubiservices/services/http/httpConfig.h>
#include <usCommon/settings.h>
#define US_TESTING_ALLOC
#if defined(US_ENABLE_ALLOC_STATS) && defined(US_TESTING_ALLOC)
#define SAVE_ALLOC_COUNT(_var) size_t _var = g_stats.m_allocCountTotal
#else
#define SAVE_ALLOC_COUNT(_var)
#endif
#if defined(US_ENABLE_ALLOC_STATS) && defined(US_TESTING_ALLOC)
#define EXPECT_ALLOC_COUNT_EQ(_value, _count) EXPECT_EQ(_value, g_stats.m_allocCountTotal - _count); _count = g_stats.m_allocCountTotal;
#if defined(US_TARGET_WINDOWS)
#define EXPECT_ALLOC_COUNT_PC_EQ(_value, _count) EXPECT_EQ(_value, g_stats.m_allocCountTotal - _count); _count = g_stats.m_allocCountTotal;
#define EXPECT_ALLOC_COUNT_PC_X1_EQ(_valuePC, _valueX1, _count) EXPECT_EQ(_valuePC, g_stats.m_allocCountTotal - _count); _count = g_stats.m_allocCountTotal;
#elif defined(US_TARGET_XONE)
#define EXPECT_ALLOC_COUNT_PC_EQ(_value, _count) US_UNUSED(_count);
#define EXPECT_ALLOC_COUNT_PC_X1_EQ(_valuePC, _valueX1, _count) EXPECT_EQ(_valueX1, g_stats.m_allocCountTotal - _count); _count = g_stats.m_allocCountTotal;
#else
#define EXPECT_ALLOC_COUNT_PC_EQ(_value, _count) US_UNUSED(_count);
#endif
#else
#define EXPECT_ALLOC_COUNT_EQ(_value, _count)
#define EXPECT_ALLOC_COUNT_PC_EQ(_value, _count)
#define EXPECT_ALLOC_COUNT_PC_X1_EQ(_valuePC, _valueX1, _count)
#endif
namespace US_NS {;
class AsyncResultBase;
class EventLog;
class Facade;
class FacadeInternal;
const char* const CUSTOM_EVENT_NAME = "Testing";
#if !defined(US_SDK_S2S)
namespace WebsocketHelper
{
// Waits for the websocket connection to be active
::testing::AssertionResult waitForWebsocketInit(Facade& facade);
}
#endif
class LeakChecker : public ::testing::Test
{
public:
enum FixtureType
{
DontExecuteInit,
AutoExecuteInit,
InitNoScheduler
};
protected:
LeakChecker(FixtureType type = AutoExecuteInit);
virtual ~LeakChecker();
EventLog* getEventLog();
void outputLog(LogLevel logLevel, LogCategory logCategory, const char* string, const char* file, uint32 line);
void setLogLevel(LogLevel logLevel);
void resetLogLevel();
void setMultiLinesLog(bool state = true);
void setupDebugLogging();
// disables all log categories
void disableLogCategories();
// disables all log categories except the ones passed in the vector
void enableLogCategories(const Vector<LogCategory>& categories);
friend struct LeakCheckerScopedInit;
virtual void initDefaultSDK();
virtual void uninitDefaultSDK();
static FacadeInternal& getFacadeInternal(Facade& facade);
static const FacadeInternal& getFacadeInternal(const Facade& facade);
protected:
void SetUp() override;
void TearDown() override;
bool m_mightLeak;
bool m_willLeak;
const FixtureType m_type;
bool m_skipEalMemForLibcurl;
#if defined(US_ENABLE_ALLOC_STATS)
AllocStatistics m_currentTestAllocStatistics;
#endif
private:
virtual HttpConfig getHttpConfig() const;
const char* getTestName() const;
const char* getTestCaseName() const;
};
struct LeakCheckerScopedInit
{
LeakCheckerScopedInit(LeakChecker& testInstance);
~LeakCheckerScopedInit();
private:
LeakChecker & m_testInstance;
};
class LeakCheckerNoLibcurlEalMem : public LeakChecker
{
#if defined(US_USE_LIBCURL)
protected:
void SetUp() override;
#endif
};
class LeakCheckerNoInit : public LeakChecker
{
public:
LeakCheckerNoInit() : LeakChecker(LeakChecker::DontExecuteInit)
{
}
};
class LeakCheckerNoInitSkipLibCurlEalMem : public LeakCheckerNoInit
{
protected:
void SetUp() override
{
m_skipEalMemForLibcurl = true;
LeakCheckerNoInit::SetUp();
}
};
class LeakCheckerNoScheduler : public LeakChecker
{
public:
LeakCheckerNoScheduler() : LeakChecker(LeakChecker::InitNoScheduler)
{
}
};
// Better to use the typedefs below
template<bool init, bool useProxy = false, bool skipEalMemForLibcurl = false>
class LeakCheckerHttpClientImpl : public LeakChecker
{
protected:
LeakCheckerHttpClientImpl()
: LeakChecker(init ? LeakChecker::AutoExecuteInit : LeakChecker::DontExecuteInit)
, m_useProxy(useProxy)
{
}
protected:
void SetUp() override
{
m_skipEalMemForLibcurl = skipEalMemForLibcurl;
LeakChecker::SetUp();
}
private:
HttpConfig getHttpConfig() const override
{
HttpConfig httpConfig;
if (m_useProxy) // This is to avoid conditional expression is constant
{
#if defined(US_TEST_ENABLE_PROXY)
// IMPORTANT: The following does NOT apply to US_TARGET_IOS nor US_TARGET_ANDROID
httpConfig.m_proxy = HttpProxyConfig(US_TEST_NS::PROXY_NAME, US_TEST_NS::PROXY_PORT, String(), String(), HttpProxyConfig::Type::Enabled);
#endif
}
return httpConfig;
}
const bool m_useProxy;
};
typedef LeakCheckerHttpClientImpl<true> LeakCheckerHttpClient;
typedef LeakCheckerHttpClientImpl<true, false, true> LeakCheckerHttpClientSkipLibCurlEalMem;
typedef LeakCheckerHttpClientImpl<true, true> LeakCheckerHttpClientProxy;
typedef LeakCheckerHttpClientImpl<false> LeakCheckerHttpClientNoInit;
typedef LeakCheckerHttpClientImpl<false, false, true> LeakCheckerHttpClientNoInitSkipLibCurlEalMem;
};