aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorChristian Kandeler <christian.kandeler@qt.io>2018-08-10 17:19:58 +0200
committerChristian Kandeler <christian.kandeler@qt.io>2018-08-13 11:31:27 +0000
commitf98170dc4411c76bc02e4ed48af29006e7f91ef8 (patch)
tree88e039cb42ad61d8e1acc7d8769abb3047c0035f /tests
parent0135957c349579d28256d05da342ea79b560bc77 (diff)
Fix race condition in TestBlackboxJobLimits
Do proper file locking in the test app. Change-Id: I985006c0e48a21b9412afd461edc3d7b9f02fcce Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/blackbox/testdata-joblimits/job-limits/main.cpp42
-rw-r--r--tests/auto/blackbox/tst_blackboxjoblimits.cpp1
2 files changed, 32 insertions, 11 deletions
diff --git a/tests/auto/blackbox/testdata-joblimits/job-limits/main.cpp b/tests/auto/blackbox/testdata-joblimits/job-limits/main.cpp
index 42796186c..0a94c6393 100644
--- a/tests/auto/blackbox/testdata-joblimits/job-limits/main.cpp
+++ b/tests/auto/blackbox/testdata-joblimits/job-limits/main.cpp
@@ -26,6 +26,7 @@
**
****************************************************************************/
+#include <cerrno>
#include <chrono>
#include <cstdio>
#include <cstring>
@@ -33,6 +34,25 @@
#include <string>
#include <thread>
+#if defined(_WIN32) || defined(WIN32)
+#include <io.h>
+#include <sys/locking.h>
+#else
+#include <unistd.h>
+#endif
+
+static bool tryLock(FILE *f)
+{
+ const int exitCode =
+#if defined(_WIN32) || defined(WIN32)
+ _locking(_fileno(f), _LK_NBLCK, 10);
+
+#else
+ lockf(fileno(f), F_TLOCK, 10);
+#endif
+ return exitCode == 0;
+}
+
int main(int argc, char *argv[])
{
if (argc != 2) {
@@ -41,21 +61,23 @@ int main(int argc, char *argv[])
}
const std::string lockFilePath = std::string(argv[0]) + ".lock";
- if (std::fopen(lockFilePath.c_str(), "r")) {
- std::cerr << "tool is exclusive" << std::endl;
- return 2;
- }
std::FILE * const lockFile = std::fopen(lockFilePath.c_str(), "w");
if (!lockFile) {
- std::cerr << "cannot create lock file: " << strerror(errno) << std::endl;
- return 3;
+ std::cerr << "cannot open lock file: " << strerror(errno) << std::endl;
+ return 2;
+ }
+ if (!tryLock(lockFile)) {
+ if (errno == EACCES || errno == EAGAIN) {
+ std::cerr << "tool is exclusive" << std::endl;
+ return 3;
+ } else {
+ std::cerr << "unexpected lock failure: " << strerror(errno) << std::endl;
+ fclose(lockFile);
+ return 4;
+ }
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
fclose(lockFile);
- if (std::remove(lockFilePath.c_str()) != 0) {
- std::cerr << "cannot remove lock file: " << strerror(errno) << std::endl;
- return 4;
- }
std::FILE * const output = std::fopen(argv[1], "w");
if (!output) {
std::cerr << "cannot create output file: " << strerror(errno) << std::endl;
diff --git a/tests/auto/blackbox/tst_blackboxjoblimits.cpp b/tests/auto/blackbox/tst_blackboxjoblimits.cpp
index 59e4ffd52..0c366759d 100644
--- a/tests/auto/blackbox/tst_blackboxjoblimits.cpp
+++ b/tests/auto/blackbox/tst_blackboxjoblimits.cpp
@@ -157,7 +157,6 @@ void TestBlackboxJobLimits::jobLimits()
if (projectPrecedence)
buildParams.arguments << "--enforce-project-job-limits";
buildParams.profile = profile.p.name();
- QFile::remove(relativeExecutableFilePath("tool") + ".lock");
const int exitCode = runQbs(buildParams);
if (expectSuccess)
QCOMPARE(exitCode, 0);