Loading libs/binder/ndk/tests/libbinder_ndk_unit_test.cpp +23 −17 Original line number Diff line number Diff line Loading @@ -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"; Loading @@ -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 { Loading Loading @@ -253,12 +254,22 @@ int lazyService(const char* instance) { } bool isServiceRunning(const char* serviceName) { AIBinder* binder = AServiceManager_checkService(serviceName); if (binder == nullptr) { 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; } return false; } AIBinder_decStrong(binder); 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; } Loading Loading @@ -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) { Loading @@ -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"; } } } Loading @@ -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."; } Loading Loading
libs/binder/ndk/tests/libbinder_ndk_unit_test.cpp +23 −17 Original line number Diff line number Diff line Loading @@ -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"; Loading @@ -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 { Loading Loading @@ -253,12 +254,22 @@ int lazyService(const char* instance) { } bool isServiceRunning(const char* serviceName) { AIBinder* binder = AServiceManager_checkService(serviceName); if (binder == nullptr) { 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; } return false; } AIBinder_decStrong(binder); 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; } Loading Loading @@ -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) { Loading @@ -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"; } } } Loading @@ -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."; } Loading