]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
test/crimson/test_crimson_coroutine: introduce repeat example 66637/head
authorMatan Breizman <mbreizma@redhat.com>
Mon, 15 Dec 2025 12:39:49 +0000 (12:39 +0000)
committerMatan Breizman <mbreizma@redhat.com>
Mon, 15 Dec 2025 16:25:57 +0000 (18:25 +0200)
interruptible repeat methods use internally seastar::repeat which
require passing a seastar::stop_iteration.
Using coroutines, it's far nicer to use primitive for/while
loops with interrutible futures inside.

Adding interruptible_coroutine_interrupted_repeat as an example.
to a discussion from https://github.com/ceph/ceph/pull/65157

Signed-off-by: Matan Breizman <mbreizma@redhat.com>
src/test/crimson/test_crimson_coroutine.cc

index 43733b11e1f570b87f57510c5fb857f39edc2e1a..86e07406a16291f8d7004a11fbbcfd3255a486b6 100644 (file)
@@ -14,6 +14,8 @@
 
 #include "test/crimson/gtest_seastar.h"
 
+using namespace std::chrono_literals;
+
 struct coroutine_test_t : public seastar_test_suite_t {
   struct interruption_state_t {
     bool interrupted = false;
@@ -262,6 +264,27 @@ TEST_F(coroutine_test_t, interruptible_coroutine_interrupted)
   });
 }
 
+TEST_F(coroutine_test_t, interruptible_coroutine_interrupted_repeat)
+{
+  run_scl([this]() -> seastar::future<> {
+    int count = 0;
+    auto ret = cwi(
+      [](auto) { return 2; },
+      [&count]() mutable -> interruptor::future<int> {
+        while (true) {
+          count++;
+          co_await interruptor::make_interruptible(seastar::sleep(100ms));
+          continue;
+        }
+      });
+    auto backgroud = seastar::sleep(3s).then([this]{interrupt();});
+    auto awaited = co_await std::move(ret);
+    EXPECT_EQ(awaited, 2);
+    // Make sure that there were a few iterations prior to interrupting..
+    EXPECT_GE(count, 10);
+  });
+}
+
 TEST_F(coroutine_test_t, dual_interruptible_coroutine)
 {
   run_scl([this]() -> seastar::future<> {