Refactor collectEvent method in SensorsHidlTestBase

Bug: 195593357
Test: Compile, run VTS
Change-Id: Ie4db0bfd88841ce2821ed664053e27ab18262a56
This commit is contained in:
Arthur Ishiguro
2021-11-10 17:58:26 +00:00
parent e51ea5f40c
commit d3c693e6b4
3 changed files with 50 additions and 57 deletions

View File

@@ -537,10 +537,9 @@ TEST_P(SensorsHidlTest, CallInitializeTwice) {
activateAllSensors(true);
// Verify that the old environment does not receive any events
EXPECT_EQ(collectEvents(kCollectionTimeoutUs, kNumEvents, getEnvironment()).size(), 0);
EXPECT_EQ(getEnvironment()->collectEvents(kCollectionTimeoutUs, kNumEvents).size(), 0);
// Verify that the new event queue receives sensor events
EXPECT_GE(collectEvents(kCollectionTimeoutUs, kNumEvents, newEnv.get(), newEnv.get()).size(),
kNumEvents);
EXPECT_GE(newEnv.get()->collectEvents(kCollectionTimeoutUs, kNumEvents).size(), kNumEvents);
activateAllSensors(false);
// Cleanup the test environment
@@ -555,7 +554,7 @@ TEST_P(SensorsHidlTest, CallInitializeTwice) {
// Ensure that the original environment is receiving events
activateAllSensors(true);
EXPECT_GE(collectEvents(kCollectionTimeoutUs, kNumEvents).size(), kNumEvents);
EXPECT_GE(getEnvironment()->collectEvents(kCollectionTimeoutUs, kNumEvents).size(), kNumEvents);
activateAllSensors(false);
}
@@ -565,7 +564,7 @@ TEST_P(SensorsHidlTest, CleanupConnectionsOnInitialize) {
// Verify that events are received
constexpr useconds_t kCollectionTimeoutUs = 1000 * 1000; // 1s
constexpr int32_t kNumEvents = 1;
ASSERT_GE(collectEvents(kCollectionTimeoutUs, kNumEvents, getEnvironment()).size(), kNumEvents);
ASSERT_GE(getEnvironment()->collectEvents(kCollectionTimeoutUs, kNumEvents).size(), kNumEvents);
// Clear the active sensor handles so they are not disabled during TearDown
auto handles = mSensorHandles;
@@ -577,9 +576,9 @@ TEST_P(SensorsHidlTest, CleanupConnectionsOnInitialize) {
}
// Verify no events are received until sensors are re-activated
ASSERT_EQ(collectEvents(kCollectionTimeoutUs, kNumEvents, getEnvironment()).size(), 0);
ASSERT_EQ(getEnvironment()->collectEvents(kCollectionTimeoutUs, kNumEvents).size(), 0);
activateAllSensors(true);
ASSERT_GE(collectEvents(kCollectionTimeoutUs, kNumEvents, getEnvironment()).size(), kNumEvents);
ASSERT_GE(getEnvironment()->collectEvents(kCollectionTimeoutUs, kNumEvents).size(), kNumEvents);
// Disable sensors
activateAllSensors(false);

View File

@@ -198,49 +198,6 @@ class SensorsHidlTestBase : public testing::TestWithParam<std::string> {
RateLevel rate,
ISensors::configDirectReport_cb _hidl_cb) = 0;
std::vector<EventType> collectEvents(useconds_t timeLimitUs, size_t nEventLimit,
bool clearBeforeStart = true,
bool changeCollection = true) {
return collectEvents(timeLimitUs, nEventLimit, getEnvironment(), clearBeforeStart,
changeCollection);
}
std::vector<EventType> collectEvents(useconds_t timeLimitUs, size_t nEventLimit,
SensorsVtsEnvironmentBase<EventType>* environment,
bool clearBeforeStart = true,
bool changeCollection = true) {
std::vector<EventType> events;
constexpr useconds_t SLEEP_GRANULARITY = 100 * 1000; // granularity 100 ms
ALOGI("collect max of %zu events for %d us, clearBeforeStart %d", nEventLimit, timeLimitUs,
clearBeforeStart);
if (changeCollection) {
environment->setCollection(true);
}
if (clearBeforeStart) {
environment->catEvents(nullptr);
}
while (timeLimitUs > 0) {
useconds_t duration = std::min(SLEEP_GRANULARITY, timeLimitUs);
usleep(duration);
timeLimitUs -= duration;
environment->catEvents(&events);
if (events.size() >= nEventLimit) {
break;
}
ALOGV("time to go = %d, events to go = %d", (int)timeLimitUs,
(int)(nEventLimit - events.size()));
}
if (changeCollection) {
environment->setCollection(false);
}
return events;
}
void testStreamingOperation(SensorTypeVersion type, std::chrono::nanoseconds samplingPeriod,
std::chrono::seconds duration,
const SensorEventsChecker<EventType>& checker) {
@@ -268,7 +225,7 @@ class SensorsHidlTestBase : public testing::TestWithParam<std::string> {
ASSERT_EQ(batch(handle, samplingPeriodInNs, batchingPeriodInNs), Result::OK);
ASSERT_EQ(activate(handle, 1), Result::OK);
events = collectEvents(minTimeUs, minNEvent, getEnvironment(), true /*clearBeforeStart*/);
events = getEnvironment()->collectEvents(minTimeUs, minNEvent, true /*clearBeforeStart*/);
ASSERT_EQ(activate(handle, 0), Result::OK);
ALOGI("Collected %zu samples", events.size());
@@ -335,13 +292,13 @@ class SensorsHidlTestBase : public testing::TestWithParam<std::string> {
ASSERT_EQ(activate(handle, 1), Result::OK);
usleep(500000); // sleep 0.5 sec to wait for change rate to happen
events1 = collectEvents(collectionTimeoutUs, minNEvent, getEnvironment());
events1 = getEnvironment()->collectEvents(collectionTimeoutUs, minNEvent);
// second collection, without stopping the sensor
ASSERT_EQ(batch(handle, secondCollectionPeriod, batchingPeriodInNs), Result::OK);
usleep(500000); // sleep 0.5 sec to wait for change rate to happen
events2 = collectEvents(collectionTimeoutUs, minNEvent, getEnvironment());
events2 = getEnvironment()->collectEvents(collectionTimeoutUs, minNEvent);
// end of collection, stop sensor
ASSERT_EQ(activate(handle, 0), Result::OK);
@@ -447,16 +404,17 @@ class SensorsHidlTestBase : public testing::TestWithParam<std::string> {
getEnvironment()->setCollection(true);
// clean existing collections
collectEvents(0 /*timeLimitUs*/, 0 /*nEventLimit*/, true /*clearBeforeStart*/,
false /*change collection*/);
getEnvironment()->collectEvents(0 /*timeLimitUs*/, 0 /*nEventLimit*/,
true /*clearBeforeStart*/, false /*change collection*/);
// 0.8 + 0.2 times the batching period
usleep(batchingPeriodInNs / 1000 * 2 / 10);
ASSERT_EQ(flush(handle), Result::OK);
// plus some time for the event to deliver
events = collectEvents(allowedBatchDeliverTimeNs / 1000, minFifoCount,
false /*clearBeforeStart*/, false /*change collection*/);
events = getEnvironment()->collectEvents(allowedBatchDeliverTimeNs / 1000, minFifoCount,
false /*clearBeforeStart*/,
false /*change collection*/);
getEnvironment()->setCollection(false);
ASSERT_EQ(activate(handle, 0), Result::OK);

View File

@@ -25,6 +25,8 @@
#include <thread>
#include <vector>
#include <log/log.h>
template <class Event>
class IEventCallback {
public:
@@ -74,6 +76,40 @@ class SensorsVtsEnvironmentBase {
mCallback = nullptr;
}
std::vector<Event> collectEvents(useconds_t timeLimitUs, size_t nEventLimit,
bool clearBeforeStart = true, bool changeCollection = true) {
std::vector<Event> events;
constexpr useconds_t SLEEP_GRANULARITY = 100 * 1000; // granularity 100 ms
ALOGI("collect max of %zu events for %d us, clearBeforeStart %d", nEventLimit, timeLimitUs,
clearBeforeStart);
if (changeCollection) {
setCollection(true);
}
if (clearBeforeStart) {
catEvents(nullptr);
}
while (timeLimitUs > 0) {
useconds_t duration = std::min(SLEEP_GRANULARITY, timeLimitUs);
usleep(duration);
timeLimitUs -= duration;
catEvents(&events);
if (events.size() >= nEventLimit) {
break;
}
ALOGV("time to go = %d, events to go = %d", (int)timeLimitUs,
(int)(nEventLimit - events.size()));
}
if (changeCollection) {
setCollection(false);
}
return events;
}
protected:
SensorsVtsEnvironmentBase(const std::string& service_name)
: mCollectionEnabled(false), mCallback(nullptr) {