Skip to content

Commit

Permalink
Fix unstable test (#1089)
Browse files Browse the repository at this point in the history
  • Loading branch information
cindyyan317 authored Jan 10, 2024
1 parent d30e63d commit 2514b79
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions unittests/feed/SubscriptionManagerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,35 +100,29 @@ class SubscriptionManagerTest : public MockBackendTest, public SyncAsioContextTe

TEST_F(SubscriptionManagerTest, MultipleThreadCtx)
{
std::optional<boost::asio::io_context::work> work_;
work_.emplace(ctx); // guard the context

std::vector<std::thread> workers;
workers.reserve(2);
for (int i = 0; i < 2; ++i)
workers.emplace_back([this]() { ctx.run(); });

SubscriptionManagerPtr->subManifest(session);
SubscriptionManagerPtr->subValidation(session);

SubscriptionManagerPtr->forwardManifest(json::parse(R"({"manifest":"test"})").get_object());
SubscriptionManagerPtr->forwardValidation(json::parse(R"({"validation":"test"})").get_object());

auto retry = 5;
while (--retry != 0) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));

if (receivedFeedMessage() == R"({"manifest":"test"}{"validation":"test"})" ||
receivedFeedMessage() == R"({"validation":"test"}{"manifest":"test"})")
break;
}
EXPECT_TRUE(retry != 0) << "receivedFeedMessage() = " << receivedFeedMessage();

session.reset();
work_.reset();
for (int i = 0; i < 2; ++i)
workers.emplace_back([this]() { ctx.run(); });

// wait for all jobs in ctx to finish
for (auto& worker : workers)
worker.join();

EXPECT_TRUE(
receivedFeedMessage() == R"({"manifest":"test"}{"validation":"test"})" ||
receivedFeedMessage() == R"({"validation":"test"}{"manifest":"test"})"
) << "receivedFeedMessage() = "
<< receivedFeedMessage();

session.reset();
SubscriptionManagerPtr.reset();
}

Expand Down

0 comments on commit 2514b79

Please sign in to comment.