Skip to content

Commit

Permalink
unit test no longer runs forever
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Nov 16, 2024
1 parent 2c3e701 commit 7994a4a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
13 changes: 11 additions & 2 deletions include/dpp/cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ class DPP_EXPORT cluster {
*/
std::unique_ptr<thread_pool> pool{nullptr};

/**
* @brief Used to spawn the socket engine into its own thread if
* the cluster is started with dpp::st_return. It is unused otherwise.
*/
std::unique_ptr<std::jthread> engine_thread{nullptr};

public:
/**
Expand Down Expand Up @@ -233,9 +238,13 @@ class DPP_EXPORT cluster {
websocket_protocol_t ws_mode;

/**
* @brief Condition variable notified when the cluster is terminating.
* @brief Atomic bool to set to true when the cluster is terminating.
*
* D++ itself does not set this value, it is for library users to set if they want
* the cluster to terminate outside of a flow where they may have simple access to
* destruct the cluster object.
*/
std::condition_variable terminating;
std::atomic_bool terminating{false};

/**
* @brief The time (in seconds) that a request is allowed to take.
Expand Down
22 changes: 11 additions & 11 deletions src/dpp/cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@ dpp::utility::uptime cluster::uptime()

void cluster::start(bool return_after) {

auto block_calling_thread = [this]() {
std::mutex thread_mutex;
std::unique_lock thread_lock(thread_mutex);
this->terminating.wait(thread_lock);
auto event_loop = [this]() -> void {
do {
socketengine->process_events();
} while (!this->terminating);
};

if (on_guild_member_add && !(intents & dpp::i_guild_members)) {
Expand Down Expand Up @@ -300,13 +300,13 @@ void cluster::start(bool return_after) {
log(ll_debug, "Shards started.");
});

do {
// TODO: Thread this
socketengine->process_events();
} while (true);

if (!return_after) {
block_calling_thread();
if (return_after) {
engine_thread = std::make_unique<std::jthread>(std::jthread([event_loop]() {
dpp::utility::set_thread_name("event_loop");
event_loop();
}));
} else {
event_loop();
}
}

Expand Down
6 changes: 0 additions & 6 deletions src/unittest/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1163,12 +1163,6 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
}
});

set_test(SYNC, false);
if (!offline) {
dpp::message m = dpp::sync<dpp::message>(&bot, &dpp::cluster::message_create, dpp::message(TEST_TEXT_CHANNEL_ID, "TEST"));
set_test(SYNC, m.content == "TEST");
}

bot.on_guild_create([&](const dpp::guild_create_t & event) {
if (event.created->id == TEST_GUILD_ID) {
set_test(GUILDCREATE, true);
Expand Down
1 change: 0 additions & 1 deletion src/unittest/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ DPP_TEST(MD_ESC_1, "Markdown escaping (ignore code block contents)", tf_offline)
DPP_TEST(MD_ESC_2, "Markdown escaping (escape code block contents)", tf_offline);
DPP_TEST(URLENC, "URL encoding", tf_offline);
DPP_TEST(BASE64ENC, "Base 64 encoding", tf_offline);
DPP_TEST(SYNC, "sync<T>()", tf_online);
DPP_TEST(COMPARISON, "manged object comparison", tf_offline);
DPP_TEST(CHANNELCACHE, "find_channel()", tf_online);
DPP_TEST(CHANNELTYPES, "channel type flags", tf_online);
Expand Down

0 comments on commit 7994a4a

Please sign in to comment.