未验证 提交 6e7fee0e 编写于 作者: A Abhinav Arora 提交者: GitHub

Add unit tests for ChannelHolder (#8510)

上级 bc0f04df
...@@ -100,8 +100,7 @@ class ChannelHolder { ...@@ -100,8 +100,7 @@ class ChannelHolder {
virtual ~Placeholder() {} virtual ~Placeholder() {}
virtual const std::type_index Type() const = 0; virtual const std::type_index Type() const = 0;
virtual void* Ptr() const = 0; virtual void* Ptr() const = 0;
virtual void Close() const = 0; virtual void Close() = 0;
std::type_info type_;
}; };
template <typename T> template <typename T>
...@@ -116,7 +115,7 @@ class ChannelHolder { ...@@ -116,7 +115,7 @@ class ChannelHolder {
if (channel_) channel_->Close(); if (channel_) channel_->Close();
} }
std::unique_ptr<Channel<T>*> channel_; std::unique_ptr<Channel<T>> channel_;
const std::type_index type_; const std::type_index type_;
}; };
......
...@@ -20,6 +20,7 @@ limitations under the License. */ ...@@ -20,6 +20,7 @@ limitations under the License. */
#include "gtest/gtest.h" #include "gtest/gtest.h"
using paddle::framework::Channel; using paddle::framework::Channel;
using paddle::framework::ChannelHolder;
using paddle::framework::MakeChannel; using paddle::framework::MakeChannel;
using paddle::framework::CloseChannel; using paddle::framework::CloseChannel;
using paddle::framework::details::Buffered; using paddle::framework::details::Buffered;
...@@ -508,3 +509,36 @@ TEST(Channel, UnbufferedChannelDestroyUnblocksSendersTest) { ...@@ -508,3 +509,36 @@ TEST(Channel, UnbufferedChannelDestroyUnblocksSendersTest) {
auto ch = MakeChannel<int>(0); auto ch = MakeChannel<int>(0);
ChannelDestroyUnblockSenders(ch); ChannelDestroyUnblockSenders(ch);
} }
void ChannelHolderSendReceive(ChannelHolder *ch) {
unsigned sum_send = 0;
std::thread t([&]() {
for (int i = 0; i < 5; i++) {
EXPECT_EQ(ch->Send(&i), true);
sum_send += i;
}
});
for (int i = 0; i < 5; i++) {
int recv;
EXPECT_EQ(ch->Receive(&recv), true);
EXPECT_EQ(recv, i);
}
ch->close();
t.join();
EXPECT_EQ(sum_send, 10U);
}
TEST(ChannelHolder, ChannelHolderBufferedSendReceiveTest) {
ChannelHolder *ch = new ChannelHolder();
ch->Reset<int>(10);
ChannelHolderSendReceive(ch);
delete ch;
}
TEST(ChannelHolder, ChannelHolderUnBufferedSendReceiveTest) {
ChannelHolder *ch = new ChannelHolder();
ch->Reset<int>(0);
ChannelHolderSendReceive(ch);
delete ch;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册