-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Test case to check if receiver blocks on empty channel, and resumes on close. #8166
Conversation
// Read from channel | ||
int out; | ||
ch->Receive(&out); // should block since channel is empty | ||
current = 10; |
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.
10 is a magical number
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.
Thank you for the review.
I just realized that we already checked in the test cases : BufferedChannelCloseUnblocksReceiversTest and UnbufferedChannelCloseUnblocksReceiversTest that perform these operations already.
In that case, this PR would be redundant and maybe I can close it.
@@ -82,6 +82,33 @@ TEST(Channel, ConcurrentSendNonConcurrentReceiveWithSufficientBufferSize) { | |||
delete ch; | |||
} | |||
|
|||
void ReceiveEmptyChannelTest(Channel* ch) { | |||
size_t current = 5; |
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.
5 is a magical number
void ReceiveEmptyChannelTest(Channel* ch) { | ||
size_t current = 5; | ||
std::thread t([&]() { | ||
// Read from channel |
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.
unnecessary comment
@@ -82,6 +82,33 @@ TEST(Channel, ConcurrentSendNonConcurrentReceiveWithSufficientBufferSize) { | |||
delete ch; | |||
} | |||
|
|||
void ReceiveEmptyChannelTest(Channel* ch) { |
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.
The prefix Test is a convention of unit test names. This function is not a unit test; a better name could be ReceiveFromEmptyChannelShouldBlock
.
A rewrite is as follows: void ReceiveFromEmptyChannelShouldBlockAndCloseUnblocksIt(Channel *ch) {
bool received = false;
std::thread t([&]() {
int out;
ch->Receive(&out); // should block since channel is empty
received = true;
});
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // wait 0.1 sec
EXPECT_EQ(received,
false); // the receiver should be blocked since channel is empty
CloseChannel(ch);
t.join();
EXPECT_EQ(received,
true); // when we close the channel, the receiver should unblock
delete ch;
}
TEST(Channel, ReceiveFromEmptyBufferedChannelShouldBlockAndCloseUnblocksIt) {
auto ch = MakeChannel<size_t>(3); // buffered
ReceiveFromEmptyChannelShouldBlockAndCloseUnblocksIt(ch);
}
TEST(Channel, ReceiveFromEmptyUnBufferedChannelShouldBlockAndCloseUnblocksIt) {
auto ch = MakeChannel<size_t>(0); // unbuffered
ReceiveEmptyChannelTest(ch);
} |
No description provided.