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

Commit e09845e4 authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Poll for shutdown lazy services in libbinder_ndk" into main am: 344e71fc am: e151a26b

parents c909c99c e151a26b
Loading
Loading
Loading
Loading
+23 −17
Original line number Original line Diff line number Diff line
@@ -46,6 +46,7 @@
#include "android/binder_ibinder.h"
#include "android/binder_ibinder.h"


using namespace android;
using namespace android;
using namespace std::chrono_literals;


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


constexpr unsigned int kShutdownWaitTime = 11;
constexpr auto kShutdownWaitTime = 30s;
constexpr uint64_t kContextTestValue = 0xb4e42fb4d9a1d715;
constexpr uint64_t kContextTestValue = 0xb4e42fb4d9a1d715;


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


bool isServiceRunning(const char* serviceName) {
bool isServiceRunning(const char* serviceName) {
    AIBinder* binder = AServiceManager_checkService(serviceName);
    static const sp<android::IServiceManager> sm(android::defaultServiceManager());
    if (binder == nullptr) {
    const Vector<String16> services = sm->listServices();
    for (const auto service : services) {
        if (service == String16(serviceName)) return true;
    }
    return false;
    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;
    return true;
}
}


@@ -450,8 +461,8 @@ TEST(NdkBinder, CheckLazyServiceShutDown) {
    service = nullptr;
    service = nullptr;
    IPCThreadState::self()->flushCommands();
    IPCThreadState::self()->flushCommands();
    // Make sure the service is dead after some time of no use
    // Make sure the service is dead after some time of no use
    sleep(kShutdownWaitTime);
    ASSERT_TRUE(isServiceShutdownWithWait(kLazyBinderNdkUnitTestService))
    ASSERT_EQ(nullptr, AServiceManager_checkService(kLazyBinderNdkUnitTestService));
            << "Service failed to shut down";
}
}


TEST(NdkBinder, ForcedPersistenceTest) {
TEST(NdkBinder, ForcedPersistenceTest) {
@@ -466,14 +477,12 @@ TEST(NdkBinder, ForcedPersistenceTest) {
        service = nullptr;
        service = nullptr;
        IPCThreadState::self()->flushCommands();
        IPCThreadState::self()->flushCommands();


        sleep(kShutdownWaitTime);

        bool isRunning = isServiceRunning(kForcePersistNdkUnitTestService);

        if (i == 0) {
        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 {
        } 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;
    service = nullptr;
    IPCThreadState::self()->flushCommands();
    IPCThreadState::self()->flushCommands();


    LOG(INFO) << "ActiveServicesCallbackTest about to sleep";
    ASSERT_TRUE(isServiceShutdownWithWait(kActiveServicesNdkUnitTestService))
    sleep(kShutdownWaitTime);

    ASSERT_FALSE(isServiceRunning(kActiveServicesNdkUnitTestService))
            << "Service failed to shut down.";
            << "Service failed to shut down.";
}
}