未验证 提交 7bf82f82 编写于 作者: A Abhinav Arora 提交者: GitHub

Fix CPPlint errors in channel.h, channel_impl.h and channel_test.cc (#9628)

* Fix cpplint issues

* Fix cpplint issues in channel.h and channel_impl.h

* Fix typo
上级 d904b3dd
...@@ -14,8 +14,8 @@ limitations under the License. */ ...@@ -14,8 +14,8 @@ limitations under the License. */
#pragma once #pragma once
#include <stddef.h> // for size_t #include <stddef.h> // for size_t
#include <condition_variable> #include <condition_variable> // NOLINT
#include <typeindex> #include <typeindex>
#include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/platform/enforce.h"
...@@ -216,7 +216,8 @@ class ChannelHolder { ...@@ -216,7 +216,8 @@ class ChannelHolder {
template <typename T> template <typename T>
struct PlaceholderImpl : public Placeholder { struct PlaceholderImpl : public Placeholder {
PlaceholderImpl(size_t buffer_size) : type_(std::type_index(typeid(T))) { explicit PlaceholderImpl(size_t buffer_size)
: type_(std::type_index(typeid(T))) {
channel_.reset(MakeChannel<T>(buffer_size)); channel_.reset(MakeChannel<T>(buffer_size));
} }
......
...@@ -15,7 +15,7 @@ limitations under the License. */ ...@@ -15,7 +15,7 @@ limitations under the License. */
#pragma once #pragma once
#include <stddef.h> // for size_t #include <stddef.h> // for size_t
#include <atomic> #include <atomic>
#include <condition_variable> #include <condition_variable> // NOLINT
#include <deque> #include <deque>
#include "paddle/fluid/framework/channel.h" #include "paddle/fluid/framework/channel.h"
#include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/platform/enforce.h"
...@@ -38,7 +38,7 @@ class ChannelImpl : public paddle::framework::Channel<T> { ...@@ -38,7 +38,7 @@ class ChannelImpl : public paddle::framework::Channel<T> {
virtual void Unlock(); virtual void Unlock();
virtual bool IsClosed(); virtual bool IsClosed();
virtual void Close(); virtual void Close();
ChannelImpl(size_t); explicit ChannelImpl(size_t);
virtual ~ChannelImpl(); virtual ~ChannelImpl();
virtual void AddToSendQ(const void *referrer, T *data, virtual void AddToSendQ(const void *referrer, T *data,
...@@ -60,7 +60,7 @@ class ChannelImpl : public paddle::framework::Channel<T> { ...@@ -60,7 +60,7 @@ class ChannelImpl : public paddle::framework::Channel<T> {
const void *referrer; // TODO(thuan): figure out better way to do this const void *referrer; // TODO(thuan): figure out better way to do this
std::function<bool(ChannelAction)> callback; std::function<bool(ChannelAction)> callback;
QueueMessage(T *item) explicit QueueMessage(T *item)
: data(item), cond(std::make_shared<std::condition_variable_any>()) {} : data(item), cond(std::make_shared<std::condition_variable_any>()) {}
QueueMessage(T *item, std::shared_ptr<std::condition_variable_any> cond) QueueMessage(T *item, std::shared_ptr<std::condition_variable_any> cond)
...@@ -88,15 +88,15 @@ class ChannelImpl : public paddle::framework::Channel<T> { ...@@ -88,15 +88,15 @@ class ChannelImpl : public paddle::framework::Channel<T> {
} }
std::shared_ptr<QueueMessage> get_first_message( std::shared_ptr<QueueMessage> get_first_message(
std::deque<std::shared_ptr<QueueMessage>> &queue, ChannelAction action) { std::deque<std::shared_ptr<QueueMessage>> *queue, ChannelAction action) {
while (!queue.empty()) { while (!queue->empty()) {
// Check whether this message was added by Select // Check whether this message was added by Select
// If this was added by Select then execute the callback // If this was added by Select then execute the callback
// to check if you can execute this message. The callback // to check if you can execute this message. The callback
// can return false if some other case was executed in Select. // can return false if some other case was executed in Select.
// In that case just discard this QueueMessage and process next. // In that case just discard this QueueMessage and process next.
std::shared_ptr<QueueMessage> m = queue.front(); std::shared_ptr<QueueMessage> m = queue->front();
queue.pop_front(); queue->pop_front();
if (m->callback == nullptr || m->callback(action)) return m; if (m->callback == nullptr || m->callback(action)) return m;
} }
return nullptr; return nullptr;
...@@ -147,7 +147,7 @@ void ChannelImpl<T>::Send(T *item) { ...@@ -147,7 +147,7 @@ void ChannelImpl<T>::Send(T *item) {
// to send to the receiver, bypassing the channel buffer if any // to send to the receiver, bypassing the channel buffer if any
if (!recvq.empty()) { if (!recvq.empty()) {
std::shared_ptr<QueueMessage> m = std::shared_ptr<QueueMessage> m =
get_first_message(recvq, ChannelAction::SEND); get_first_message(&recvq, ChannelAction::SEND);
if (m != nullptr) { if (m != nullptr) {
*(m->data) = std::move(*item); *(m->data) = std::move(*item);
...@@ -198,7 +198,7 @@ bool ChannelImpl<T>::Receive(T *item) { ...@@ -198,7 +198,7 @@ bool ChannelImpl<T>::Receive(T *item) {
// buffer and move front of send queue to the buffer // buffer and move front of send queue to the buffer
if (!sendq.empty()) { if (!sendq.empty()) {
std::shared_ptr<QueueMessage> m = std::shared_ptr<QueueMessage> m =
get_first_message(sendq, ChannelAction::RECEIVE); get_first_message(&sendq, ChannelAction::RECEIVE);
if (buf_.size() > 0) { if (buf_.size() > 0) {
// Case 1 : Channel is Buffered // Case 1 : Channel is Buffered
// Do Data transfer from front of buffer // Do Data transfer from front of buffer
...@@ -219,8 +219,9 @@ bool ChannelImpl<T>::Receive(T *item) { ...@@ -219,8 +219,9 @@ bool ChannelImpl<T>::Receive(T *item) {
if (m != nullptr) { if (m != nullptr) {
*item = std::move(*(m->data)); *item = std::move(*(m->data));
m->Notify(); m->Notify();
} else } else {
return recv_return(Receive(item)); return recv_return(Receive(item));
}
} }
return recv_return(true); return recv_return(true);
} }
......
...@@ -14,8 +14,8 @@ limitations under the License. */ ...@@ -14,8 +14,8 @@ limitations under the License. */
#include "paddle/fluid/framework/channel.h" #include "paddle/fluid/framework/channel.h"
#include <chrono> #include <chrono> // NOLINT
#include <thread> #include <thread> // NOLINT
#include "gtest/gtest.h" #include "gtest/gtest.h"
using paddle::framework::Channel; using paddle::framework::Channel;
...@@ -166,9 +166,9 @@ TEST(Channel, ConcurrentSendNonConcurrentReceiveWithSufficientBufferSize) { ...@@ -166,9 +166,9 @@ TEST(Channel, ConcurrentSendNonConcurrentReceiveWithSufficientBufferSize) {
std::thread t([&]() { std::thread t([&]() {
// Try to write more than buffer size. // Try to write more than buffer size.
for (size_t i = 0; i < 2 * buffer_size; ++i) { for (size_t i = 0; i < 2 * buffer_size; ++i) {
if (i < buffer_size) if (i < buffer_size) {
ch->Send(&i); // should block after 10 iterations ch->Send(&i); // should block after 10 iterations
else { } else {
bool is_exception = false; bool is_exception = false;
try { try {
ch->Send(&i); ch->Send(&i);
...@@ -212,12 +212,12 @@ TEST(Channel, RecevingOrderEqualToSendingOrderWithBufferedChannel3) { ...@@ -212,12 +212,12 @@ TEST(Channel, RecevingOrderEqualToSendingOrderWithBufferedChannel3) {
} }
void ChannelCloseUnblocksReceiversTest(Channel<int> *ch) { void ChannelCloseUnblocksReceiversTest(Channel<int> *ch) {
size_t num_threads = 5; const size_t kNumThreads = 5;
std::thread t[num_threads]; std::thread t[kNumThreads];
bool thread_ended[num_threads]; bool thread_ended[kNumThreads];
// Launches threads that try to read and are blocked because of no writers // Launches threads that try to read and are blocked because of no writers
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
thread_ended[i] = false; thread_ended[i] = false;
t[i] = std::thread( t[i] = std::thread(
[&](bool *p) { [&](bool *p) {
...@@ -230,7 +230,7 @@ void ChannelCloseUnblocksReceiversTest(Channel<int> *ch) { ...@@ -230,7 +230,7 @@ void ChannelCloseUnblocksReceiversTest(Channel<int> *ch) {
std::this_thread::sleep_for(std::chrono::milliseconds(200)); // wait 0.2 sec std::this_thread::sleep_for(std::chrono::milliseconds(200)); // wait 0.2 sec
// Verify that all the threads are blocked // Verify that all the threads are blocked
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
EXPECT_EQ(thread_ended[i], false); EXPECT_EQ(thread_ended[i], false);
} }
...@@ -241,21 +241,21 @@ void ChannelCloseUnblocksReceiversTest(Channel<int> *ch) { ...@@ -241,21 +241,21 @@ void ChannelCloseUnblocksReceiversTest(Channel<int> *ch) {
std::this_thread::sleep_for(std::chrono::milliseconds(200)); // wait 0.2 sec std::this_thread::sleep_for(std::chrono::milliseconds(200)); // wait 0.2 sec
// Verify that all threads got unblocked // Verify that all threads got unblocked
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
EXPECT_EQ(thread_ended[i], true); EXPECT_EQ(thread_ended[i], true);
} }
for (size_t i = 0; i < num_threads; i++) t[i].join(); for (size_t i = 0; i < kNumThreads; i++) t[i].join();
} }
void ChannelCloseUnblocksSendersTest(Channel<int> *ch, bool isBuffered) { void ChannelCloseUnblocksSendersTest(Channel<int> *ch, bool isBuffered) {
size_t num_threads = 5; const size_t kNumThreads = 5;
std::thread t[num_threads]; std::thread t[kNumThreads];
bool thread_ended[num_threads]; bool thread_ended[kNumThreads];
bool send_success[num_threads]; bool send_success[kNumThreads];
// Launches threads that try to write and are blocked because of no readers // Launches threads that try to write and are blocked because of no readers
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
thread_ended[i] = false; thread_ended[i] = false;
send_success[i] = false; send_success[i] = false;
t[i] = std::thread( t[i] = std::thread(
...@@ -277,13 +277,13 @@ void ChannelCloseUnblocksSendersTest(Channel<int> *ch, bool isBuffered) { ...@@ -277,13 +277,13 @@ void ChannelCloseUnblocksSendersTest(Channel<int> *ch, bool isBuffered) {
if (isBuffered) { if (isBuffered) {
// If ch is Buffered, atleast 4 threads must be blocked. // If ch is Buffered, atleast 4 threads must be blocked.
int ct = 0; int ct = 0;
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
if (!thread_ended[i]) ct++; if (!thread_ended[i]) ct++;
} }
EXPECT_GE(ct, 4); EXPECT_GE(ct, 4);
} else { } else {
// If ch is UnBuffered, all the threads should be blocked. // If ch is UnBuffered, all the threads should be blocked.
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
EXPECT_EQ(thread_ended[i], false); EXPECT_EQ(thread_ended[i], false);
} }
} }
...@@ -294,21 +294,21 @@ void ChannelCloseUnblocksSendersTest(Channel<int> *ch, bool isBuffered) { ...@@ -294,21 +294,21 @@ void ChannelCloseUnblocksSendersTest(Channel<int> *ch, bool isBuffered) {
std::this_thread::sleep_for(std::chrono::milliseconds(200)); // wait std::this_thread::sleep_for(std::chrono::milliseconds(200)); // wait
// Verify that all threads got unblocked // Verify that all threads got unblocked
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
EXPECT_EQ(thread_ended[i], true); EXPECT_EQ(thread_ended[i], true);
} }
if (isBuffered) { if (isBuffered) {
// Verify that only 1 send was successful // Verify that only 1 send was successful
int ct = 0; int ct = 0;
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
if (send_success[i]) ct++; if (send_success[i]) ct++;
} }
// Only 1 send must be successful // Only 1 send must be successful
EXPECT_EQ(ct, 1); EXPECT_EQ(ct, 1);
} }
for (size_t i = 0; i < num_threads; i++) t[i].join(); for (size_t i = 0; i < kNumThreads; i++) t[i].join();
} }
// This tests that closing a buffered channel also unblocks // This tests that closing a buffered channel also unblocks
...@@ -409,13 +409,13 @@ TEST(Channel, UnbufferedMoreReceiveLessSendTest) { ...@@ -409,13 +409,13 @@ TEST(Channel, UnbufferedMoreReceiveLessSendTest) {
// This tests that destroying a channel unblocks // This tests that destroying a channel unblocks
// any senders waiting for channel to have write space // any senders waiting for channel to have write space
void ChannelDestroyUnblockSenders(Channel<int> *ch, bool isBuffered) { void ChannelDestroyUnblockSenders(Channel<int> *ch, bool isBuffered) {
size_t num_threads = 5; const size_t kNumThreads = 5;
std::thread t[num_threads]; std::thread t[kNumThreads];
bool thread_ended[num_threads]; bool thread_ended[kNumThreads];
bool send_success[num_threads]; bool send_success[kNumThreads];
// Launches threads that try to write and are blocked because of no readers // Launches threads that try to write and are blocked because of no readers
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
thread_ended[i] = false; thread_ended[i] = false;
send_success[i] = false; send_success[i] = false;
t[i] = std::thread( t[i] = std::thread(
...@@ -438,14 +438,14 @@ void ChannelDestroyUnblockSenders(Channel<int> *ch, bool isBuffered) { ...@@ -438,14 +438,14 @@ void ChannelDestroyUnblockSenders(Channel<int> *ch, bool isBuffered) {
if (isBuffered) { if (isBuffered) {
// If channel is buffered, verify that atleast 4 threads are blocked // If channel is buffered, verify that atleast 4 threads are blocked
int ct = 0; int ct = 0;
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
if (thread_ended[i] == false) ct++; if (thread_ended[i] == false) ct++;
} }
// Atleast 4 threads must be blocked // Atleast 4 threads must be blocked
EXPECT_GE(ct, 4); EXPECT_GE(ct, 4);
} else { } else {
// Verify that all the threads are blocked // Verify that all the threads are blocked
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
EXPECT_EQ(thread_ended[i], false); EXPECT_EQ(thread_ended[i], false);
} }
} }
...@@ -454,13 +454,13 @@ void ChannelDestroyUnblockSenders(Channel<int> *ch, bool isBuffered) { ...@@ -454,13 +454,13 @@ void ChannelDestroyUnblockSenders(Channel<int> *ch, bool isBuffered) {
std::this_thread::sleep_for(std::chrono::milliseconds(200)); // wait std::this_thread::sleep_for(std::chrono::milliseconds(200)); // wait
// Verify that all threads got unblocked // Verify that all threads got unblocked
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
EXPECT_EQ(thread_ended[i], true); EXPECT_EQ(thread_ended[i], true);
} }
// Count number of successful sends // Count number of successful sends
int ct = 0; int ct = 0;
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
if (send_success[i]) ct++; if (send_success[i]) ct++;
} }
...@@ -473,18 +473,18 @@ void ChannelDestroyUnblockSenders(Channel<int> *ch, bool isBuffered) { ...@@ -473,18 +473,18 @@ void ChannelDestroyUnblockSenders(Channel<int> *ch, bool isBuffered) {
} }
// Join all threads // Join all threads
for (size_t i = 0; i < num_threads; i++) t[i].join(); for (size_t i = 0; i < kNumThreads; i++) t[i].join();
} }
// This tests that destroying a channel also unblocks // This tests that destroying a channel also unblocks
// any receivers waiting on the channel // any receivers waiting on the channel
void ChannelDestroyUnblockReceivers(Channel<int> *ch) { void ChannelDestroyUnblockReceivers(Channel<int> *ch) {
size_t num_threads = 5; const size_t kNumThreads = 5;
std::thread t[num_threads]; std::thread t[kNumThreads];
bool thread_ended[num_threads]; bool thread_ended[kNumThreads];
// Launches threads that try to read and are blocked because of no writers // Launches threads that try to read and are blocked because of no writers
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
thread_ended[i] = false; thread_ended[i] = false;
t[i] = std::thread( t[i] = std::thread(
[&](bool *p) { [&](bool *p) {
...@@ -498,18 +498,18 @@ void ChannelDestroyUnblockReceivers(Channel<int> *ch) { ...@@ -498,18 +498,18 @@ void ChannelDestroyUnblockReceivers(Channel<int> *ch) {
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // wait std::this_thread::sleep_for(std::chrono::milliseconds(100)); // wait
// Verify that all threads are blocked // Verify that all threads are blocked
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
EXPECT_EQ(thread_ended[i], false); EXPECT_EQ(thread_ended[i], false);
} }
// delete the channel // delete the channel
delete ch; delete ch;
std::this_thread::sleep_for(std::chrono::milliseconds(200)); // wait std::this_thread::sleep_for(std::chrono::milliseconds(200)); // wait
// Verify that all threads got unblocked // Verify that all threads got unblocked
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
EXPECT_EQ(thread_ended[i], true); EXPECT_EQ(thread_ended[i], true);
} }
for (size_t i = 0; i < num_threads; i++) t[i].join(); for (size_t i = 0; i < kNumThreads; i++) t[i].join();
} }
TEST(Channel, BufferedChannelDestroyUnblocksReceiversTest) { TEST(Channel, BufferedChannelDestroyUnblocksReceiversTest) {
...@@ -679,12 +679,12 @@ TEST(ChannelHolder, TypeMismatchReceiveTest) { ...@@ -679,12 +679,12 @@ TEST(ChannelHolder, TypeMismatchReceiveTest) {
} }
void ChannelHolderCloseUnblocksReceiversTest(ChannelHolder *ch) { void ChannelHolderCloseUnblocksReceiversTest(ChannelHolder *ch) {
size_t num_threads = 5; const size_t kNumThreads = 5;
std::thread t[num_threads]; std::thread t[kNumThreads];
bool thread_ended[num_threads]; bool thread_ended[kNumThreads];
// Launches threads that try to read and are blocked because of no writers // Launches threads that try to read and are blocked because of no writers
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
thread_ended[i] = false; thread_ended[i] = false;
t[i] = std::thread( t[i] = std::thread(
[&](bool *p) { [&](bool *p) {
...@@ -697,7 +697,7 @@ void ChannelHolderCloseUnblocksReceiversTest(ChannelHolder *ch) { ...@@ -697,7 +697,7 @@ void ChannelHolderCloseUnblocksReceiversTest(ChannelHolder *ch) {
std::this_thread::sleep_for(std::chrono::milliseconds(200)); // wait 0.2 sec std::this_thread::sleep_for(std::chrono::milliseconds(200)); // wait 0.2 sec
// Verify that all the threads are blocked // Verify that all the threads are blocked
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
EXPECT_EQ(thread_ended[i], false); EXPECT_EQ(thread_ended[i], false);
} }
...@@ -708,21 +708,21 @@ void ChannelHolderCloseUnblocksReceiversTest(ChannelHolder *ch) { ...@@ -708,21 +708,21 @@ void ChannelHolderCloseUnblocksReceiversTest(ChannelHolder *ch) {
std::this_thread::sleep_for(std::chrono::milliseconds(200)); // wait 0.2 sec std::this_thread::sleep_for(std::chrono::milliseconds(200)); // wait 0.2 sec
// Verify that all threads got unblocked // Verify that all threads got unblocked
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
EXPECT_EQ(thread_ended[i], true); EXPECT_EQ(thread_ended[i], true);
} }
for (size_t i = 0; i < num_threads; i++) t[i].join(); for (size_t i = 0; i < kNumThreads; i++) t[i].join();
} }
void ChannelHolderCloseUnblocksSendersTest(ChannelHolder *ch, bool isBuffered) { void ChannelHolderCloseUnblocksSendersTest(ChannelHolder *ch, bool isBuffered) {
size_t num_threads = 5; const size_t kNumThreads = 5;
std::thread t[num_threads]; std::thread t[kNumThreads];
bool thread_ended[num_threads]; bool thread_ended[kNumThreads];
bool send_success[num_threads]; bool send_success[kNumThreads];
// Launches threads that try to write and are blocked because of no readers // Launches threads that try to write and are blocked because of no readers
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
thread_ended[i] = false; thread_ended[i] = false;
send_success[i] = false; send_success[i] = false;
t[i] = std::thread( t[i] = std::thread(
...@@ -744,13 +744,13 @@ void ChannelHolderCloseUnblocksSendersTest(ChannelHolder *ch, bool isBuffered) { ...@@ -744,13 +744,13 @@ void ChannelHolderCloseUnblocksSendersTest(ChannelHolder *ch, bool isBuffered) {
if (isBuffered) { if (isBuffered) {
// If ch is Buffered, atleast 4 threads must be blocked. // If ch is Buffered, atleast 4 threads must be blocked.
int ct = 0; int ct = 0;
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
if (!thread_ended[i]) ct++; if (!thread_ended[i]) ct++;
} }
EXPECT_GE(ct, 4); EXPECT_GE(ct, 4);
} else { } else {
// If ch is UnBuffered, all the threads should be blocked. // If ch is UnBuffered, all the threads should be blocked.
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
EXPECT_EQ(thread_ended[i], false); EXPECT_EQ(thread_ended[i], false);
} }
} }
...@@ -761,21 +761,21 @@ void ChannelHolderCloseUnblocksSendersTest(ChannelHolder *ch, bool isBuffered) { ...@@ -761,21 +761,21 @@ void ChannelHolderCloseUnblocksSendersTest(ChannelHolder *ch, bool isBuffered) {
std::this_thread::sleep_for(std::chrono::milliseconds(200)); // wait std::this_thread::sleep_for(std::chrono::milliseconds(200)); // wait
// Verify that all threads got unblocked // Verify that all threads got unblocked
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
EXPECT_EQ(thread_ended[i], true); EXPECT_EQ(thread_ended[i], true);
} }
if (isBuffered) { if (isBuffered) {
// Verify that only 1 send was successful // Verify that only 1 send was successful
int ct = 0; int ct = 0;
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
if (send_success[i]) ct++; if (send_success[i]) ct++;
} }
// Only 1 send must be successful // Only 1 send must be successful
EXPECT_EQ(ct, 1); EXPECT_EQ(ct, 1);
} }
for (size_t i = 0; i < num_threads; i++) t[i].join(); for (size_t i = 0; i < kNumThreads; i++) t[i].join();
} }
// This tests that closing a channelholder unblocks // This tests that closing a channelholder unblocks
...@@ -813,13 +813,13 @@ TEST(Channel, ChannelHolderCloseUnblocksSendersTest) { ...@@ -813,13 +813,13 @@ TEST(Channel, ChannelHolderCloseUnblocksSendersTest) {
// This tests that destroying a channelholder unblocks // This tests that destroying a channelholder unblocks
// any senders waiting for channel // any senders waiting for channel
void ChannelHolderDestroyUnblockSenders(ChannelHolder *ch, bool isBuffered) { void ChannelHolderDestroyUnblockSenders(ChannelHolder *ch, bool isBuffered) {
size_t num_threads = 5; const size_t kNumThreads = 5;
std::thread t[num_threads]; std::thread t[kNumThreads];
bool thread_ended[num_threads]; bool thread_ended[kNumThreads];
bool send_success[num_threads]; bool send_success[kNumThreads];
// Launches threads that try to write and are blocked because of no readers // Launches threads that try to write and are blocked because of no readers
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
thread_ended[i] = false; thread_ended[i] = false;
send_success[i] = false; send_success[i] = false;
t[i] = std::thread( t[i] = std::thread(
...@@ -841,14 +841,14 @@ void ChannelHolderDestroyUnblockSenders(ChannelHolder *ch, bool isBuffered) { ...@@ -841,14 +841,14 @@ void ChannelHolderDestroyUnblockSenders(ChannelHolder *ch, bool isBuffered) {
if (isBuffered) { if (isBuffered) {
// If channel is buffered, verify that atleast 4 threads are blocked // If channel is buffered, verify that atleast 4 threads are blocked
int ct = 0; int ct = 0;
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
if (thread_ended[i] == false) ct++; if (thread_ended[i] == false) ct++;
} }
// Atleast 4 threads must be blocked // Atleast 4 threads must be blocked
EXPECT_GE(ct, 4); EXPECT_GE(ct, 4);
} else { } else {
// Verify that all the threads are blocked // Verify that all the threads are blocked
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
EXPECT_EQ(thread_ended[i], false); EXPECT_EQ(thread_ended[i], false);
} }
} }
...@@ -857,13 +857,13 @@ void ChannelHolderDestroyUnblockSenders(ChannelHolder *ch, bool isBuffered) { ...@@ -857,13 +857,13 @@ void ChannelHolderDestroyUnblockSenders(ChannelHolder *ch, bool isBuffered) {
std::this_thread::sleep_for(std::chrono::milliseconds(200)); // wait std::this_thread::sleep_for(std::chrono::milliseconds(200)); // wait
// Verify that all threads got unblocked // Verify that all threads got unblocked
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
EXPECT_EQ(thread_ended[i], true); EXPECT_EQ(thread_ended[i], true);
} }
// Count number of successfuld sends // Count number of successfuld sends
int ct = 0; int ct = 0;
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
if (send_success[i]) ct++; if (send_success[i]) ct++;
} }
...@@ -876,18 +876,18 @@ void ChannelHolderDestroyUnblockSenders(ChannelHolder *ch, bool isBuffered) { ...@@ -876,18 +876,18 @@ void ChannelHolderDestroyUnblockSenders(ChannelHolder *ch, bool isBuffered) {
} }
// Join all threads // Join all threads
for (size_t i = 0; i < num_threads; i++) t[i].join(); for (size_t i = 0; i < kNumThreads; i++) t[i].join();
} }
// This tests that destroying a channelholder also unblocks // This tests that destroying a channelholder also unblocks
// any receivers waiting on the channel // any receivers waiting on the channel
void ChannelHolderDestroyUnblockReceivers(ChannelHolder *ch) { void ChannelHolderDestroyUnblockReceivers(ChannelHolder *ch) {
size_t num_threads = 5; const size_t kNumThreads = 5;
std::thread t[num_threads]; std::thread t[kNumThreads];
bool thread_ended[num_threads]; bool thread_ended[kNumThreads];
// Launches threads that try to read and are blocked because of no writers // Launches threads that try to read and are blocked because of no writers
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
thread_ended[i] = false; thread_ended[i] = false;
t[i] = std::thread( t[i] = std::thread(
[&](bool *p) { [&](bool *p) {
...@@ -901,18 +901,18 @@ void ChannelHolderDestroyUnblockReceivers(ChannelHolder *ch) { ...@@ -901,18 +901,18 @@ void ChannelHolderDestroyUnblockReceivers(ChannelHolder *ch) {
std::this_thread::sleep_for(std::chrono::milliseconds(200)); // wait std::this_thread::sleep_for(std::chrono::milliseconds(200)); // wait
// Verify that all threads are blocked // Verify that all threads are blocked
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
EXPECT_EQ(thread_ended[i], false); EXPECT_EQ(thread_ended[i], false);
} }
// delete the channel // delete the channel
delete ch; delete ch;
std::this_thread::sleep_for(std::chrono::milliseconds(200)); // wait std::this_thread::sleep_for(std::chrono::milliseconds(200)); // wait
// Verify that all threads got unblocked // Verify that all threads got unblocked
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
EXPECT_EQ(thread_ended[i], true); EXPECT_EQ(thread_ended[i], true);
} }
for (size_t i = 0; i < num_threads; i++) t[i].join(); for (size_t i = 0; i < kNumThreads; i++) t[i].join();
} }
TEST(ChannelHolder, ChannelHolderDestroyUnblocksReceiversTest) { TEST(ChannelHolder, ChannelHolderDestroyUnblocksReceiversTest) {
...@@ -945,12 +945,12 @@ TEST(ChannelHolder, ChannelHolderDestroyUnblocksSendersTest) { ...@@ -945,12 +945,12 @@ TEST(ChannelHolder, ChannelHolderDestroyUnblocksSendersTest) {
// This tests that closing a channelholder many times. // This tests that closing a channelholder many times.
void ChannelHolderManyTimesClose(ChannelHolder *ch) { void ChannelHolderManyTimesClose(ChannelHolder *ch) {
const int num_threads = 15; const int kNumThreads = 15;
std::thread t[num_threads]; std::thread t[kNumThreads];
bool thread_ended[num_threads]; bool thread_ended[kNumThreads];
// Launches threads that try to send data to channel. // Launches threads that try to send data to channel.
for (size_t i = 0; i < num_threads / 3; i++) { for (size_t i = 0; i < kNumThreads / 3; i++) {
thread_ended[i] = false; thread_ended[i] = false;
t[i] = std::thread( t[i] = std::thread(
[&](bool *ended) { [&](bool *ended) {
...@@ -962,7 +962,7 @@ void ChannelHolderManyTimesClose(ChannelHolder *ch) { ...@@ -962,7 +962,7 @@ void ChannelHolderManyTimesClose(ChannelHolder *ch) {
} }
// Launches threads that try to receive data to channel. // Launches threads that try to receive data to channel.
for (size_t i = num_threads / 3; i < 2 * num_threads / 3; i++) { for (size_t i = kNumThreads / 3; i < 2 * kNumThreads / 3; i++) {
thread_ended[i] = false; thread_ended[i] = false;
t[i] = std::thread( t[i] = std::thread(
[&](bool *p) { [&](bool *p) {
...@@ -976,7 +976,7 @@ void ChannelHolderManyTimesClose(ChannelHolder *ch) { ...@@ -976,7 +976,7 @@ void ChannelHolderManyTimesClose(ChannelHolder *ch) {
} }
// Launches threads that try to close the channel. // Launches threads that try to close the channel.
for (size_t i = 2 * num_threads / 3; i < num_threads; i++) { for (size_t i = 2 * kNumThreads / 3; i < kNumThreads; i++) {
thread_ended[i] = false; thread_ended[i] = false;
t[i] = std::thread( t[i] = std::thread(
[&](bool *p) { [&](bool *p) {
...@@ -991,13 +991,13 @@ void ChannelHolderManyTimesClose(ChannelHolder *ch) { ...@@ -991,13 +991,13 @@ void ChannelHolderManyTimesClose(ChannelHolder *ch) {
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // wait std::this_thread::sleep_for(std::chrono::milliseconds(100)); // wait
// Verify that all threads are unblocked // Verify that all threads are unblocked
for (size_t i = 0; i < num_threads; i++) { for (size_t i = 0; i < kNumThreads; i++) {
EXPECT_EQ(thread_ended[i], true); EXPECT_EQ(thread_ended[i], true);
} }
EXPECT_TRUE(ch->IsClosed()); EXPECT_TRUE(ch->IsClosed());
// delete the channel // delete the channel
delete ch; delete ch;
for (size_t i = 0; i < num_threads; i++) t[i].join(); for (size_t i = 0; i < kNumThreads; i++) t[i].join();
} }
TEST(ChannelHolder, ChannelHolderManyTimesCloseTest) { TEST(ChannelHolder, ChannelHolderManyTimesCloseTest) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册