diff options
-rw-r--r-- | libs/binder/ndk/tests/libbinder_ndk_unit_test.cpp | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/libs/binder/ndk/tests/libbinder_ndk_unit_test.cpp b/libs/binder/ndk/tests/libbinder_ndk_unit_test.cpp index f518a22902..3cd2b9a891 100644 --- a/libs/binder/ndk/tests/libbinder_ndk_unit_test.cpp +++ b/libs/binder/ndk/tests/libbinder_ndk_unit_test.cpp @@ -46,6 +46,7 @@ #include "android/binder_ibinder.h" using namespace android; +using namespace std::chrono_literals; constexpr char kExistingNonNdkService[] = "SurfaceFlinger"; constexpr char kBinderNdkUnitTestService[] = "BinderNdkUnitTest"; @@ -54,7 +55,7 @@ constexpr char kForcePersistNdkUnitTestService[] = "ForcePersistNdkUnitTestServi constexpr char kActiveServicesNdkUnitTestService[] = "ActiveServicesNdkUnitTestService"; constexpr char kBinderNdkUnitTestServiceFlagged[] = "BinderNdkUnitTestFlagged"; -constexpr unsigned int kShutdownWaitTime = 11; +constexpr auto kShutdownWaitTime = 30s; constexpr uint64_t kContextTestValue = 0xb4e42fb4d9a1d715; class MyTestFoo : public IFoo { @@ -253,12 +254,22 @@ int lazyService(const char* instance) { } bool isServiceRunning(const char* serviceName) { - AIBinder* binder = AServiceManager_checkService(serviceName); - if (binder == nullptr) { - return false; + static const sp<android::IServiceManager> sm(android::defaultServiceManager()); + const Vector<String16> services = sm->listServices(); + for (const auto service : services) { + if (service == String16(serviceName)) return true; } - AIBinder_decStrong(binder); + return false; +} +bool isServiceShutdownWithWait(const char* serviceName) { + LOG(INFO) << "About to check and wait for shutdown of " << std::string(serviceName); + const auto before = std::chrono::steady_clock::now(); + while (isServiceRunning(serviceName)) { + sleep(1); + const auto after = std::chrono::steady_clock::now(); + if (after - before >= kShutdownWaitTime) return false; + } return true; } @@ -450,8 +461,8 @@ TEST(NdkBinder, CheckLazyServiceShutDown) { service = nullptr; IPCThreadState::self()->flushCommands(); // Make sure the service is dead after some time of no use - sleep(kShutdownWaitTime); - ASSERT_EQ(nullptr, AServiceManager_checkService(kLazyBinderNdkUnitTestService)); + ASSERT_TRUE(isServiceShutdownWithWait(kLazyBinderNdkUnitTestService)) + << "Service failed to shut down"; } TEST(NdkBinder, ForcedPersistenceTest) { @@ -466,14 +477,12 @@ TEST(NdkBinder, ForcedPersistenceTest) { service = nullptr; IPCThreadState::self()->flushCommands(); - sleep(kShutdownWaitTime); - - bool isRunning = isServiceRunning(kForcePersistNdkUnitTestService); - if (i == 0) { - ASSERT_TRUE(isRunning) << "Service shut down when it shouldn't have."; + ASSERT_TRUE(isServiceRunning(kForcePersistNdkUnitTestService)) + << "Service shut down when it shouldn't have."; } else { - ASSERT_FALSE(isRunning) << "Service failed to shut down."; + ASSERT_TRUE(isServiceShutdownWithWait(kForcePersistNdkUnitTestService)) + << "Service failed to shut down"; } } } @@ -491,10 +500,7 @@ TEST(NdkBinder, ActiveServicesCallbackTest) { service = nullptr; IPCThreadState::self()->flushCommands(); - LOG(INFO) << "ActiveServicesCallbackTest about to sleep"; - sleep(kShutdownWaitTime); - - ASSERT_FALSE(isServiceRunning(kActiveServicesNdkUnitTestService)) + ASSERT_TRUE(isServiceShutdownWithWait(kActiveServicesNdkUnitTestService)) << "Service failed to shut down."; } |