-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. 5 is a magical number |
||
std::thread t([&]() { | ||
// Read from channel | ||
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. unnecessary comment |
||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the review. |
||
}); | ||
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // wait 0.1 sec | ||
EXPECT_EQ(current, 5U); // the receiver should be blocked since channel is empty | ||
|
||
CloseChannel(ch); | ||
t.join(); | ||
EXPECT_EQ(current, 10U); // when we close the channel, the receiver should unblock | ||
delete ch; | ||
} | ||
|
||
TEST(Channel, BufferedReceiveEmptyChannelTest) { | ||
auto ch = MakeChannel<size_t>(3); | ||
ReceiveEmptyChannelTest(ch); | ||
} | ||
|
||
TEST(Channel, UnBufferedReceiveEmptyChannelTest) { | ||
auto ch = MakeChannel<size_t>(0); | ||
ReceiveEmptyChannelTest(ch); | ||
} | ||
|
||
TEST(Channel, SimpleUnbufferedChannelTest) { | ||
auto ch = MakeChannel<int>(0); | ||
unsigned sum_send = 0; | ||
|
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
.