missive: Fix synchronization issue in factory tests
BUG=b:209839434
BUG=b:209896423
TEST=FEATURES="test" emerge-$BOARD missive
Change-Id: Idf9d9b96d099b57527f5c9471f104b2db7bc06b5
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/3331668
Tested-by: Leonid Baraz <lbaraz@chromium.org>
Auto-Submit: Leonid Baraz <lbaraz@chromium.org>
Reviewed-by: Yu-Ping Wu <yupingso@chromium.org>
Commit-Queue: Leonid Baraz <lbaraz@chromium.org>
diff --git a/missive/client/report_queue_factory_test.cc b/missive/client/report_queue_factory_test.cc
index 422b492..35eb802 100644
--- a/missive/client/report_queue_factory_test.cc
+++ b/missive/client/report_queue_factory_test.cc
@@ -19,6 +19,7 @@
#include "missive/client/mock_report_queue_provider.h"
#include "missive/client/report_queue.h"
#include "missive/client/report_queue_provider_test_helper.h"
+#include "missive/util/test_support_callbacks.h"
using ::testing::_;
using ::testing::HasSubstr;
@@ -32,13 +33,17 @@
class MockReportQueueConsumer {
public:
MockReportQueueConsumer() = default;
- void SetReportQueue(std::unique_ptr<ReportQueue> report_queue) {
+ void SetReportQueue(test::TestCallbackWaiter* waiter,
+ std::unique_ptr<ReportQueue> report_queue) {
report_queue_ = std::move(report_queue);
+ if (waiter) {
+ waiter->Signal();
+ }
}
base::OnceCallback<void(std::unique_ptr<reporting::ReportQueue>)>
- GetReportQueueSetter() {
+ GetReportQueueSetter(test::TestCallbackWaiter* waiter) {
return base::BindOnce(&MockReportQueueConsumer::SetReportQueue,
- weak_factory_.GetWeakPtr());
+ weak_factory_.GetWeakPtr(), base::Unretained(waiter));
}
ReportQueue* GetReportQueue() const { return report_queue_.get(); }
@@ -58,6 +63,7 @@
}
void TearDown() override {
+ task_environment_.RunUntilIdle(); // Drain remaining scheduled tasks.
report_queue_provider_test_helper::SetForTesting(nullptr);
}
@@ -73,12 +79,14 @@
TEST_F(ReportQueueFactoryTest, CreateAndGetQueueUsingDMToken) {
// Initially the queue must be an uninitialized unique_ptr
EXPECT_FALSE(consumer_->GetReportQueue());
- reporting::ReportQueueFactory::Create(/*dm_token_value=*/"TOKEN",
- destination_,
- consumer_->GetReportQueueSetter());
- EXPECT_CALL(*provider_.get(), OnInitCompleted()).Times(1);
- provider_->ExpectCreateNewQueueAndReturnNewMockQueue(1);
- task_environment_.RunUntilIdle();
+ {
+ test::TestCallbackAutoWaiter set_waiter;
+ reporting::ReportQueueFactory::Create(
+ /*dm_token_value=*/"TOKEN", destination_,
+ consumer_->GetReportQueueSetter(&set_waiter));
+ EXPECT_CALL(*provider_.get(), OnInitCompleted()).Times(1);
+ provider_->ExpectCreateNewQueueAndReturnNewMockQueue(1);
+ }
// We expect the report queue to be existing in the consumer.
EXPECT_TRUE(consumer_->GetReportQueue());
}
@@ -86,11 +94,14 @@
TEST_F(ReportQueueFactoryTest, CreateAndGetQueue) {
// Initially the queue must be an uninitialized unique_ptr
EXPECT_FALSE(consumer_->GetReportQueue());
- reporting::ReportQueueFactory::Create(EventType::kDevice, destination_,
- consumer_->GetReportQueueSetter());
- EXPECT_CALL(*provider_.get(), OnInitCompleted()).Times(1);
- provider_->ExpectCreateNewQueueAndReturnNewMockQueue(1);
- task_environment_.RunUntilIdle();
+ {
+ test::TestCallbackAutoWaiter set_waiter;
+ reporting::ReportQueueFactory::Create(
+ EventType::kDevice, destination_,
+ consumer_->GetReportQueueSetter(&set_waiter));
+ EXPECT_CALL(*provider_.get(), OnInitCompleted()).Times(1);
+ provider_->ExpectCreateNewQueueAndReturnNewMockQueue(1);
+ }
// We expect the report queue to be existing in the consumer.
EXPECT_TRUE(consumer_->GetReportQueue());
}
@@ -98,12 +109,11 @@
TEST_F(ReportQueueFactoryTest, CreateQueueWithInvalidConfig) {
// Initially the queue must be an uninitialized unique_ptr
EXPECT_FALSE(consumer_->GetReportQueue());
- reporting::ReportQueueFactory::Create(EventType::kDevice,
- Destination::UNDEFINED_DESTINATION,
- consumer_->GetReportQueueSetter());
+ reporting::ReportQueueFactory::Create(
+ EventType::kDevice, Destination::UNDEFINED_DESTINATION,
+ consumer_->GetReportQueueSetter(nullptr));
// Expect failure before it gets to the report queue provider
EXPECT_CALL(*provider_.get(), OnInitCompleted()).Times(0);
- task_environment_.RunUntilIdle();
// We do not expect the report queue to be existing in the consumer.
EXPECT_FALSE(consumer_->GetReportQueue());
}
@@ -114,7 +124,6 @@
const auto report_queue =
reporting::ReportQueueFactory::CreateSpeculativeReportQueue(
EventType::kDevice, destination_);
- task_environment_.RunUntilIdle();
EXPECT_THAT(report_queue, NotNull());
}
@@ -122,18 +131,19 @@
const auto report_queue =
reporting::ReportQueueFactory::CreateSpeculativeReportQueue(
EventType::kDevice, Destination::UNDEFINED_DESTINATION);
- task_environment_.RunUntilIdle();
EXPECT_THAT(report_queue, IsNull());
}
TEST_F(ReportQueueFactoryTest, EmptyDmToken) {
// Initially the queue must be an uninitialized unique_ptr
EXPECT_FALSE(consumer_->GetReportQueue());
- reporting::ReportQueueFactory::Create("", destination_,
- consumer_->GetReportQueueSetter());
- EXPECT_CALL(*provider_.get(), OnInitCompleted()).Times(1);
- provider_->ExpectCreateNewQueueAndReturnNewMockQueue(1);
- task_environment_.RunUntilIdle();
+ {
+ test::TestCallbackAutoWaiter set_waiter;
+ reporting::ReportQueueFactory::Create(
+ "", destination_, consumer_->GetReportQueueSetter(&set_waiter));
+ EXPECT_CALL(*provider_.get(), OnInitCompleted()).Times(1);
+ provider_->ExpectCreateNewQueueAndReturnNewMockQueue(1);
+ }
// We expect the report queue to be existing in the consumer.
EXPECT_TRUE(consumer_->GetReportQueue());
}
@@ -143,13 +153,18 @@
auto consumer2 = std::make_unique<MockReportQueueConsumer>();
EXPECT_FALSE(consumer_->GetReportQueue());
EXPECT_FALSE(consumer2->GetReportQueue());
- reporting::ReportQueueFactory::Create(EventType::kDevice, destination_,
- consumer_->GetReportQueueSetter());
- reporting::ReportQueueFactory::Create(EventType::kUser, destination_,
- consumer2->GetReportQueueSetter());
- EXPECT_CALL(*provider_.get(), OnInitCompleted()).Times(1);
- provider_->ExpectCreateNewQueueAndReturnNewMockQueue(2);
- task_environment_.RunUntilIdle();
+ {
+ test::TestCallbackAutoWaiter set_waiter;
+ set_waiter.Attach();
+ reporting::ReportQueueFactory::Create(
+ EventType::kDevice, destination_,
+ consumer_->GetReportQueueSetter(&set_waiter));
+ reporting::ReportQueueFactory::Create(
+ EventType::kUser, destination_,
+ consumer2->GetReportQueueSetter(&set_waiter));
+ EXPECT_CALL(*provider_.get(), OnInitCompleted()).Times(1);
+ provider_->ExpectCreateNewQueueAndReturnNewMockQueue(2);
+ }
// We expect the report queue to be existing in the consumer.
EXPECT_TRUE(consumer_->GetReportQueue());
// And for the 2nd consumer