diff --git a/paddle/framework/channel_test.cc b/paddle/framework/channel_test.cc index 444d68498c967..f415e0e264cdb 100644 --- a/paddle/framework/channel_test.cc +++ b/paddle/framework/channel_test.cc @@ -60,6 +60,37 @@ TEST(Channel, SufficientBufferSizeDoesntBlock) { delete ch; } +// This test tests that CloseChannel returns a value of zero +// immediately to all receivers that are trying to receive from the channel. +TEST(Channel, ReceiverGetsZeroOnClosedBufferedChannel) { + const size_t buffer_size = 10; + auto ch = MakeChannel(buffer_size); + + for (size_t i = 1; i <= buffer_size; ++i) { + EXPECT_EQ(ch->Send(&i), true); // sending should not block + } + + for (size_t i = 1; i < buffer_size/2; ++i) { + int out; + EXPECT_EQ(ch->Receive(&out), true); + EXPECT_EQ(out, i); + } + CloseChannel(ch); + + // Now try receiving for more number of times than buffer size + // after channel is closed + + for (size_t i = buffer_size/2; i <= 12; ++i) { + int out; + ch->Receive(&out); + if (i <= buffer_size) + EXPECT_EQ(out, i); // same value as was written by senders + else + EXPECT_EQ(out, 0U); // 0 after all elements are emptied from a closed channel + } + delete ch; +} + TEST(Channel, ConcurrentSendNonConcurrentReceiveWithSufficientBufferSize) { const size_t buffer_size = 10; auto ch = MakeChannel(buffer_size); @@ -119,6 +150,7 @@ TEST(Channel, BufferedChannelCloseUnblocksReceiversTest) { int data; // All reads should return false EXPECT_EQ(ch->Receive(&data), false); + EXPECT_EQ(data, 0); *p = true; }, &thread_ended[i]); @@ -214,6 +246,7 @@ TEST(Channel, UnbufferedChannelCloseUnblocksReceiversTest) { [&](bool *p) { int data; EXPECT_EQ(ch->Receive(&data), false); + EXPECT_EQ(data, 0); *p = true; }, &thread_ended[i]);