handler_ptr tests:

fix #932

Ensure handler_ptr cleans up when move-constructing
a handler throws an exception.

Signed-off-by: Damian Jarek <damian.jarek93@gmail.com>
This commit is contained in:
Damian Jarek
2017-12-10 00:20:17 +01:00
committed by Vinnie Falco
parent 3edb30b2e8
commit 96e04022c4
2 changed files with 62 additions and 18 deletions

View File

@ -1,3 +1,9 @@
Version 150:
* handler_ptr tests
--------------------------------------------------------------------------------
Version 149: Version 149:
* built-in r-value return values can't be assigned * built-in r-value return values can't be assigned

View File

@ -24,9 +24,7 @@ public:
struct handler struct handler
{ {
std::unique_ptr<int> ptr; std::unique_ptr<int> ptr;
void operator()(bool& b) const
void
operator()(bool& b) const
{ {
b = true; b = true;
} }
@ -34,8 +32,7 @@ public:
struct T struct T
{ {
explicit explicit T(handler const&)
T(handler const&)
{ {
} }
@ -44,18 +41,16 @@ public:
} }
}; };
struct U
{
explicit
U(handler const&)
{
throw std::exception{};
}
};
void void
run() override testCtorExcept()
{ {
struct U
{
explicit U(handler const&)
{
throw std::exception{};
}
};
handler_ptr<T, handler> p1{handler{}}; handler_ptr<T, handler> p1{handler{}};
try try
{ {
@ -68,13 +63,56 @@ public:
} }
catch(...) catch(...)
{ {
fail(); fail("", __FILE__, __LINE__);
} }
handler_ptr<T, handler> 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<T, throwing_handler> p{std::move(h)};
fail("", __FILE__, __LINE__);
}
catch (std::bad_alloc const&)
{
pass();
}
}
void
testInvoke()
{
handler_ptr<T, handler> p{handler{}};
bool b = false; bool b = false;
p3.invoke(std::ref(b)); p.invoke(std::ref(b));
BEAST_EXPECT(b); BEAST_EXPECT(b);
} }
void
run() override
{
testCtorExcept();
testMoveExcept();
testInvoke();
}
}; };
BEAST_DEFINE_TESTSUITE(beast,core,handler_ptr); BEAST_DEFINE_TESTSUITE(beast,core,handler_ptr);