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

Commit dfb1d75a authored by Yu Shan's avatar Yu Shan
Browse files

Fix flaky TestWakeupClientServiceImplUnitTest.

Previously the test will block forever if buildAndStart returns
a nullptr if the test grpc server fails to start. This CL fixes
this issue by adding a timeout and stop the wait even if the return
value is nullptr. This CL also lets grpc randomly pick an available
port instead of hard coding the port to make test more stable.

Test: atest --host TestWakeupClientServiceImplUnitTest
Bug: 330115017
Change-Id: I1113f95f92a25254e850b7a2eec249d1101c0dfe
parent d3753ec9
Loading
Loading
Loading
Loading
+18 −7
Original line number Diff line number Diff line
@@ -41,7 +41,6 @@ const std::vector<uint8_t> kTestData = {0xde, 0xad, 0xbe, 0xef};
constexpr int32_t kTestCount = 1234;
constexpr int64_t kTestStartTimeInEpochSeconds = 2345;
constexpr int64_t kTestPeriodicInSeconds = 123;
const std::string kTestGrpcAddr = "localhost:50051";

class MyTestWakeupClientServiceImpl final : public ServiceImpl {
  public:
@@ -53,27 +52,38 @@ class MyTestWakeupClientServiceImpl final : public ServiceImpl {
class TestWakeupClientServiceImplUnitTest : public ::testing::Test {
  public:
    virtual void SetUp() override {
        mServerThread = std::thread([this] {
        int selectedPort = 0;
        mServerThread = std::thread([this, &selectedPort] {
            mService = std::make_unique<MyTestWakeupClientServiceImpl>();
            ServerBuilder builder;
            builder.AddListeningPort(kTestGrpcAddr, grpc::InsecureServerCredentials());
            builder.AddListeningPort("localhost:0", grpc::InsecureServerCredentials(),
                                     &selectedPort);
            WakeupClientServiceImpl wakeupClientService(mService.get());
            builder.RegisterService(&wakeupClientService);
            mServer = builder.BuildAndStart();
            mServerStarted = true;
            {
                std::unique_lock<std::mutex> lock(mLock);
                mServerStartCv.notify_one();
            }
            if (mServer != nullptr) {
                mServer->Wait();
            }
        });
        {
            std::unique_lock<std::mutex> lock(mLock);
            mServerStartCv.wait(lock, [this] {
            bool serverStarted = mServerStartCv.wait_for(lock, std::chrono::seconds(10), [this] {
                ScopedLockAssertion lockAssertion(mLock);
                return mServer != nullptr;
                return mServerStarted.load();
            });
            ASSERT_TRUE(serverStarted) << "Failed to wait for mServerStarted to be set within 10s";
        }
        if (mServer == nullptr) {
            GTEST_SKIP() << "Failed to start the test grpc server";
        }
        mChannel = grpc::CreateChannel(kTestGrpcAddr, grpc::InsecureChannelCredentials());
        std::string address = "localhost:" + std::to_string(selectedPort);
        std::cout << "Test grpc server started at: " << address << std::endl;
        mChannel = grpc::CreateChannel(address, grpc::InsecureChannelCredentials());
        mStub = WakeupClient::NewStub(mChannel);
    }

@@ -147,6 +157,7 @@ class TestWakeupClientServiceImplUnitTest : public ::testing::Test {
    std::thread mServerThread;
    std::unique_ptr<MyTestWakeupClientServiceImpl> mService;
    std::unique_ptr<Server> mServer;
    std::atomic<bool> mServerStarted = false;
    std::shared_ptr<Channel> mChannel;
    std::unique_ptr<WakeupClient::Stub> mStub;
    std::vector<GetRemoteTasksResponse> mRemoteTaskResponses;