-
Notifications
You must be signed in to change notification settings - Fork 596
Add unit tests for JsonRpcConnection
connection handling
#10568
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
Draft
jschmidt-icinga
wants to merge
8
commits into
master
Choose a base branch
from
json-rpc-unit-testing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d82b7e4
tests: fix min severity doesn't get updated
yhabteab 5fa68e6
Make timeouts in JsonRpcConnection configurable
jschmidt-icinga 4bd51fe
Cleanup non-existing method from JsonRpcConnection
jschmidt-icinga 5f3a8c6
Add Assert-Macros for the TestLogger
jschmidt-icinga f0e8191
Add header providing timed asserts for unit tests
jschmidt-icinga 51d2a85
Add unit-tests for JsonRpcConnection
jschmidt-icinga 3468bb1
Why is it always Windows?
jschmidt-icinga 20ea6d9
And only Windows?
jschmidt-icinga 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 hidden or 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 |
---|---|---|
|
@@ -4,7 +4,6 @@ on: | |
push: | ||
branches: | ||
- master | ||
pull_request: {} | ||
release: | ||
types: | ||
- published | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -38,7 +38,7 @@ JsonRpcConnection::JsonRpcConnection(const WaitGroup::Ptr& waitGroup, const Stri | |
JsonRpcConnection::JsonRpcConnection(const WaitGroup::Ptr& waitGroup, const String& identity, bool authenticated, | ||
const Shared<AsioTlsStream>::Ptr& stream, ConnectionRole role, boost::asio::io_context& io) | ||
: m_Identity(identity), m_Authenticated(authenticated), m_Stream(stream), m_Role(role), | ||
m_Timestamp(Utility::GetTime()), m_Seen(Utility::GetTime()), m_IoStrand(io), | ||
m_Timestamp(Utility::GetTime()), m_Seen(std::chrono::steady_clock::now()), m_IoStrand(io), | ||
m_OutgoingMessagesQueued(io), m_WriterDone(io), m_ShuttingDown(false), m_WaitGroup(waitGroup), | ||
m_CheckLivenessTimer(io), m_HeartbeatTimer(io) | ||
{ | ||
|
@@ -81,7 +81,7 @@ void JsonRpcConnection::HandleIncomingMessages(boost::asio::yield_context yc) | |
break; | ||
} | ||
|
||
m_Seen = Utility::GetTime(); | ||
m_Seen = std::chrono::steady_clock::now(); | ||
if (m_Endpoint) { | ||
m_Endpoint->AddMessageReceived(jsonString.GetLength()); | ||
} | ||
|
@@ -236,6 +236,11 @@ void JsonRpcConnection::SendRawMessage(const String& message) | |
}); | ||
} | ||
|
||
void JsonRpcConnection::SetLivenessTimeout(std::chrono::milliseconds timeout) | ||
{ | ||
m_LivenessTimeout = timeout; | ||
} | ||
|
||
void JsonRpcConnection::SendMessageInternal(const Dictionary::Ptr& message) | ||
{ | ||
if (m_ShuttingDown) { | ||
|
@@ -411,31 +416,53 @@ void JsonRpcConnection::CheckLiveness(boost::asio::yield_context yc) | |
* leaking the connection. Therefore close it after a timeout. | ||
*/ | ||
|
||
m_CheckLivenessTimer.expires_from_now(boost::posix_time::seconds(10)); | ||
auto anonymousTimeout = m_LivenessTimeout / 6; | ||
m_CheckLivenessTimer.expires_after(anonymousTimeout); | ||
m_CheckLivenessTimer.async_wait(yc[ec]); | ||
if (ec) { | ||
Log(LogCritical, "JsonRpcConnection") << "Error waiting for Liveness timer: " << ec.message(); | ||
} | ||
|
||
if (m_ShuttingDown) { | ||
return; | ||
} | ||
|
||
auto remote (m_Stream->lowest_layer().remote_endpoint()); | ||
{ | ||
auto remote(m_Stream->lowest_layer().remote_endpoint()); | ||
|
||
Log(LogInformation, "JsonRpcConnection") | ||
<< "Closing anonymous connection [" << remote.address() << "]:" << remote.port() << " after 10 seconds."; | ||
auto msg = Log(LogInformation, "JsonRpcConnection"); | ||
msg << "Closing anonymous connection [" << remote.address() << "]:" << remote.port() << " after "; | ||
if (anonymousTimeout > 1s) { | ||
msg << anonymousTimeout.count() / 1000 << " seconds."; | ||
} else { | ||
msg << anonymousTimeout.count() << " milliseconds"; | ||
} | ||
} | ||
|
||
Disconnect(); | ||
} else { | ||
for (;;) { | ||
m_CheckLivenessTimer.expires_from_now(boost::posix_time::seconds(30)); | ||
m_CheckLivenessTimer.expires_after(m_LivenessTimeout / 2); | ||
m_CheckLivenessTimer.async_wait(yc[ec]); | ||
if (ec) { | ||
Log(LogCritical, "JsonRpcConnection") << "Error waiting for Liveness timer: " << ec.message(); | ||
} | ||
|
||
if (m_ShuttingDown) { | ||
break; | ||
} | ||
|
||
if (m_Seen < Utility::GetTime() - 60 && (!m_Endpoint || !m_Endpoint->GetSyncing())) { | ||
Log(LogInformation, "JsonRpcConnection") | ||
<< "No messages for identity '" << m_Identity << "' have been received in the last 60 seconds."; | ||
if (m_Seen + m_LivenessTimeout < std::chrono::steady_clock::now() && | ||
(!m_Endpoint || !m_Endpoint->GetSyncing())) { | ||
{ | ||
auto msg = Log(LogInformation, "JsonRpcConnection"); | ||
msg << "No messages for identity '" << m_Identity << "' have been received in the last "; | ||
if (m_LivenessTimeout > 1s) { | ||
msg << m_LivenessTimeout.count() / 1000 << " seconds."; | ||
} else { | ||
msg << m_LivenessTimeout.count() << " milliseconds"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here! |
||
} | ||
} | ||
|
||
Disconnect(); | ||
break; | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
Just to be consistent with its sibling ;).
Also, if you really need the extra scoping for the log, I would also move the
remote
variable into that new scope.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.
Without it, the message would get printed after the message in Disconnect().