Skip to content

Commit b9ea707

Browse files
Poll for shutdown lazy services in libbinder_ndk
Poll every 1s for the services to be shut down. Increas max sleep time to 30s if we still find the service. Lazy services may take a long time to shut down. Test: atest libbinder_ndk_unit_tests Bug: none Change-Id: Ica5f933e69e7f102a78c9272b089d187453e9895
1 parent 9f40868 commit b9ea707

1 file changed

Lines changed: 23 additions & 17 deletions

File tree

libs/binder/ndk/tests/libbinder_ndk_unit_test.cpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "android/binder_ibinder.h"
4747

4848
using namespace android;
49+
using namespace std::chrono_literals;
4950

5051
constexpr char kExistingNonNdkService[] = "SurfaceFlinger";
5152
constexpr char kBinderNdkUnitTestService[] = "BinderNdkUnitTest";
@@ -54,7 +55,7 @@ constexpr char kForcePersistNdkUnitTestService[] = "ForcePersistNdkUnitTestServi
5455
constexpr char kActiveServicesNdkUnitTestService[] = "ActiveServicesNdkUnitTestService";
5556
constexpr char kBinderNdkUnitTestServiceFlagged[] = "BinderNdkUnitTestFlagged";
5657

57-
constexpr unsigned int kShutdownWaitTime = 11;
58+
constexpr auto kShutdownWaitTime = 30s;
5859
constexpr uint64_t kContextTestValue = 0xb4e42fb4d9a1d715;
5960

6061
class MyTestFoo : public IFoo {
@@ -253,12 +254,22 @@ int lazyService(const char* instance) {
253254
}
254255

255256
bool 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

457468
TEST(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

Comments
 (0)