]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
test/crimson: factor out seastar_runner helpers
authorSamuel Just <sjust@redhat.com>
Fri, 30 Oct 2020 23:57:48 +0000 (16:57 -0700)
committerSamuel Just <sjust@redhat.com>
Tue, 8 Dec 2020 19:06:52 +0000 (19:06 +0000)
Signed-off-by: Samuel Just <sjust@redhat.com>
src/test/crimson/gtest_seastar.cc
src/test/crimson/gtest_seastar.h
src/test/crimson/seastar_runner.h [new file with mode: 0644]

index 767066b3173433abac405086d404c559f71f7efc..9d43fc6be1496cef3d2e15db709db0635f44ee21 100644 (file)
@@ -4,49 +4,7 @@
 #include "include/ceph_assert.h"
 #include "gtest_seastar.h"
 
-seastar_gtest_env_t seastar_test_suite_t::seastar_env;
-
-seastar_gtest_env_t::seastar_gtest_env_t() :
-  begin_fd{seastar::file_desc::eventfd(0, 0)} {}
-
-void seastar_gtest_env_t::init(int argc, char **argv)
-{
-  thread = std::thread([argc, argv, this] { reactor(argc, argv); });
-  eventfd_t result = 0;
-  if (int r = ::eventfd_read(begin_fd.get(), &result); r < 0) {
-    std::cerr << "unable to eventfd_read():" << errno << std::endl;
-    throw std::runtime_error("Cannot start seastar");
-  }
-}
-
-void seastar_gtest_env_t::stop()
-{
-  run([this] {
-    on_end->write_side().signal(1);
-    return seastar::now();
-  });
-  thread.join();
-}
-
-seastar_gtest_env_t::~seastar_gtest_env_t()
-{}
-
-void seastar_gtest_env_t::reactor(int argc, char** argv)
-{
-  app.run(argc, argv, [this] {
-    on_end.reset(new seastar::readable_eventfd);
-    return seastar::now().then([this] {
-      ::eventfd_write(begin_fd.get(), 1);
-       return seastar::now();
-    }).then([this] {
-      return on_end->wait().then([](size_t){});
-    }).handle_exception([](auto ep) {
-      std::cerr << "Error: " << ep << std::endl;
-    }).finally([this] {
-      on_end.reset();
-    });
-  });
-}
+SeastarRunner seastar_test_suite_t::seastar_env;
 
 int main(int argc, char **argv)
 {
index 6f4d376d27aaa7f847079ac264bb8771578da87f..20709a3eec4870e5cb7372d8d1904d26d44ea729 100644 (file)
@@ -3,41 +3,12 @@
 
 #pragma once
 
-#include <stdio.h>
-#include <signal.h>
-#include <thread>
-
-#include <seastar/core/app-template.hh>
-#include <seastar/core/future-util.hh>
-#include <seastar/core/reactor.hh>
-#include <seastar/core/alien.hh>
-#include <seastar/core/thread.hh>
-
 #include "gtest/gtest.h"
 
-struct seastar_gtest_env_t {
-  seastar::app_template app;
-  seastar::file_desc begin_fd;
-  std::unique_ptr<seastar::readable_eventfd> on_end;
-
-  std::thread thread;
-
-  seastar_gtest_env_t();
-  ~seastar_gtest_env_t();
-
-  void init(int argc, char **argv);
-  void stop();
-  void reactor(int argc, char **argv);
-
-  template <typename Func>
-  void run(Func &&func) {
-    auto fut = seastar::alien::submit_to(0, std::forward<Func>(func));
-    fut.get();
-  }
-};
+#include "seastar_runner.h"
 
 struct seastar_test_suite_t : public ::testing::Test {
-  static seastar_gtest_env_t seastar_env;
+  static SeastarRunner seastar_env;
 
   template <typename Func>
   void run(Func &&func) {
diff --git a/src/test/crimson/seastar_runner.h b/src/test/crimson/seastar_runner.h
new file mode 100644 (file)
index 0000000..5a43055
--- /dev/null
@@ -0,0 +1,71 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#pragma once
+
+#include <stdio.h>
+#include <signal.h>
+#include <thread>
+
+#include <seastar/core/app-template.hh>
+#include <seastar/core/future-util.hh>
+#include <seastar/core/reactor.hh>
+#include <seastar/core/alien.hh>
+#include <seastar/core/thread.hh>
+
+struct SeastarRunner {
+  seastar::app_template app;
+  seastar::file_desc begin_fd;
+  std::unique_ptr<seastar::readable_eventfd> on_end;
+
+  std::thread thread;
+
+  SeastarRunner() :
+    begin_fd{seastar::file_desc::eventfd(0, 0)} {}
+
+  ~SeastarRunner() {}
+
+  void init(int argc, char **argv)
+  {
+    thread = std::thread([argc, argv, this] { reactor(argc, argv); });
+    eventfd_t result = 0;
+    if (int r = ::eventfd_read(begin_fd.get(), &result); r < 0) {
+      std::cerr << "unable to eventfd_read():" << errno << std::endl;
+      throw std::runtime_error("Cannot start seastar");
+    }
+  }
+  
+  void stop()
+  {
+    run([this] {
+      on_end->write_side().signal(1);
+      return seastar::now();
+    });
+    thread.join();
+  }
+
+  void reactor(int argc, char **argv)
+  {
+    app.run(argc, argv, [this] {
+      on_end.reset(new seastar::readable_eventfd);
+      return seastar::now().then([this] {
+       ::eventfd_write(begin_fd.get(), 1);
+         return seastar::now();
+      }).then([this] {
+       return on_end->wait().then([](size_t){});
+      }).handle_exception([](auto ep) {
+       std::cerr << "Error: " << ep << std::endl;
+      }).finally([this] {
+       on_end.reset();
+      });
+    });
+  }
+
+  template <typename Func>
+  void run(Func &&func) {
+    auto fut = seastar::alien::submit_to(0, std::forward<Func>(func));
+    fut.get();
+  }
+};
+
+