4646#include " android/binder_ibinder.h"
4747
4848using namespace android ;
49+ using namespace std ::chrono_literals;
4950
5051constexpr char kExistingNonNdkService [] = " SurfaceFlinger" ;
5152constexpr char kBinderNdkUnitTestService [] = " BinderNdkUnitTest" ;
@@ -54,7 +55,7 @@ constexpr char kForcePersistNdkUnitTestService[] = "ForcePersistNdkUnitTestServi
5455constexpr char kActiveServicesNdkUnitTestService [] = " ActiveServicesNdkUnitTestService" ;
5556constexpr char kBinderNdkUnitTestServiceFlagged [] = " BinderNdkUnitTestFlagged" ;
5657
57- constexpr unsigned int kShutdownWaitTime = 11 ;
58+ constexpr auto kShutdownWaitTime = 30s ;
5859constexpr uint64_t kContextTestValue = 0xb4e42fb4d9a1d715 ;
5960
6061class MyTestFoo : public IFoo {
@@ -253,12 +254,22 @@ int lazyService(const char* instance) {
253254}
254255
255256bool isServiceRunning (const char * serviceName) {
256- AIBinder* binder = AServiceManager_checkService (serviceName);
257- if (binder == nullptr ) {
258- return false ;
257+ static const sp<android::IServiceManager> sm (android::defaultServiceManager ());
258+ const Vector<String16> services = sm->listServices ();
259+ for (const auto service : services) {
260+ if (service == String16 (serviceName)) return true ;
259261 }
260- AIBinder_decStrong (binder);
262+ return false ;
263+ }
261264
265+ bool isServiceShutdownWithWait (const char * serviceName) {
266+ LOG (INFO) << " About to check and wait for shutdown of " << std::string (serviceName);
267+ const auto before = std::chrono::steady_clock::now ();
268+ while (isServiceRunning (serviceName)) {
269+ sleep (1 );
270+ const auto after = std::chrono::steady_clock::now ();
271+ if (after - before >= kShutdownWaitTime ) return false ;
272+ }
262273 return true ;
263274}
264275
@@ -450,8 +461,8 @@ TEST(NdkBinder, CheckLazyServiceShutDown) {
450461 service = nullptr ;
451462 IPCThreadState::self ()->flushCommands ();
452463 // Make sure the service is dead after some time of no use
453- sleep ( kShutdownWaitTime );
454- ASSERT_EQ ( nullptr , AServiceManager_checkService ( kLazyBinderNdkUnitTestService )) ;
464+ ASSERT_TRUE ( isServiceShutdownWithWait ( kLazyBinderNdkUnitTestService ))
465+ << " Service failed to shut down " ;
455466}
456467
457468TEST (NdkBinder, ForcedPersistenceTest) {
@@ -466,14 +477,12 @@ TEST(NdkBinder, ForcedPersistenceTest) {
466477 service = nullptr ;
467478 IPCThreadState::self ()->flushCommands ();
468479
469- sleep (kShutdownWaitTime );
470-
471- bool isRunning = isServiceRunning (kForcePersistNdkUnitTestService );
472-
473480 if (i == 0 ) {
474- ASSERT_TRUE (isRunning) << " Service shut down when it shouldn't have." ;
481+ ASSERT_TRUE (isServiceRunning (kForcePersistNdkUnitTestService ))
482+ << " Service shut down when it shouldn't have." ;
475483 } else {
476- ASSERT_FALSE (isRunning) << " Service failed to shut down." ;
484+ ASSERT_TRUE (isServiceShutdownWithWait (kForcePersistNdkUnitTestService ))
485+ << " Service failed to shut down" ;
477486 }
478487 }
479488}
@@ -491,10 +500,7 @@ TEST(NdkBinder, ActiveServicesCallbackTest) {
491500 service = nullptr ;
492501 IPCThreadState::self ()->flushCommands ();
493502
494- LOG (INFO) << " ActiveServicesCallbackTest about to sleep" ;
495- sleep (kShutdownWaitTime );
496-
497- ASSERT_FALSE (isServiceRunning (kActiveServicesNdkUnitTestService ))
503+ ASSERT_TRUE (isServiceShutdownWithWait (kActiveServicesNdkUnitTestService ))
498504 << " Service failed to shut down." ;
499505}
500506
0 commit comments