From 9b6404fe80a093a086de1a0fbfb550eaad71b6bf Mon Sep 17 00:00:00 2001 From: Yu Shan Date: Wed, 27 Dec 2023 13:55:23 -0800 Subject: [PATCH] Fix bugs in TestWakeupClientServiceImpl. Initialize atomic variable. By default, it is not initialized. Check task queue stopped status while returning from the wait to prevent infinite loop. Test: atest TestWakeupClientServerHostUnitTest Bug: 317907688 Change-Id: I0259203797caca2fe3ff716c17398d3c1feab94d --- .../impl/include/TestWakeupClientServiceImpl.h | 5 +++-- .../impl/src/TestWakeupClientServiceImpl.cpp | 8 +++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/automotive/remoteaccess/test_grpc_server/impl/include/TestWakeupClientServiceImpl.h b/automotive/remoteaccess/test_grpc_server/impl/include/TestWakeupClientServiceImpl.h index a7f47c2c57..41cc5d0035 100644 --- a/automotive/remoteaccess/test_grpc_server/impl/include/TestWakeupClientServiceImpl.h +++ b/automotive/remoteaccess/test_grpc_server/impl/include/TestWakeupClientServiceImpl.h @@ -78,6 +78,7 @@ class TaskQueue final { void waitForTask(); void stopWait(); bool isEmpty(); + bool isStopped(); private: friend class TaskTimeoutMessageHandler; @@ -87,7 +88,7 @@ class TaskQueue final { GUARDED_BY(mLock); // A variable to notify mTasks is not empty. std::condition_variable mTasksNotEmptyCv; - std::atomic mStopped; + std::atomic mStopped = false; android::sp mLooper; android::sp mTaskTimeoutMessageHandler; std::atomic mTaskIdCounter = 0; @@ -214,7 +215,7 @@ class TestWakeupClientServiceImpl : public WakeupClient::Service { std::atomic mRemoteTaskConnectionAlive = false; std::mutex mLock; bool mGeneratingFakeTask GUARDED_BY(mLock); - std::atomic mServerStopped; + std::atomic mServerStopped = false; std::unordered_map> mInfoByScheduleIdByClientId GUARDED_BY(mLock); diff --git a/automotive/remoteaccess/test_grpc_server/impl/src/TestWakeupClientServiceImpl.cpp b/automotive/remoteaccess/test_grpc_server/impl/src/TestWakeupClientServiceImpl.cpp index d22335377a..eed3495575 100644 --- a/automotive/remoteaccess/test_grpc_server/impl/src/TestWakeupClientServiceImpl.cpp +++ b/automotive/remoteaccess/test_grpc_server/impl/src/TestWakeupClientServiceImpl.cpp @@ -105,6 +105,10 @@ void TaskQueue::waitForTask() { }); } +bool TaskQueue::isStopped() { + return mStopped; +} + void TaskQueue::stopWait() { mStopped = true; { @@ -241,7 +245,7 @@ Status TestWakeupClientServiceImpl::GetRemoteTasks(ServerContext* context, while (true) { mTaskQueue->waitForTask(); - if (mServerStopped) { + if (mTaskQueue->isStopped()) { // Server stopped, exit the loop. printf("Server stopped exit loop\n"); break; @@ -250,11 +254,13 @@ Status TestWakeupClientServiceImpl::GetRemoteTasks(ServerContext* context, while (true) { auto maybeTask = mTaskQueue->maybePopOne(); if (!maybeTask.has_value()) { + printf("no task left\n"); // No task left, loop again and wait for another task(s). break; } // Loop through all the task in the queue but obtain lock for each element so we don't // hold lock while writing the response. + printf("Sending one remote task\n"); const GetRemoteTasksResponse& response = maybeTask.value(); if (!writer->Write(response)) { // Broken stream, maybe the client is shutting down.