Skip to content

Commit 47520a9

Browse files
committed
Improve IpcMessageTest.
1 parent f16e9e9 commit 47520a9

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

src/common/ipc/tests/IpcMessageTest.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ BOOST_AUTO_TEST_CASE(ProducerConsumerMessageTest)
8080

8181
using TestMessage = std::variant<Small, Big, Stop>;
8282

83+
constexpr unsigned MESSAGE_COUNT = 8'000;
84+
constexpr unsigned THREAD_COUNT = 2;
85+
8386
constexpr auto ENV_NAME = "FB_PRODUCER_CONSUMER_MESSAGE_TEST_NAME";
8487
constexpr auto ENV_RECEIVER = "FB_PRODUCER_CONSUMER_MESSAGE_TEST_RECEIVER";
8588
constexpr auto ENV_PRODUCER_PROCESSES = "FB_PRODUCER_CONSUMER_MESSAGE_TEST_PRODUCER_PROCESSES";
@@ -107,7 +110,7 @@ BOOST_AUTO_TEST_CASE(ProducerConsumerMessageTest)
107110

108111
std::vector<std::unique_ptr<IpcMessageSender<TestMessage>>> senders;
109112

110-
for (unsigned i = 0u; i < (multiProcessIsReceiver ? 0u : 2u); ++i)
113+
for (unsigned i = 0u; i < (multiProcessIsReceiver ? 0u : THREAD_COUNT); ++i)
111114
{
112115
senders.emplace_back(std::make_unique<IpcMessageSender<TestMessage>>(IpcMessageParameters{
113116
.physicalName = testPath,
@@ -117,9 +120,7 @@ BOOST_AUTO_TEST_CASE(ProducerConsumerMessageTest)
117120
}));
118121
}
119122

120-
constexpr unsigned numMessages = 8'000;
121-
constexpr unsigned start[2] = {0, numMessages + 10};
122-
unsigned writeNum[2] = {0, 0};
123+
std::vector<unsigned> writeNum(THREAD_COUNT, 0);
123124
unsigned readCount = 0;
124125
unsigned stopReads = 0;
125126
unsigned smallReads = 0;
@@ -130,9 +131,9 @@ BOOST_AUTO_TEST_CASE(ProducerConsumerMessageTest)
130131
if (!multiProcess || !multiProcessIsReceiver)
131132
{
132133
const auto senderFunc = [&](unsigned i) {
133-
for (writeNum[i] = start[i]; writeNum[i] - start[i] < numMessages; ++writeNum[i])
134+
for (writeNum[i] = MESSAGE_COUNT * i; writeNum[i] - MESSAGE_COUNT * i < MESSAGE_COUNT; ++writeNum[i])
134135
{
135-
if (writeNum[i] % 2 == 0)
136+
if (writeNum[i] % 2u == 0)
136137
{
137138
if (!senders[i]->send(Small{ writeNum[i] }))
138139
++problems;
@@ -148,14 +149,14 @@ BOOST_AUTO_TEST_CASE(ProducerConsumerMessageTest)
148149
++problems;
149150
};
150151

151-
for (unsigned i = 0u; i < 2; ++i)
152+
for (unsigned i = 0u; i < THREAD_COUNT; ++i)
152153
threads.emplace_back(senderFunc, i);
153154
}
154155

155156
if (!multiProcess || multiProcessIsReceiver)
156157
{
157158
threads.emplace_back([&]() {
158-
for (readCount = 0; readCount < (numMessages + 1u) * processCount * 2u;)
159+
for (readCount = 0u; readCount < (MESSAGE_COUNT + 1u) * processCount * THREAD_COUNT;)
159160
{
160161
const auto message = receiver->receive();
161162

@@ -191,20 +192,20 @@ BOOST_AUTO_TEST_CASE(ProducerConsumerMessageTest)
191192
for (auto& thread : threads)
192193
thread.join();
193194

194-
BOOST_CHECK_EQUAL(problems, 0);
195+
BOOST_CHECK_EQUAL(problems, 0u);
195196

196197
if (!multiProcess || !multiProcessIsReceiver)
197198
{
198-
BOOST_CHECK_EQUAL(writeNum[0], start[0] + numMessages);
199-
BOOST_CHECK_EQUAL(writeNum[1], start[1] + numMessages);
199+
for (unsigned i = 0u; i < THREAD_COUNT; ++i)
200+
BOOST_CHECK_EQUAL(writeNum[i], MESSAGE_COUNT * (i + 1u));
200201
}
201202

202203
if (!multiProcess || multiProcessIsReceiver)
203204
{
204-
BOOST_CHECK_EQUAL(readCount, (numMessages + 1u) * processCount * 2u);
205-
BOOST_CHECK_EQUAL(stopReads, processCount * 2u);
206-
BOOST_CHECK_EQUAL(smallReads, processCount * numMessages);
207-
BOOST_CHECK_EQUAL(bigReads, processCount * numMessages);
205+
BOOST_CHECK_EQUAL(readCount, (MESSAGE_COUNT + 1u) * processCount * THREAD_COUNT);
206+
BOOST_CHECK_EQUAL(stopReads, processCount * THREAD_COUNT);
207+
BOOST_CHECK_EQUAL(smallReads, processCount * MESSAGE_COUNT * THREAD_COUNT / 2u);
208+
BOOST_CHECK_EQUAL(bigReads, processCount * MESSAGE_COUNT * THREAD_COUNT / 2u);
208209
}
209210
}
210211

0 commit comments

Comments
 (0)