================ @@ -0,0 +1,164 @@ +//===-- AlarmTest.cpp -----------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "lldb/Host/Alarm.h" +#include "gtest/gtest.h" + +#include <chrono> +#include <thread> + +using namespace lldb_private; +using namespace std::chrono_literals; + +static constexpr auto ALARM_TIMEOUT = 500ms; +static constexpr auto TEST_TIMEOUT = 1000ms; +static constexpr bool RUN_CALLBACKS_ON_EXIT = true; + +// Enable strict checking of the ALARM_TIMEOUTs. This should only be enabled for +// development and never during automated testing where scheduling and +// ALARM_TIMEOUTs are unpredictable. +#define STRICT_ALARM_TIMEOUT 1 + +#if STRICT_ALARM_TIMEOUT +static constexpr auto EPSILON = 10ms; +#endif + +TEST(AlarmTest, Create) { + std::mutex m; + + std::vector<Alarm::TimePoint> callbacks_actual; + std::vector<Alarm::TimePoint> callbacks_expected; + + Alarm alarm(ALARM_TIMEOUT, RUN_CALLBACKS_ON_EXIT); + + // Create 5 alarms some time apart. + for (size_t i = 0; i < 5; ++i) { + callbacks_actual.emplace_back(); + callbacks_expected.emplace_back(std::chrono::system_clock::now() + + ALARM_TIMEOUT); + + alarm.Create([&callbacks_actual, &m, i]() { + std::lock_guard guard(m); + callbacks_actual[i] = std::chrono::system_clock::now(); + }); + + std::this_thread::sleep_for(ALARM_TIMEOUT / 5); + } + + // Leave plenty of time for all the alarms to fire. + std::this_thread::sleep_for(TEST_TIMEOUT); + + // Make sure all the alarms around the expected time. ---------------- chelcassanova wrote:
nit: Missing word or am I just reading this wrong? https://github.com/llvm/llvm-project/pull/84854 _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits