mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Remove beast::Thread (RIPD-1189):
All uses of beast::Thread were previously removed from the code base, so beast::Thread is removed. One piece of beast::Thread needed to be preserved: the ability to set the current thread's name. So there's now a beast::CurrentThreadName that allows the current thread's name to be set and returned. Thread naming is also cleaned up a bit. ThreadName.h and .cpp are removed since beast::CurrentThreadName does a better job. ThreadEntry is also removed, but its terminateHandler() is preserved in TerminateHandler.cpp. The revised terminateHandler() uses beast::CurrentThreadName to recover the name of the running thread. Finally, the NO_LOG_UNHANDLED_EXCEPTIONS #define is removed since it was discovered that the MacOS debugger preserves the stack of the original throw even if the terminateHandler() rethrows.
This commit is contained in:
153
src/test/core/TerminateHandler_test.cpp
Normal file
153
src/test/core/TerminateHandler_test.cpp
Normal file
@@ -0,0 +1,153 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2016 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <BeastConfig.h>
|
||||
#include <ripple/core/TerminateHandler.h>
|
||||
#include <ripple/beast/core/CurrentThreadName.h>
|
||||
#include <ripple/beast/unit_test.h>
|
||||
|
||||
#include <boost/coroutine/exceptions.hpp>
|
||||
#include <exception>
|
||||
#include <sstream>
|
||||
#include <streambuf>
|
||||
|
||||
namespace ripple {
|
||||
namespace test {
|
||||
|
||||
class TerminateHandler_test : public beast::unit_test::suite
|
||||
{
|
||||
private:
|
||||
// Allow cerr to be redirected. Destructor restores old cerr streambuf.
|
||||
class CerrRedirect
|
||||
{
|
||||
public:
|
||||
CerrRedirect (std::stringstream& sStream)
|
||||
: old_ (std::cerr.rdbuf (sStream.rdbuf()))
|
||||
{ }
|
||||
|
||||
~CerrRedirect()
|
||||
{
|
||||
std::cerr.rdbuf (old_);
|
||||
}
|
||||
|
||||
private:
|
||||
std::streambuf* const old_;
|
||||
};
|
||||
|
||||
// Set a new current thread name. Destructor restores the old thread name.
|
||||
class ThreadNameGuard
|
||||
{
|
||||
public:
|
||||
ThreadNameGuard (std::string const& newName)
|
||||
: old_ (beast::getCurrentThreadName ())
|
||||
{
|
||||
beast::setCurrentThreadName (newName);
|
||||
}
|
||||
|
||||
~ThreadNameGuard()
|
||||
{
|
||||
std::string oldName;
|
||||
if (old_)
|
||||
oldName = std::move (*old_);
|
||||
|
||||
beast::setCurrentThreadName (oldName);
|
||||
}
|
||||
|
||||
private:
|
||||
boost::optional<std::string> old_;
|
||||
};
|
||||
|
||||
public:
|
||||
void
|
||||
run() override
|
||||
{
|
||||
// Set the current thread name, but restore the old name on exit.
|
||||
std::string const threadName {"terminateHandler_test"};
|
||||
ThreadNameGuard nameGuard {threadName};
|
||||
{
|
||||
// Test terminateHandler() with a std::exception.
|
||||
|
||||
// The terminateHandler() output goes to std::cerr. Capture that.
|
||||
std::stringstream cerrCapture;
|
||||
CerrRedirect cerrRedirect {cerrCapture};
|
||||
|
||||
try
|
||||
{
|
||||
throw std::range_error ("Out of range");
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
terminateHandler();
|
||||
}
|
||||
{
|
||||
std::string result = cerrCapture.str();
|
||||
BEAST_EXPECT (result.find (threadName) != std::string::npos);
|
||||
BEAST_EXPECT (
|
||||
result.find ("Out of range") != std::string::npos);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// Verify terminateHnadler() handles forced_unwind correctly.
|
||||
std::stringstream cerrCapture;
|
||||
CerrRedirect cerrRedirect {cerrCapture};
|
||||
|
||||
try
|
||||
{
|
||||
throw boost::coroutines::detail::forced_unwind();
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
terminateHandler();
|
||||
}
|
||||
{
|
||||
std::string result = cerrCapture.str();
|
||||
BEAST_EXPECT (result.find (threadName) != std::string::npos);
|
||||
BEAST_EXPECT (
|
||||
result.find ("forced_unwind") != std::string::npos);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// Verify terminatHandler()'s handling of non-standard exceptions.
|
||||
std::stringstream cerrCapture;
|
||||
CerrRedirect cerrRedirect {cerrCapture};
|
||||
|
||||
try
|
||||
{
|
||||
throw 7;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
terminateHandler();
|
||||
}
|
||||
{
|
||||
std::string result = cerrCapture.str();
|
||||
BEAST_EXPECT (result.find (threadName) != std::string::npos);
|
||||
BEAST_EXPECT (
|
||||
result.find ("unknown exception") != std::string::npos);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(TerminateHandler,core,ripple);
|
||||
|
||||
} // test
|
||||
} // ripple
|
||||
Reference in New Issue
Block a user