Skip to content

Commit

Permalink
Fix TestJobManager::testCancelJobs random faillure.
Browse files Browse the repository at this point in the history
  • Loading branch information
herve-er committed Jan 3, 2025
1 parent 8dfe487 commit 58d00ca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
40 changes: 25 additions & 15 deletions test/libsyncengine/jobs/testjobmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ namespace KDC {
static const SyncPath localTestDirPath(std::wstring(L"" TEST_DIR) + L"/test_ci");
static const SyncPath localTestDirPath_manyFiles(std::wstring(L"" TEST_DIR) + L"/test_ci/many_files_dir");
static const SyncPath localTestDirPath_pictures(std::wstring(L"" TEST_DIR) + L"/test_ci/test_pictures");
static const SyncPath localTestDirPath_bigFiles(std::wstring(L"" TEST_DIR) + L"/test_ci/big_file_dir");
static const int driveDbId = 1;
void KDC::TestJobManager::setUp() {
const testhelpers::TestVariables testVariables;
Expand Down Expand Up @@ -175,25 +174,40 @@ void TestJobManager::testWithCallbackBigFiles() {
}

void TestJobManager::testCancelJobs() {
// Create temp remote directory
const RemoteTemporaryDirectory remoteTmpDir(driveDbId, _testVariables.remoteDirId, "TestJobManager testCancelJobs");
const LocalTemporaryDirectory localTmpDir("testJobManager");
const int localFileCounter = 100;
for (int i = 0; i < localFileCounter; i++) {
testhelpers::generateOrEditTestFile(localTmpDir.path() / ("file_" + std::to_string(i) + ".txt"));
}

// Upload all files in testDir
ulong jobCounter = 0;
for (auto &dirEntry: std::filesystem::directory_iterator(localTestDirPath_manyFiles)) {
for (auto &dirEntry: std::filesystem::directory_iterator(localTmpDir.path())) {
auto job = std::make_shared<UploadJob>(driveDbId, dirEntry.path(), dirEntry.path().filename().native(), remoteTmpDir.id(),
0);
std::function<void(UniqueId)> callback = std::bind(&TestJobManager::callback, this, std::placeholders::_1);
JobManager::instance()->queueAsyncJob(job, Poco::Thread::PRIO_NORMAL, callback);
jobCounter++;
const std::scoped_lock lock(_mutex);
_ongoingJobs.try_emplace(static_cast<uint64_t>(job->jobId()), job);
}

Utility::msleep(1000); // Wait 1sec
while (_ongoingJobs.size() == localFileCounter) {
Utility::msleep(1); // Wait 1ms
}

cancelAllOngoingJobs();

Utility::msleep(10000); // Wait 10sec
int rerty = 1000; // Wait max 10sec
while ((!JobManager::_managedJobs.empty() || !JobManager::_queuedJobs.empty() || !JobManager::_runningJobs.empty() ||
!JobManager::_pendingJobs.empty()) &&
(rerty > 0)) {
rerty--;
Utility::msleep(10);
}

CPPUNIT_ASSERT(JobManager::instance()->_managedJobs.empty());
CPPUNIT_ASSERT(JobManager::instance()->_queuedJobs.empty());
CPPUNIT_ASSERT(JobManager::instance()->_runningJobs.empty());
CPPUNIT_ASSERT(JobManager::instance()->_pendingJobs.empty());

GetFileListJob fileListJob(driveDbId, remoteTmpDir.id());
fileListJob.runSynchronously();
Expand All @@ -202,14 +216,10 @@ void TestJobManager::testCancelJobs() {
CPPUNIT_ASSERT(resObj);

Poco::JSON::Array::Ptr data = resObj->getArray(dataKey);
size_t total = data->size();
CPPUNIT_ASSERT(jobCounter != total);
CPPUNIT_ASSERT(total > 0);
size_t uploadedFileCounter = data->size();
CPPUNIT_ASSERT(localFileCounter != uploadedFileCounter);
CPPUNIT_ASSERT(uploadedFileCounter > 0);
CPPUNIT_ASSERT(ongoingJobsCount() == 0);
CPPUNIT_ASSERT(JobManager::instance()->_managedJobs.empty());
CPPUNIT_ASSERT(JobManager::instance()->_queuedJobs.empty());
CPPUNIT_ASSERT(JobManager::instance()->_runningJobs.empty());
CPPUNIT_ASSERT(JobManager::instance()->_pendingJobs.empty());
}

std::queue<int64_t> finishedJobs;
Expand Down
2 changes: 1 addition & 1 deletion test/test_utility/remotetemporarydirectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ RemoteTemporaryDirectory::RemoteTemporaryDirectory(int driveDbId, const NodeId&
// Create remote test dir
CreateDirJob job(_driveDbId, parentId, _dirName);
job.runSynchronously();
if (job.exitInfo() == ExitInfo::ExitInfo(ExitCode::BackError, ExitCause::FileAlreadyExist) && retry > 0) {
if (job.exitInfo() == ExitInfo(ExitCode::BackError, ExitCause::FileAlreadyExist) && retry > 0) {
suffix = CommonUtility::generateRandomStringAlphaNum(5);
retry--;
continue;
Expand Down

0 comments on commit 58d00ca

Please sign in to comment.