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
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 df5ada6..b5026a6 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 @@
android::ConcurrentQueue<VehiclePropValuePtr> mEventQueue;
android::ConcurrentQueue<VehiclePropValuePtr> mHeartBeatQueue;
+ // Wait until receive enough events in receivedEvents.
+ void waitForEvents(std::vector<VehiclePropValuePtr>* 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 @@
ASSERT_EQ(StatusCode::OK, status);
- std::this_thread::sleep_for(std::chrono::milliseconds(500));
+ std::vector<VehiclePropValuePtr> 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 d7547f6..2e59dbf 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 @@
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