Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
01536e8d
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
01536e8d
编写于
2月 28, 2018
作者:
A
Abhinav Arora
提交者:
GitHub
2月 28, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Adding more unit tests for ChannelHolder class (#8668)
上级
12a3cea0
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
338 addition
and
0 deletion
+338
-0
paddle/fluid/framework/channel_test.cc
paddle/fluid/framework/channel_test.cc
+338
-0
未找到文件。
paddle/fluid/framework/channel_test.cc
浏览文件 @
01536e8d
...
...
@@ -542,3 +542,341 @@ TEST(ChannelHolder, ChannelHolderUnBufferedSendReceiveTest) {
ChannelHolderSendReceive
(
ch
);
delete
ch
;
}
TEST
(
ChannelHolder
,
ChannelUninitializedTest
)
{
ChannelHolder
*
ch
=
new
ChannelHolder
();
EXPECT_EQ
(
ch
->
IsInitialized
(),
false
);
int
i
=
10
;
EXPECT_EQ
(
ch
->
Send
(
&
i
),
false
);
EXPECT_EQ
(
ch
->
Receive
(
&
i
),
false
);
bool
is_exception
=
false
;
try
{
ch
->
Type
();
}
catch
(
paddle
::
platform
::
EnforceNotMet
e
)
{
is_exception
=
true
;
}
EXPECT_EQ
(
is_exception
,
true
);
delete
ch
;
}
TEST
(
ChannelHolder
,
ChannelInitializedTest
)
{
ChannelHolder
*
ch
=
new
ChannelHolder
();
ch
->
Reset
<
int
>
(
2
);
EXPECT_EQ
(
ch
->
IsInitialized
(),
true
);
// Channel should remain intialized even after close
ch
->
close
();
EXPECT_EQ
(
ch
->
IsInitialized
(),
true
);
delete
ch
;
}
TEST
(
ChannelHolder
,
TypeMismatchSendTest
)
{
// Test with unbuffered channel
ChannelHolder
*
ch
=
new
ChannelHolder
();
ch
->
Reset
<
int
>
(
0
);
bool
is_exception
=
false
;
bool
boolean_data
=
true
;
try
{
ch
->
Send
(
&
boolean_data
);
}
catch
(
paddle
::
platform
::
EnforceNotMet
e
)
{
is_exception
=
true
;
}
EXPECT_EQ
(
is_exception
,
true
);
delete
ch
;
// Test with Buffered Channel
ch
=
new
ChannelHolder
();
ch
->
Reset
<
float
>
(
10
);
is_exception
=
false
;
int
int_data
=
23
;
try
{
ch
->
Send
(
&
int_data
);
}
catch
(
paddle
::
platform
::
EnforceNotMet
e
)
{
is_exception
=
true
;
}
EXPECT_EQ
(
is_exception
,
true
);
delete
ch
;
}
TEST
(
ChannelHolder
,
TypeMismatchReceiveTest
)
{
// Test with unbuffered channel
ChannelHolder
*
ch
=
new
ChannelHolder
();
ch
->
Reset
<
int
>
(
0
);
bool
is_exception
=
false
;
bool
float_data
;
try
{
ch
->
Receive
(
&
float_data
);
}
catch
(
paddle
::
platform
::
EnforceNotMet
e
)
{
is_exception
=
true
;
}
EXPECT_EQ
(
is_exception
,
true
);
delete
ch
;
// Test with Buffered Channel
ch
=
new
ChannelHolder
();
ch
->
Reset
<
float
>
(
10
);
is_exception
=
false
;
int
int_data
=
23
;
try
{
ch
->
Receive
(
&
int_data
);
}
catch
(
paddle
::
platform
::
EnforceNotMet
e
)
{
is_exception
=
true
;
}
EXPECT_EQ
(
is_exception
,
true
);
delete
ch
;
}
void
ChannelHolderCloseUnblocksReceiversTest
(
ChannelHolder
*
ch
)
{
size_t
num_threads
=
5
;
std
::
thread
t
[
num_threads
];
bool
thread_ended
[
num_threads
];
// Launches threads that try to read and are blocked because of no writers
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
{
thread_ended
[
i
]
=
false
;
t
[
i
]
=
std
::
thread
(
[
&
](
bool
*
p
)
{
int
data
;
EXPECT_EQ
(
ch
->
Receive
(
&
data
),
false
);
*
p
=
true
;
},
&
thread_ended
[
i
]);
}
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
100
));
// wait 0.1 sec
// Verify that all the threads are blocked
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
{
EXPECT_EQ
(
thread_ended
[
i
],
false
);
}
// Explicitly close the channel
// This should unblock all receivers
ch
->
close
();
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
100
));
// wait 0.1 sec
// Verify that all threads got unblocked
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
{
EXPECT_EQ
(
thread_ended
[
i
],
true
);
}
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
t
[
i
].
join
();
}
void
ChannelHolderCloseUnblocksSendersTest
(
ChannelHolder
*
ch
,
bool
isBuffered
)
{
using
paddle
::
framework
::
details
::
Buffered
;
using
paddle
::
framework
::
details
::
UnBuffered
;
size_t
num_threads
=
5
;
std
::
thread
t
[
num_threads
];
bool
thread_ended
[
num_threads
];
bool
send_success
[
num_threads
];
// Launches threads that try to write and are blocked because of no readers
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
{
thread_ended
[
i
]
=
false
;
send_success
[
i
]
=
false
;
t
[
i
]
=
std
::
thread
(
[
&
](
bool
*
ended
,
bool
*
success
)
{
int
data
=
10
;
*
success
=
ch
->
Send
(
&
data
);
*
ended
=
true
;
},
&
thread_ended
[
i
],
&
send_success
[
i
]);
}
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
100
));
// wait
if
(
isBuffered
)
{
// If ch is Buffered, atleast 4 threads must be blocked.
int
ct
=
0
;
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
{
if
(
!
thread_ended
[
i
])
ct
++
;
}
EXPECT_GE
(
ct
,
4
);
}
else
{
// If ch is UnBuffered, all the threads should be blocked.
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
{
EXPECT_EQ
(
thread_ended
[
i
],
false
);
}
}
// Explicitly close the thread
// This should unblock all senders
ch
->
close
();
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
100
));
// wait
// Verify that all threads got unblocked
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
{
EXPECT_EQ
(
thread_ended
[
i
],
true
);
}
if
(
isBuffered
)
{
// Verify that only 1 send was successful
int
ct
=
0
;
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
{
if
(
send_success
[
i
])
ct
++
;
}
// Only 1 send must be successful
EXPECT_EQ
(
ct
,
1
);
}
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
t
[
i
].
join
();
}
// This tests that closing a channelholder unblocks
// any receivers waiting on the channel
TEST
(
ChannelHolder
,
ChannelHolderCloseUnblocksReceiversTest
)
{
// Check for buffered channel
ChannelHolder
*
ch
=
new
ChannelHolder
();
ch
->
Reset
<
int
>
(
1
);
ChannelHolderCloseUnblocksReceiversTest
(
ch
);
delete
ch
;
// Check for unbuffered channel
ch
=
new
ChannelHolder
();
ch
->
Reset
<
int
>
(
0
);
ChannelHolderCloseUnblocksReceiversTest
(
ch
);
delete
ch
;
}
// This tests that closing a channelholder unblocks
// any senders waiting for channel to have write space
TEST
(
Channel
,
ChannelHolderCloseUnblocksSendersTest
)
{
// Check for buffered channel
ChannelHolder
*
ch
=
new
ChannelHolder
();
ch
->
Reset
<
int
>
(
1
);
ChannelHolderCloseUnblocksSendersTest
(
ch
,
true
);
delete
ch
;
// Check for unbuffered channel
ch
=
new
ChannelHolder
();
ch
->
Reset
<
int
>
(
0
);
ChannelHolderCloseUnblocksSendersTest
(
ch
,
false
);
delete
ch
;
}
// This tests that destroying a channelholder unblocks
// any senders waiting for channel
void
ChannelHolderDestroyUnblockSenders
(
ChannelHolder
*
ch
,
bool
isBuffered
)
{
size_t
num_threads
=
5
;
std
::
thread
t
[
num_threads
];
bool
thread_ended
[
num_threads
];
bool
send_success
[
num_threads
];
// Launches threads that try to write and are blocked because of no readers
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
{
thread_ended
[
i
]
=
false
;
send_success
[
i
]
=
false
;
t
[
i
]
=
std
::
thread
(
[
&
](
bool
*
ended
,
bool
*
success
)
{
int
data
=
10
;
*
success
=
ch
->
Send
(
&
data
);
*
ended
=
true
;
},
&
thread_ended
[
i
],
&
send_success
[
i
]);
}
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
500
));
// wait 0.5 sec
if
(
isBuffered
)
{
// If channel is buffered, verify that atleast 4 threads are blocked
int
ct
=
0
;
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
{
if
(
thread_ended
[
i
]
==
false
)
ct
++
;
}
// Atleast 4 threads must be blocked
EXPECT_GE
(
ct
,
4
);
}
else
{
// Verify that all the threads are blocked
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
{
EXPECT_EQ
(
thread_ended
[
i
],
false
);
}
}
// Explicitly destroy the channel
delete
ch
;
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
200
));
// wait
// Verify that all threads got unblocked
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
{
EXPECT_EQ
(
thread_ended
[
i
],
true
);
}
// Count number of successfuld sends
int
ct
=
0
;
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
{
if
(
send_success
[
i
])
ct
++
;
}
if
(
isBuffered
)
{
// Only 1 send must be successful
EXPECT_EQ
(
ct
,
1
);
}
else
{
// In unbuffered channel, no send should be successful
EXPECT_EQ
(
ct
,
0
);
}
// Join all threads
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
t
[
i
].
join
();
}
// This tests that destroying a channelholder also unblocks
// any receivers waiting on the channel
void
ChannelHolderDestroyUnblockReceivers
(
ChannelHolder
*
ch
)
{
size_t
num_threads
=
5
;
std
::
thread
t
[
num_threads
];
bool
thread_ended
[
num_threads
];
// Launches threads that try to read and are blocked because of no writers
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
{
thread_ended
[
i
]
=
false
;
t
[
i
]
=
std
::
thread
(
[
&
](
bool
*
p
)
{
int
data
;
// All reads should return false
EXPECT_EQ
(
ch
->
Receive
(
&
data
),
false
);
*
p
=
true
;
},
&
thread_ended
[
i
]);
}
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
100
));
// wait
// Verify that all threads are blocked
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
{
EXPECT_EQ
(
thread_ended
[
i
],
false
);
}
// delete the channel
delete
ch
;
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
200
));
// wait
// Verify that all threads got unblocked
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
{
EXPECT_EQ
(
thread_ended
[
i
],
true
);
}
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
t
[
i
].
join
();
}
TEST
(
ChannelHolder
,
ChannelHolderDestroyUnblocksReceiversTest
)
{
// Check for Buffered Channel
ChannelHolder
*
ch
=
new
ChannelHolder
();
ch
->
Reset
<
int
>
(
1
);
ChannelHolderDestroyUnblockReceivers
(
ch
);
// ch is already deleted already deleted in
// ChannelHolderDestroyUnblockReceivers
// Check for Unbuffered channel
ch
=
new
ChannelHolder
();
ch
->
Reset
<
int
>
(
0
);
ChannelHolderDestroyUnblockReceivers
(
ch
);
}
TEST
(
ChannelHolder
,
ChannelHolderDestroyUnblocksSendersTest
)
{
// Check for Buffered Channel
ChannelHolder
*
ch
=
new
ChannelHolder
();
ch
->
Reset
<
int
>
(
1
);
ChannelHolderDestroyUnblockSenders
(
ch
,
true
);
// ch is already deleted already deleted in
// ChannelHolderDestroyUnblockReceivers
// Check for Unbuffered channel
ch
=
new
ChannelHolder
();
ch
->
Reset
<
int
>
(
0
);
ChannelHolderDestroyUnblockSenders
(
ch
,
false
);
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录