[llvm] [llvm][Support] Add ExponentialBackoff helper (PR #81206)

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 13 10:42:29 PST 2024


================
@@ -0,0 +1,31 @@
+//===- unittests/ExponentialBackoffTest.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 "llvm/Support/ExponentialBackoff.h"
+#include "gtest/gtest.h"
+#include <chrono>
+
+using namespace llvm;
+using namespace std::chrono_literals;
+
+namespace {
+
+TEST(ExponentialBackoffTest, Timeout) {
+  auto Start = std::chrono::steady_clock::now();
+  // Use short enough times that this test runs quickly.
+  ExponentialBackoff Backoff(100ms, 1ms, 10ms);
+  do {
+  } while (Backoff.waitForNextAttempt());
----------------
dwblaikie wrote:

Could check the bounds of the waits in the loop? (like check that it's at least 1ms, less than 15 or something?)

Though, yeah, any testing here will be either slow or brittle or both - so don't do it if it's just impractical to make it reliable.

https://github.com/llvm/llvm-project/pull/81206


More information about the llvm-commits mailing list