From 6024a170f321e3ed572260e68cad39820f15ff67 Mon Sep 17 00:00:00 2001 From: Yi Wang Date: Tue, 6 Feb 2018 05:36:50 -0800 Subject: [PATCH] Receive from closed channel (#8175) * 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 --- paddle/framework/channel_test.cc | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/paddle/framework/channel_test.cc b/paddle/framework/channel_test.cc index 444d68498c9..6416c04f36e 100644 --- a/paddle/framework/channel_test.cc +++ b/paddle/framework/channel_test.cc @@ -60,6 +60,38 @@ TEST(Channel, SufficientBufferSizeDoesntBlock) { delete ch; } +TEST(Channel, ReceiveFromBufferedChannelReturnResidualValuesTest) { + const size_t buffer_size = 10; + auto ch = MakeChannel(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(buffer_size); -- GitLab