Merge "Increase timer test tolerance." into udc-dev

This commit is contained in:
Yu Shan 2023-02-23 01:12:41 +00:00 committed by Android (Google) Code Review
commit 9c762cde05
2 changed files with 21 additions and 11 deletions

View file

@ -124,6 +124,17 @@ class DefaultVhalImplTest : public ::testing::Test {
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 @@ TEST_F(DefaultVhalImplTest, testSubscribe) {
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) {

View file

@ -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