]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
test/crimson: minimize the lexical scope of thread pool 34874/head
authorKefu Chai <kchai@redhat.com>
Thu, 30 Apr 2020 02:44:10 +0000 (10:44 +0800)
committerKefu Chai <kchai@redhat.com>
Fri, 1 May 2020 07:05:22 +0000 (15:05 +0800)
to show the typical use case of thread pool in a better manner: thread
pool can be initialized *in* a seastar application.

also move `handle_exception()` out to catch all exceptions throwns in
the seastar app.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/test/crimson/test_thread_pool.cc

index c3ab6fdb0e1b85b7cf301b9018c36e857038c4e8..962470e3b69ce4a74bf2b1a33b66ca8c09dc090f 100644 (file)
@@ -37,9 +37,8 @@ seastar::future<> test_void_return(ThreadPool& tp) {
 
 int main(int argc, char** argv)
 {
-  std::unique_ptr<crimson::thread::ThreadPool> tp;
   seastar::app_template app;
-  return app.run(argc, argv, [&tp] {
+  return app.run(argc, argv, [] {
     std::vector<const char*> args;
     std::string cluster;
     std::string conf_file_list;
@@ -50,20 +49,22 @@ int main(int argc, char** argv)
     return crimson::common::sharded_conf().start(init_params.name, cluster)
     .then([conf_file_list] {
       return local_conf().parse_config_files(conf_file_list);
-    }).then([&tp] {
-      tp = std::make_unique<crimson::thread::ThreadPool>(2, 128, 0);
-      return tp->start().then([&tp] {
-        return test_accumulate(*tp);
-      }).then([&tp] {
-        return test_void_return(*tp);
-      }).handle_exception([](auto e) {
-        std::cerr << "Error: " << e << std::endl;
-        seastar::engine().exit(1);
-      }).finally([&tp] {
-        return tp->stop();
+    }).then([] {
+      return seastar::do_with(std::make_unique<crimson::thread::ThreadPool>(2, 128, 0),
+                              [](auto& tp) {
+        return tp->start().then([&tp] {
+          return test_accumulate(*tp);
+        }).then([&tp] {
+          return test_void_return(*tp);
+        }).finally([&tp] {
+          return tp->stop();
+        });
       });
     }).finally([] {
       return crimson::common::sharded_conf().stop();
+    }).handle_exception([](auto e) {
+      std::cerr << "Error: " << e << std::endl;
+      seastar::engine().exit(1);
     });
   });
 }