-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Drain io_context after shutdown of plugins #34
Merged
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9fafe2e
Clear io_context before destroying plugins
heifner 9a0156a
Clear io_context before destroying plugins
heifner d15b6f3
Add shutdown test. Thanks to @spoonincode
heifner da11ec0
Move destroy of plugins to after io_context clear.
heifner 6bfc1be
Destroy io_context after plugin shutdown to clear it out before plugi…
heifner fd7cf03
Add includes
heifner 454a161
Add include
heifner 71ed3d7
Drain the io_context instead of destroying it
heifner d1cf9fc
Call quit() so is_quitting is set
heifner c719ef2
Add a make_timer method
heifner 284bcd8
Use io_context as io_service is old deprecated name
heifner 24d85e1
Rename setup_signal_handling_on_ios to setup_signal_handling_on_ioc
heifner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include <appbase/application.hpp> | ||
#include <thread> | ||
|
||
#include <boost/test/unit_test.hpp> | ||
|
||
namespace bpo = boost::program_options; | ||
using bpo::options_description; | ||
using bpo::variables_map; | ||
|
||
static bool thing = true; | ||
struct thing_better_be_alive { | ||
~thing_better_be_alive() noexcept(false) { | ||
if(!thing) | ||
throw "BOOM"; | ||
} | ||
}; | ||
|
||
class thready_plugin : public appbase::plugin<thready_plugin> { | ||
public: | ||
|
||
template <typename Lambda> | ||
void plugin_requires(Lambda&& l) {} | ||
|
||
void set_program_options( options_description& cli, options_description& cfg ) override {} | ||
|
||
void thread_work() { | ||
boost::asio::post(ctx, [&]() { | ||
thing_better_be_alive better_be; | ||
boost::asio::post(appbase::app().get_io_service(), [&,is_it=std::move(better_be)]() { | ||
thread_work(); | ||
}); | ||
}); | ||
} | ||
|
||
void plugin_initialize( const variables_map& options ) {} | ||
void plugin_startup() { | ||
for(unsigned i = 0; i < 48*4; i++) | ||
thread_work(); | ||
|
||
for(unsigned i = 0; i < 48; ++i) | ||
threads.emplace_back([this]() { | ||
ctx.run(); | ||
}); | ||
} | ||
void plugin_shutdown() { | ||
usleep(100000); //oh gee it takes a while to stop | ||
ctx.stop(); | ||
for(unsigned i = 0; i < 48; ++i) | ||
threads[i].join(); | ||
} | ||
|
||
~thready_plugin() { | ||
thing = false; | ||
} | ||
|
||
boost::asio::io_context ctx; | ||
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> wg = boost::asio::make_work_guard(ctx); | ||
|
||
private: | ||
std::vector<std::thread> threads; | ||
}; | ||
|
||
|
||
BOOST_AUTO_TEST_CASE(test_shutdown) | ||
{ | ||
appbase::application::register_plugin<thready_plugin>(); | ||
appbase::scoped_app app; | ||
|
||
const char* argv[] = { "nodoes" }; | ||
if( !app->initialize<thready_plugin>( 1, const_cast<char**>(argv) ) ) | ||
return; | ||
app->startup(); | ||
boost::asio::post(appbase::app().get_io_service(), [&](){ | ||
std::this_thread::sleep_for(std::chrono::milliseconds(5)); | ||
app->quit(); | ||
}); | ||
app->exec(); | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You added this file as a test but didn't plumb it through cmake etc. not sure if that's intentional or not
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests/CMakeFiles.txt
hasfile(GLOB UNIT_TESTS "*.cpp")
I verified it runs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I completely missed the glob 👍