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
diff --git a/automotive/remoteaccess/test_grpc_server/impl/test/TestWakeupClientServiceImplUnitTest.cpp b/automotive/remoteaccess/test_grpc_server/impl/test/TestWakeupClientServiceImplUnitTest.cpp
index 4bc0086..dd08e32 100644
--- a/automotive/remoteaccess/test_grpc_server/impl/test/TestWakeupClientServiceImplUnitTest.cpp
+++ b/automotive/remoteaccess/test_grpc_server/impl/test/TestWakeupClientServiceImplUnitTest.cpp
@@ -41,7 +41,6 @@
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 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();
}
- mServer->Wait();
+ 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";
}
- mChannel = grpc::CreateChannel(kTestGrpcAddr, grpc::InsecureChannelCredentials());
+ if (mServer == nullptr) {
+ GTEST_SKIP() << "Failed to start the test grpc server";
+ }
+ 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 @@
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;