Skip to content

Commit

Permalink
Fix the socket file cleanup order bug in the test
Browse files Browse the repository at this point in the history
  • Loading branch information
cjr-mega committed Jan 6, 2025
1 parent 95a7dbf commit 7236016
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/gfx/isolatedprocess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ void AutoStartLauncher::stop()
{
// Defensive: the thread doesn't exit, detach the thread
// We had such bug and it is usually a bug
// assert(false && "AutoStartLauncher detaching loop thread");
assert(false && "AutoStartLauncher detaching loop thread");
LOG_warn << "AutoStartLauncher detaching loop thread";
mThread.detach();
}
Expand Down
91 changes: 50 additions & 41 deletions tests/integration/SdkTest_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,28 +265,30 @@ namespace
#endif
}

MegaApiTest* newMegaApi(const char* appKey,
const char* basePath,
const char* userAgent,
unsigned workerThreadCount,
const int clientType = MegaApi::CLIENT_TYPE_DEFAULT)
MegaApiTestPointer newMegaApi(const char* appKey,
const char* basePath,
const char* userAgent,
unsigned workerThreadCount,
const int clientType = MegaApi::CLIENT_TYPE_DEFAULT)
{
#ifdef ENABLE_ISOLATED_GFX
#ifdef ENABLE_ISOLATED_GFX
const auto gfxworkerPath = sdk_test::getTestDataDir() / executableName("gfxworker");
const auto endpointName = newEndpointName();
std::unique_ptr<MegaGfxProvider> provider{
MegaGfxProvider::createIsolatedInstance(endpointName.c_str(), gfxworkerPath.string().c_str())
};
return new MegaApiTest(endpointName,
appKey,
provider.get(),
basePath,
userAgent,
workerThreadCount,
clientType);
#else
return new MegaApiTest(appKey, basePath, userAgent, workerThreadCount, clientType);
#endif
return MegaApiTestPointer{new MegaApiTest(appKey,
provider.get(),
basePath,
userAgent,
workerThreadCount,
clientType),
MegaApiTestDeleter{endpointName}};
#else
return MegaApiTestPointer{
new MegaApiTest(appKey, basePath, userAgent, workerThreadCount, clientType),
MegaApiTestDeleter{""}};
#endif
}

enum class HasIcon
Expand Down Expand Up @@ -346,23 +348,33 @@ MegaApiTest::MegaApiTest(const char* appKey,
{
}

MegaApiTest::MegaApiTest(const std::string& endpointName,
const char* appKey,
MegaApiTest::MegaApiTest(const char* appKey,
MegaGfxProvider* provider,
const char* basePath,
const char* userAgent,
unsigned workerThreadCount,
const int clientType):
MegaApi(appKey, provider, basePath, userAgent, workerThreadCount, clientType),
mEndpointName(endpointName)
MegaApi(appKey, provider, basePath, userAgent, workerThreadCount, clientType)
{
}

MegaApiTest::~MegaApiTest()
MegaClient* MegaApiTest::getClient()
{
return pImpl->getMegaClient();
}

void MegaApiTestDeleter::operator()(MegaApiTest* p) const
{
delete p;

// Clean up the socket file if it has been created and only after MegaApiTest is deleted.
// Reason: the GfxIsolatedProcess is desctructed in the subclass MegaApi
// Another alernative is to clean up the socket file in the GfxIsolatedProcess destructor.
// However it might clean up a socket file created by a new GfxIsolatedProcess is a same
// name is used alghouth it seems be rare.
#if !defined(WIN32) && defined(ENABLE_ISOLATED_GFX)
// Clean up socket file if it has been created
if (mEndpointName.empty()) return;
if (mEndpointName.empty())
return;

if (std::error_code errorCode = SocketUtils::removeSocketFile(mEndpointName))
{
Expand All @@ -371,12 +383,6 @@ MegaApiTest::~MegaApiTest()
#endif
}

MegaClient* MegaApiTest::getClient()
{
return pImpl->getMegaClient();
}


void SdkTest::SetUp()
{
SdkTestBase::SetUp();
Expand Down Expand Up @@ -1614,11 +1620,11 @@ void SdkTest::configureTestInstance(unsigned index,
ASSERT_FALSE(mApi[index].pwd.empty()) << "Set test account " << index << " password at the environment variable $" << passVarName;
}

megaApi[index].reset(newMegaApi(APP_KEY.c_str(),
megaApiCacheFolder(static_cast<int>(index)).c_str(),
USER_AGENT.c_str(),
unsigned(THREADS_PER_MEGACLIENT),
clientType));
megaApi[index] = newMegaApi(APP_KEY.c_str(),
megaApiCacheFolder(static_cast<int>(index)).c_str(),
USER_AGENT.c_str(),
unsigned(THREADS_PER_MEGACLIENT),
clientType);
mApi[index].megaApi = megaApi[index].get();

// helps with restoring logging after tests that fiddle with log level
Expand Down Expand Up @@ -7132,7 +7138,10 @@ TEST_F(SdkTest, SdkTestCloudraidTransfers)
exitresumecount += 1;
WaitMillisec(100);

megaApi[0].reset(newMegaApi(APP_KEY.c_str(), megaApiCacheFolder(0).c_str(), USER_AGENT.c_str(), unsigned(THREADS_PER_MEGACLIENT)));
megaApi[0] = newMegaApi(APP_KEY.c_str(),
megaApiCacheFolder(0).c_str(),
USER_AGENT.c_str(),
unsigned(THREADS_PER_MEGACLIENT));
mApi[0].megaApi = megaApi[0].get();
megaApi[0]->addListener(this);
megaApi[0]->setMaxDownloadSpeed(1024 * 1024);
Expand Down Expand Up @@ -11692,7 +11701,7 @@ TEST_F(SdkTest, DISABLED_StressTestSDKInstancesOverWritableFoldersOverWritableFo
std::vector<std::unique_ptr<RequestTracker>> trackers;
trackers.resize(howMany);

std::vector<std::unique_ptr<MegaApi>> exportedFolderApis;
std::vector<MegaApiTestPointer> exportedFolderApis;
exportedFolderApis.resize(howMany);

std::vector<std::string> exportedLinks;
Expand Down Expand Up @@ -11732,11 +11741,11 @@ TEST_F(SdkTest, DISABLED_StressTestSDKInstancesOverWritableFoldersOverWritableFo
// create apis to exported folders
for (unsigned index = 0; index < howMany; index++)
{
exportedFolderApis[static_cast<size_t>(index)].reset(
exportedFolderApis[static_cast<size_t>(index)] =
newMegaApi(APP_KEY.c_str(),
megaApiCacheFolder(static_cast<int>(index) + 10).c_str(),
USER_AGENT.c_str(),
static_cast<unsigned>(THREADS_PER_MEGACLIENT)));
static_cast<unsigned>(THREADS_PER_MEGACLIENT));

// reduce log level to something beareable
exportedFolderApis[static_cast<size_t>(index)]->setLogLevel(MegaApi::LOG_LEVEL_WARNING);
Expand Down Expand Up @@ -11837,7 +11846,7 @@ TEST_F(SdkTest, WritableFolderSessionResumption)
std::vector<std::unique_ptr<RequestTracker>> trackers;
trackers.resize(howMany);

std::vector<std::unique_ptr<MegaApi>> exportedFolderApis;
std::vector<MegaApiTestPointer> exportedFolderApis;
exportedFolderApis.resize(howMany);

std::vector<std::string> exportedLinks;
Expand Down Expand Up @@ -11883,11 +11892,11 @@ TEST_F(SdkTest, WritableFolderSessionResumption)
// create apis to exported folders
for (unsigned index = 0 ; index < howMany; index++ )
{
exportedFolderApis[index].reset(
exportedFolderApis[index] =
newMegaApi(APP_KEY.c_str(),
megaApiCacheFolder(static_cast<int>(index) + 10).c_str(),
USER_AGENT.c_str(),
static_cast<unsigned>(THREADS_PER_MEGACLIENT)));
static_cast<unsigned>(THREADS_PER_MEGACLIENT));

// reduce log level to something beareable
exportedFolderApis[index]->setLogLevel(MegaApi::LOG_LEVEL_WARNING);
Expand Down
22 changes: 16 additions & 6 deletions tests/integration/SdkTest_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,23 +249,33 @@ class MegaApiTest: public MegaApi
unsigned workerThreadCount = 1,
const int clientType = MegaApi::CLIENT_TYPE_DEFAULT);

MegaApiTest(const std::string& endpointName,
const char* appKey,
MegaApiTest(const char* appKey,
MegaGfxProvider* provider,
const char* basePath = nullptr,
const char* userAgent = nullptr,
unsigned workerThreadCount = 1,
const int clientType = MegaApi::CLIENT_TYPE_DEFAULT);

~MegaApiTest();

MegaClient* getClient();
};

class MegaApiTestDeleter
{
public:
MegaApiTestDeleter(const std::string& endpointName):
mEndpointName{endpointName} {};

MegaApiTestDeleter():
MegaApiTestDeleter(""){};

void operator()(MegaApiTest* p) const;

private:
// the endpoint name for isolated gfx
std::string mEndpointName;
};

using MegaApiTestPointer = std::unique_ptr<MegaApiTest, MegaApiTestDeleter>;

// Fixture class with common code for most of tests
class SdkTest : public SdkTestBase, public MegaListener, public MegaRequestListener, MegaTransferListener, MegaLogger {

Expand Down Expand Up @@ -417,7 +427,7 @@ class SdkTest : public SdkTestBase, public MegaListener, public MegaRequestListe
};

std::vector<PerApi> mApi;
std::vector<std::unique_ptr<MegaApiTest>> megaApi;
std::vector<MegaApiTestPointer> megaApi;

m_off_t onTransferStart_progress;
m_off_t onTransferUpdate_progress;
Expand Down

0 comments on commit 7236016

Please sign in to comment.