Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
kep-808 - a garbage collection callback is added to a session as it i…
Browse files Browse the repository at this point in the history
…s added to the session pool
  • Loading branch information
rnistuk authored and rnistuk committed Feb 28, 2019
1 parent cc6b8d7 commit 3728307
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pbft/pbft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1894,7 +1894,7 @@ void pbft::add_session_to_pool(const std::string& msg_hash, std::shared_ptr<bzn:
if (session)
{
this->sessions_waiting_on_forwarded_requests[msg_hash] = session;
session->add_shutdown_handler([&]()
session->add_shutdown_handler([msg_hash, this]()
{
std::lock_guard<std::mutex> lock(this->pbft_lock);
auto it = this->sessions_waiting_on_forwarded_requests.find(msg_hash);
Expand Down
2 changes: 2 additions & 0 deletions pbft/pbft.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ namespace bzn
{
// fwd declare test as it's not in the same namespace...
class pbft_test_database_response_is_forwarded_to_session_Test;
class pbft_test_add_session_to_pool_can_add_a_session_and_shutdown_handler_removes_session_from_pool_Test;
}

using request_hash_t = std::string;
Expand Down Expand Up @@ -298,6 +299,7 @@ namespace bzn
FRIEND_TEST(pbft_newview_test, get_sequences_and_request_hashes_from_proofs);
FRIEND_TEST(pbft_newview_test, test_last_sequence_in_newview_prepared_proofs);
FRIEND_TEST(bzn::test::pbft_test, database_response_is_forwarded_to_session);
FRIEND_TEST(bzn::test::pbft_test, add_session_to_pool_can_add_a_session_and_shutdown_handler_removes_session_from_pool);

friend class pbft_proto_test;
friend class pbft_join_leave_test;
Expand Down
28 changes: 28 additions & 0 deletions pbft/test/pbft_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,34 @@ namespace bzn::test
this->database_response_handler(this->request_msg, mock_session);
}

TEST_F(pbft_test, add_session_to_pool_can_add_a_session_and_shutdown_handler_removes_session_from_pool)
{
this->build_pbft();

EXPECT_EQ(size_t(0), this->pbft->sessions_waiting_on_forwarded_requests.size());

bzn::session_shutdown_handler shutdown_handler{0};

EXPECT_CALL(*mock_session, add_shutdown_handler(_))
.Times(Exactly(1))
.WillRepeatedly(Invoke([&](auto handler) {
shutdown_handler = handler;
}));

EXPECT_CALL(*mock_session, is_open())
.WillOnce(Return(false));

pbft->handle_database_message(this->request_msg, this->mock_session);

EXPECT_EQ(size_t(1), this->pbft->sessions_waiting_on_forwarded_requests.size());

EXPECT_TRUE(shutdown_handler != nullptr);

shutdown_handler();

EXPECT_EQ(size_t(0), this->pbft->sessions_waiting_on_forwarded_requests.size());
}


TEST_F(pbft_test, client_request_executed_results_in_message_response)
{
Expand Down

0 comments on commit 3728307

Please sign in to comment.