Skip to content
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

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions paddle/framework/channel_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,33 @@ TEST(Channel, ConcurrentSendNonConcurrentReceiveWithSufficientBufferSize) {
delete ch;
}

void ReceiveEmptyChannelTest(Channel* ch) {
Copy link
Collaborator

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.

size_t current = 5;
Copy link
Collaborator

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

std::thread t([&]() {
// Read from channel
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
Copy link
Collaborator

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

Copy link
Author

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.

});
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;
Expand Down