]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/seastar/tests/unit/semaphore_test.cc
import 15.2.0 Octopus source
[ceph.git] / ceph / src / seastar / tests / unit / semaphore_test.cc
index c12edb99380d72d7b7051c211eeb23178eb87e85..ef18e19dad9ba3350ea24cad33ba7080f4690834 100644 (file)
@@ -54,7 +54,7 @@ SEASTAR_TEST_CASE(test_semaphore_consume) {
 
 SEASTAR_TEST_CASE(test_semaphore_1) {
     return do_with(std::make_pair(semaphore(0), 0), [] (std::pair<semaphore, int>& x) {
-        x.first.wait().then([&x] {
+        (void)x.first.wait().then([&x] {
             x.second++;
         });
         x.first.signal();
@@ -66,7 +66,7 @@ SEASTAR_TEST_CASE(test_semaphore_1) {
 
 SEASTAR_TEST_CASE(test_semaphore_2) {
     return do_with(std::make_pair(semaphore(0), 0), [] (std::pair<semaphore, int>& x) {
-        x.first.wait().then([&x] {
+        (void)x.first.wait().then([&x] {
             x.second++;
         });
         return sleep(10ms).then([&x] {
@@ -77,13 +77,13 @@ SEASTAR_TEST_CASE(test_semaphore_2) {
 
 SEASTAR_TEST_CASE(test_semaphore_timeout_1) {
     return do_with(std::make_pair(semaphore(0), 0), [] (std::pair<semaphore, int>& x) {
-        x.first.wait(10ms).then([&x] {
+        (void)x.first.wait(100ms).then([&x] {
             x.second++;
         });
-        sleep(3ms).then([&x] {
+        (void)sleep(3ms).then([&x] {
             x.first.signal();
         });
-        return sleep(20ms).then([&x] {
+        return sleep(200ms).then([&x] {
             BOOST_REQUIRE_EQUAL(x.second, 1);
         });
     });
@@ -91,13 +91,13 @@ SEASTAR_TEST_CASE(test_semaphore_timeout_1) {
 
 SEASTAR_TEST_CASE(test_semaphore_timeout_2) {
     return do_with(std::make_pair(semaphore(0), 0), [] (std::pair<semaphore, int>& x) {
-        x.first.wait(3ms).then([&x] {
+        (void)x.first.wait(3ms).then([&x] {
             x.second++;
         });
-        sleep(10ms).then([&x] {
+        (void)sleep(100ms).then([&x] {
             x.first.signal();
         });
-        return sleep(20ms).then([&x] {
+        return sleep(200ms).then([&x] {
             BOOST_REQUIRE_EQUAL(x.second, 0);
         });
     });
@@ -105,16 +105,16 @@ SEASTAR_TEST_CASE(test_semaphore_timeout_2) {
 
 SEASTAR_TEST_CASE(test_semaphore_mix_1) {
     return do_with(std::make_pair(semaphore(0), 0), [] (std::pair<semaphore, int>& x) {
-        x.first.wait(3ms).then([&x] {
+        (void)x.first.wait(30ms).then([&x] {
             x.second++;
         });
-        x.first.wait().then([&x] {
+        (void)x.first.wait().then([&x] {
             x.second = 10;
         });
-        sleep(10ms).then([&x] {
+        (void)sleep(100ms).then([&x] {
             x.first.signal();
         });
-        return sleep(20ms).then([&x] {
+        return sleep(200ms).then([&x] {
             BOOST_REQUIRE_EQUAL(x.second, 10);
         });
     });
@@ -247,3 +247,37 @@ SEASTAR_THREAD_TEST_CASE(test_semaphore_units_splitting) {
     BOOST_REQUIRE_THROW(units.split(10), std::invalid_argument);
     BOOST_REQUIRE_EQUAL(sm.available_units(), 0);
 }
+
+SEASTAR_THREAD_TEST_CASE(test_named_semaphore_error) {
+    auto sem = make_lw_shared<named_semaphore>(0, named_semaphore_exception_factory{"name_of_the_semaphore"});
+    auto check_result = [sem] (future<> f) {
+        try {
+            f.get();
+            BOOST_FAIL("Expecting an exception");
+        } catch (broken_named_semaphore& ex) {
+            BOOST_REQUIRE_NE(std::string(ex.what()).find("name_of_the_semaphore"), std::string::npos);
+        } catch (...) {
+            BOOST_FAIL("Expected an instance of broken_named_semaphore with proper semaphore name");
+        }
+        return make_ready_future<>();
+    };
+    auto ret = sem->wait().then_wrapped(check_result);
+    sem->broken();
+    sem->wait().then_wrapped(check_result).then([ret = std::move(ret)] () mutable {
+        return std::move(ret);
+    }).get();
+}
+
+SEASTAR_THREAD_TEST_CASE(test_named_semaphore_timeout) {
+    auto sem = make_lw_shared<named_semaphore>(0, named_semaphore_exception_factory{"name_of_the_semaphore"});
+
+    auto f = sem->wait(named_semaphore::clock::now() + 1ms, 1);
+    try {
+        f.get();
+        BOOST_FAIL("Expecting an exception");
+    } catch (named_semaphore_timed_out& ex) {
+        BOOST_REQUIRE_NE(std::string(ex.what()).find("name_of_the_semaphore"), std::string::npos);
+    } catch (...) {
+        BOOST_FAIL("Expected an instance of named_semaphore_timed_out with proper semaphore name");
+    }
+}