From 96e04022c4a7c13c7d0184a0521490ed2abea9ce Mon Sep 17 00:00:00 2001 From: Damian Jarek Date: Sun, 10 Dec 2017 00:20:17 +0100 Subject: [PATCH] handler_ptr tests: fix #932 Ensure handler_ptr cleans up when move-constructing a handler throws an exception. Signed-off-by: Damian Jarek --- CHANGELOG.md | 6 +++ test/beast/core/handler_ptr.cpp | 74 +++++++++++++++++++++++++-------- 2 files changed, 62 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2061b51a..778bb57c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +Version 150: + +* handler_ptr tests + +-------------------------------------------------------------------------------- + Version 149: * built-in r-value return values can't be assigned diff --git a/test/beast/core/handler_ptr.cpp b/test/beast/core/handler_ptr.cpp index 1885de4b..41fe444e 100644 --- a/test/beast/core/handler_ptr.cpp +++ b/test/beast/core/handler_ptr.cpp @@ -24,9 +24,7 @@ public: struct handler { std::unique_ptr ptr; - - void - operator()(bool& b) const + void operator()(bool& b) const { b = true; } @@ -34,8 +32,7 @@ public: struct T { - explicit - T(handler const&) + explicit T(handler const&) { } @@ -44,18 +41,16 @@ public: } }; - struct U - { - explicit - U(handler const&) - { - throw std::exception{}; - } - }; - void - run() override + testCtorExcept() { + struct U + { + explicit U(handler const&) + { + throw std::exception{}; + } + }; handler_ptr p1{handler{}}; try { @@ -68,13 +63,56 @@ public: } catch(...) { - fail(); + fail("", __FILE__, __LINE__); } - handler_ptr p3{handler{}}; + } + + void + testMoveExcept() + { + struct throwing_handler + { + throwing_handler() = default; + throwing_handler(throwing_handler&&) + { + throw std::bad_alloc{}; + } + void operator()() const + { + } + }; + struct T + { + explicit T(throwing_handler const&) noexcept {} + }; + try + { + throwing_handler h; + handler_ptr p{std::move(h)}; + fail("", __FILE__, __LINE__); + } + catch (std::bad_alloc const&) + { + pass(); + } + } + + void + testInvoke() + { + handler_ptr p{handler{}}; bool b = false; - p3.invoke(std::ref(b)); + p.invoke(std::ref(b)); BEAST_EXPECT(b); } + + void + run() override + { + testCtorExcept(); + testMoveExcept(); + testInvoke(); + } }; BEAST_DEFINE_TESTSUITE(beast,core,handler_ptr);