From a0060489fdf2b09a5578a337e6711722cab41f94 Mon Sep 17 00:00:00 2001 From: Yu Shan Date: Tue, 21 Feb 2023 12:48:10 -0800 Subject: [PATCH] Increase timer test tolerance. This test was flaky on cf. Increase the tolerance to make it stable. This CL also modifies subscribe test to not rely on sleep to make it stable. Test: Presubmit Bug: 268603744 270074790 Change-Id: I4a8003be152ef0a1f409f3705892fffee58170dd --- .../vhal_v2_0/tests/DefaultVhalImpl_test.cpp | 30 ++++++++++++------- .../2.0/default/tests/RecurrentTimer_test.cpp | 2 +- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/automotive/vehicle/2.0/default/impl/vhal_v2_0/tests/DefaultVhalImpl_test.cpp b/automotive/vehicle/2.0/default/impl/vhal_v2_0/tests/DefaultVhalImpl_test.cpp index df5ada6aa2..b5026a63dd 100644 --- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/tests/DefaultVhalImpl_test.cpp +++ b/automotive/vehicle/2.0/default/impl/vhal_v2_0/tests/DefaultVhalImpl_test.cpp @@ -124,6 +124,17 @@ class DefaultVhalImplTest : public ::testing::Test { android::ConcurrentQueue mEventQueue; android::ConcurrentQueue mHeartBeatQueue; + // Wait until receive enough events in receivedEvents. + void waitForEvents(std::vector* receivedEvents, size_t count) { + while (receivedEvents->size() < count) { + mEventQueue.waitForItems(); + auto newEvents = mEventQueue.flush(); + for (size_t i = 0; i < newEvents.size(); i++) { + receivedEvents->push_back(std::move(newEvents[i])); + } + } + } + private: void onHalEvent(VehiclePropValuePtr v) { if (v->prop != toInt(VehicleProperty::VHAL_HEARTBEAT)) { @@ -314,26 +325,25 @@ TEST_F(DefaultVhalImplTest, testSubscribe) { ASSERT_EQ(StatusCode::OK, status); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); + std::vector receivedEvents; + waitForEvents(&receivedEvents, 5); - // Modify the speed after 0.5 seconds. + // Modify the speed after 5 events arrive. VehiclePropValue value; value.prop = toInt(VehicleProperty::PERF_VEHICLE_SPEED); value.value.floatValues.resize(1); value.value.floatValues[0] = 1.0f; ASSERT_EQ(StatusCode::OK, mHal->set(value)); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - - auto events = mEventQueue.flush(); - ASSERT_LE((size_t)10, events.size()); + waitForEvents(&receivedEvents, 10); // The first event should be the default value. - ASSERT_EQ((size_t)1, events[0]->value.floatValues.size()); - EXPECT_EQ(0.0f, events[0]->value.floatValues[0]); + ASSERT_EQ((size_t)1, receivedEvents[0]->value.floatValues.size()); + EXPECT_EQ(0.0f, receivedEvents[0]->value.floatValues[0]); // The last event should be the value after update. - ASSERT_EQ((size_t)1, events[events.size() - 1]->value.floatValues.size()); - EXPECT_EQ(1.0f, events[events.size() - 1]->value.floatValues[0]); + const auto& lastEvent = receivedEvents[receivedEvents.size() - 1]; + ASSERT_EQ((size_t)1, lastEvent->value.floatValues.size()); + EXPECT_EQ(1.0f, lastEvent->value.floatValues[0]); } TEST_F(DefaultVhalImplTest, testSubscribeInvalidProp) { diff --git a/automotive/vehicle/2.0/default/tests/RecurrentTimer_test.cpp b/automotive/vehicle/2.0/default/tests/RecurrentTimer_test.cpp index d7547f6556..2e59dbfc90 100644 --- a/automotive/vehicle/2.0/default/tests/RecurrentTimer_test.cpp +++ b/automotive/vehicle/2.0/default/tests/RecurrentTimer_test.cpp @@ -69,7 +69,7 @@ TEST(RecurrentTimerTest, multipleIntervals) { std::this_thread::sleep_for(milliseconds(100)); // This test is unstable, so set the tolerance to 50. ASSERT_EQ_WITH_TOLERANCE(100, counter1ms.load(), 50); - ASSERT_EQ_WITH_TOLERANCE(20, counter5ms.load(), 5); + ASSERT_EQ_WITH_TOLERANCE(20, counter5ms.load(), 10); } } // anonymous namespace