]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
test: Add program for manually testing the lock file implementation
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 3 Jul 2022 09:48:14 +0000 (11:48 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 4 Jul 2022 18:58:43 +0000 (20:58 +0200)
src/CMakeLists.txt
src/test_lockfile.cpp [new file with mode: 0644]

index dcb399fa6af6ce41faec4a21f5fc5fb5d43d090a..c30fa8a92cf6d05a0733e0313dbd754f14b6ced1 100644 (file)
@@ -61,16 +61,19 @@ target_link_libraries(
   PRIVATE standard_settings standard_warnings ZSTD::ZSTD Threads::Threads third_party
 )
 
-target_include_directories(ccache_framework PRIVATE ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
+target_include_directories(ccache_framework PUBLIC ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
 
 if(REDIS_STORAGE_BACKEND)
-  target_compile_definitions(ccache_framework PRIVATE -DHAVE_REDIS_STORAGE_BACKEND)
+  target_compile_definitions(ccache_framework PUBLIC -DHAVE_REDIS_STORAGE_BACKEND)
   target_link_libraries(
     ccache_framework
     PUBLIC standard_settings standard_warnings HIREDIS::HIREDIS third_party
   )
 endif()
 
+add_executable(test-lockfile test_lockfile.cpp)
+target_link_libraries(test-lockfile PRIVATE ccache_framework)
+
 add_subdirectory(compression)
 add_subdirectory(core)
 add_subdirectory(storage)
diff --git a/src/test_lockfile.cpp b/src/test_lockfile.cpp
new file mode 100644 (file)
index 0000000..25f5dc2
--- /dev/null
@@ -0,0 +1,60 @@
+// Copyright (C) 2022 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#include <Config.hpp>
+#include <Lockfile.hpp>
+#include <Logging.hpp>
+#include <fmtmacros.hpp>
+#include <util/string.hpp>
+
+#include <string>
+#include <thread>
+
+int
+main(int argc, char** argv)
+{
+  if (argc != 3) {
+    PRINT_RAW(stderr, "Usage: test-lockfile PATH SECONDS\n");
+    return 1;
+  }
+  Config config;
+  config.update_from_environment();
+  Logging::init(config);
+
+  std::string path(argv[1]);
+  auto seconds = util::parse_signed(argv[2]);
+  if (!seconds) {
+    PRINT_RAW(stderr, "Error: Failed to parse seconds\n");
+    return 1;
+  }
+
+  PRINT_RAW(stdout, "Acquiring\n");
+  {
+    Lockfile lock(path);
+    if (lock.acquired()) {
+      PRINT_RAW(stdout, "Acquired\n");
+      PRINT(
+        stdout, "Sleeping {} second{}\n", *seconds, *seconds == 1 ? "" : "s");
+      std::this_thread::sleep_for(std::chrono::seconds{*seconds});
+    } else {
+      PRINT_RAW(stdout, "Failed to acquire\n");
+    }
+    PRINT_RAW(stdout, "Releasing\n");
+  }
+  PRINT_RAW(stdout, "Released\n");
+}