From 1b0cef7db2395b500ba56bd88f938e2a97157cb0 Mon Sep 17 00:00:00 2001 From: cyan Date: Tue, 9 Jan 2024 16:00:57 +0000 Subject: [PATCH] Fix unstable test --- unittests/feed/SubscriptionManagerTests.cpp | 28 ++++++++------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/unittests/feed/SubscriptionManagerTests.cpp b/unittests/feed/SubscriptionManagerTests.cpp index 8c580498f..41b67e084 100644 --- a/unittests/feed/SubscriptionManagerTests.cpp +++ b/unittests/feed/SubscriptionManagerTests.cpp @@ -100,13 +100,8 @@ class SubscriptionManagerTest : public MockBackendTest, public SyncAsioContextTe TEST_F(SubscriptionManagerTest, MultipleThreadCtx) { - std::optional work_; - work_.emplace(ctx); // guard the context - std::vector workers; workers.reserve(2); - for (int i = 0; i < 2; ++i) - workers.emplace_back([this]() { ctx.run(); }); SubscriptionManagerPtr->subManifest(session); SubscriptionManagerPtr->subValidation(session); @@ -114,21 +109,20 @@ TEST_F(SubscriptionManagerTest, MultipleThreadCtx) 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(); }