From 818f2d0c5cf19e2a036e858ea1d9339750ef0296 Mon Sep 17 00:00:00 2001 From: Nikolai Kosjar Date: Thu, 21 Nov 2019 15:09:08 +0100 Subject: [PATCH] CppTools: Fix race condition when opening projects for tests The ClangTools plugin tests open a project and immediately trigger a build afterwards. We checked whether CppModelManager got already data from the project manager, but this alone was racy for the mentioned use case as e.g. the QbsProjectManager might still be in parsing mode and thus the build was not triggerable. Check for the parsing state, too. While at it, simplify the code by using QTest::qWaitFor(), which also takes care of posted events. Change-Id: Id298695b2e67578def42275bac4ea31f4a39edf2 Reviewed-by: Christian Kandeler --- .../test/clangbatchfileprocessor.cpp | 2 +- src/plugins/cpptools/cpptoolstestcase.cpp | 21 +++++++------------ src/plugins/cpptools/cpptoolstestcase.h | 5 ++--- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/plugins/clangcodemodel/test/clangbatchfileprocessor.cpp b/src/plugins/clangcodemodel/test/clangbatchfileprocessor.cpp index 25d22bb1c1b..8556c84b4b0 100644 --- a/src/plugins/clangcodemodel/test/clangbatchfileprocessor.cpp +++ b/src/plugins/clangcodemodel/test/clangbatchfileprocessor.cpp @@ -237,7 +237,7 @@ bool OpenProjectCommand::run() Project *project = openProjectSucceeded.project(); project->configureAsExampleProject(); - return CppTools::Tests::TestCase::waitUntilCppModelManagerIsAwareOf(project, timeOutInMs()); + return CppTools::Tests::TestCase::waitUntilProjectIsFullyOpened(project, timeOutInMs()); } Command::Ptr OpenProjectCommand::parse(BatchFileLineTokenizer &arguments, diff --git a/src/plugins/cpptools/cpptoolstestcase.cpp b/src/plugins/cpptools/cpptoolstestcase.cpp index b9ba701aebb..8040e3f811f 100644 --- a/src/plugins/cpptools/cpptoolstestcase.cpp +++ b/src/plugins/cpptools/cpptoolstestcase.cpp @@ -226,22 +226,17 @@ QList TestCase::waitForFilesInGlobalSnapshot(const QSt return result; } -bool TestCase::waitUntilCppModelManagerIsAwareOf(Project *project, int timeOutInMs) +bool TestCase::waitUntilProjectIsFullyOpened(Project *project, int timeOutInMs) { if (!project) return false; - QElapsedTimer t; - t.start(); - - CppModelManager *modelManager = CppModelManager::instance(); - forever { - if (modelManager->projectInfo(project).isValid()) - return true; - if (t.elapsed() > timeOutInMs) - return false; - QCoreApplication::processEvents(); - } + return QTest::qWaitFor( + [project]() { + return !project->isParsing() + && CppModelManager::instance()->projectInfo(project).isValid(); + }, + timeOutInMs); } bool TestCase::writeFile(const QString &filePath, const QByteArray &contents) @@ -293,7 +288,7 @@ ProjectInfo ProjectOpenerAndCloser::open(const QString &projectFile, bool config if (configureAsExampleProject) project->configureAsExampleProject(); - if (TestCase::waitUntilCppModelManagerIsAwareOf(project)) { + if (TestCase::waitUntilProjectIsFullyOpened(project)) { m_openProjects.append(project); return CppModelManager::instance()->projectInfo(project); } diff --git a/src/plugins/cpptools/cpptoolstestcase.h b/src/plugins/cpptools/cpptoolstestcase.h index 009a5a9c3ed..d6e306d255f 100644 --- a/src/plugins/cpptools/cpptoolstestcase.h +++ b/src/plugins/cpptools/cpptoolstestcase.h @@ -92,9 +92,8 @@ public: static bool waitForProcessedEditorDocument(const QString &filePath, int timeOutInMs = 5000); enum { defaultTimeOutInMs = 30 * 1000 /*= 30 secs*/ }; - static bool waitUntilCppModelManagerIsAwareOf( - ProjectExplorer::Project *project, - int timeOutInMs = defaultTimeOutInMs); + static bool waitUntilProjectIsFullyOpened(ProjectExplorer::Project *project, + int timeOutInMs = defaultTimeOutInMs); static CPlusPlus::Document::Ptr waitForFileInGlobalSnapshot( const QString &filePath, int timeOutInMs = defaultTimeOutInMs);