Skip to content

Commit

Permalink
Receive from closed channel (#8175)
Browse files Browse the repository at this point in the history
* Add test case to return zero on a closed channel

* Rename method

* Fix test

* ReceiveFromBufferedChannelReturnResidualValuesTest

* Adding the variable and case for unbuffered channel

* Fix review comments

* Fix format

* Remove a zero-value comparison
  • Loading branch information
wangkuiyi authored Feb 6, 2018
1 parent de7fa8b commit 6024a17
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions paddle/framework/channel_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,38 @@ TEST(Channel, SufficientBufferSizeDoesntBlock) {
delete ch;
}

TEST(Channel, ReceiveFromBufferedChannelReturnResidualValuesTest) {
const size_t buffer_size = 10;
auto ch = MakeChannel<size_t>(buffer_size);

for (size_t i = 0; i < buffer_size; ++i) {
EXPECT_EQ(ch->Send(&i), true); // sending should not block
}

size_t out;
for (size_t i = 0; i < buffer_size / 2; ++i) {
EXPECT_EQ(ch->Receive(&out), true); // receiving should not block
EXPECT_EQ(out, i);
}

CloseChannel(ch);

for (size_t i = buffer_size / 2; i < buffer_size; ++i) {
EXPECT_EQ(ch->Receive(&out),
true); // receving should return residual values.
EXPECT_EQ(out, i);
}

for (size_t i = 0; i < buffer_size; ++i) {
EXPECT_EQ(ch->Receive(&out),
false); // after receiving residual values, return zeros.
// Note: we cannot check EXPECT_EQ(out, 0), because C++ doesn't
// define zero values like Go does.
}

delete ch;
}

TEST(Channel, ConcurrentSendNonConcurrentReceiveWithSufficientBufferSize) {
const size_t buffer_size = 10;
auto ch = MakeChannel<size_t>(buffer_size);
Expand Down

0 comments on commit 6024a17

Please sign in to comment.