Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 344e71fc authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Poll for shutdown lazy services in libbinder_ndk" into main

parents afa61f54 b9ea7076
Loading
Loading
Loading
Loading
+23 −17
Original line number Diff line number Diff line
@@ -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) {
    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;
}

@@ -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.";
}