From f18ab078739ed8fb95daff032c6bf4275742d1a7 Mon Sep 17 00:00:00 2001 From: Janusz Sobczak Date: Thu, 3 Aug 2023 16:21:09 -0700 Subject: [PATCH] Fix FastPairScannerImplTest The scanner must not be destroyed while there are still scanner's tasks running on the background executor. PiperOrigin-RevId: 553621813 --- .../fastpair/fast_pair_scanner_impl_test.cc | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/fastpair/scanning/fastpair/fast_pair_scanner_impl_test.cc b/fastpair/scanning/fastpair/fast_pair_scanner_impl_test.cc index 80c9c254..184bd3aa 100644 --- a/fastpair/scanning/fastpair/fast_pair_scanner_impl_test.cc +++ b/fastpair/scanning/fastpair/fast_pair_scanner_impl_test.cc @@ -16,6 +16,7 @@ #include #include +#include #include "gtest/gtest.h" #include "absl/strings/escaping.h" @@ -26,6 +27,7 @@ #include "internal/platform/byte_array.h" #include "internal/platform/count_down_latch.h" #include "internal/platform/medium_environment.h" +#include "internal/platform/single_thread_executor.h" namespace nearby { namespace fastpair { @@ -61,18 +63,16 @@ class FastPairScannerObserver : public FastPairScanner::Observer { }; class FastPairScannerImplTest : public testing::Test { - protected: - MediumEnvironment& env_{MediumEnvironment::Instance()}; - - SingleThreadExecutor executor_; + public: + void SetUp() override { MediumEnvironment::Instance().Start(); } + void TearDown() override { MediumEnvironment::Instance().Stop(); } }; TEST_F(FastPairScannerImplTest, StartScanning) { - env_.Start(); - // Create Fast Pair Scanner and add its observer Mediums mediums_1; - auto scanner = std::make_unique(mediums_1, &executor_); + SingleThreadExecutor executor; + auto scanner = std::make_unique(mediums_1, &executor); CountDownLatch accept_latch(1); CountDownLatch lost_latch(1); FastPairScannerObserver observer(scanner.get(), &accept_latch, &lost_latch); @@ -95,14 +95,14 @@ TEST_F(FastPairScannerImplTest, StartScanning) { // Notify device lost EXPECT_TRUE(lost_latch.Await(kTaskWaitTimeout).result()); scan_session.reset(); - env_.Stop(); + DestroyOnExecutor(std::move(scanner), &executor); } TEST_F(FastPairScannerImplTest, StopScanning) { - env_.Start(); // Create Fast Pair Scanner and add its observer Mediums mediums_1; - auto scanner = std::make_unique(mediums_1, &executor_); + SingleThreadExecutor executor; + auto scanner = std::make_unique(mediums_1, &executor); CountDownLatch accept_latch(1); CountDownLatch lost_latch(1); FastPairScannerObserver observer(scanner.get(), &accept_latch, &lost_latch); @@ -120,7 +120,7 @@ TEST_F(FastPairScannerImplTest, StopScanning) { mediums_2.GetBle().GetMedium().StopAdvertising(service_id); // Device lost event should not be delivered when scan session has terminated. EXPECT_FALSE(lost_latch.Await(kShortTimeout).result()); - env_.Stop(); + DestroyOnExecutor(std::move(scanner), &executor); } } // namespace