Skip to content

Commit

Permalink
Support respin sensors checking in HwTest
Browse files Browse the repository at this point in the history
Summary:
We were only checking for base sensors.

As respin sensors can be used in our platforms in basset pool, adding the support

Reviewed By: alandau

Differential Revision: D64476042

fbshipit-source-id: 95f24de09ea9cf901b0cbc8e5ca14dd01bd27a6d
  • Loading branch information
Justin Kim authored and facebook-github-bot committed Oct 17, 2024
1 parent d1f8cb9 commit 1357ecc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
2 changes: 1 addition & 1 deletion fboss/platform/sensor_service/SensorServiceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ class SensorServiceImpl {
const std::vector<std::string>& sensorNames);
std::map<std::string, SensorData> getAllSensorData();
void fetchSensorData();
std::vector<PmSensor> resolveSensors(const PmUnitSensors& pmUnitSensors);

FsdbSyncer* fsdbSyncer() {
return fsdbSyncer_.get();
}

private:
std::vector<PmSensor> resolveSensors(const PmUnitSensors& pmUnitSensors);
SensorData fetchSensorDataImpl(
const std::string& name,
const std::string& sysfsPath,
Expand Down
61 changes: 33 additions & 28 deletions fboss/platform/sensor_service/hw_test/SensorServiceHwTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,37 @@ bool sensorReadOk(const std::string& sensorName) {
return false;
}

TEST_F(SensorServiceHwTest, GetAllSensors) {
auto res = getSensors(std::vector<std::string>{});
std::vector<std::string> sensorNames;
std::vector<std::string> SensorServiceHwTest::allSensorNamesFromConfig() {
std::vector<std::string> sensors;
for (const auto& pmUnitSensors : *sensorConfig_.pmUnitSensorsList()) {
for (const auto& sensor : *pmUnitSensors.sensors()) {
sensorNames.push_back(*sensor.name());
for (const auto& sensor :
sensorServiceImpl_->resolveSensors(pmUnitSensors)) {
sensors.push_back(*sensor.name());
}
}
EXPECT_EQ(sensorNames.size(), res.sensorData()->size());
for (const auto& sensorName : sensorNames) {
return sensors;
}

std::vector<std::string> SensorServiceHwTest::someSensorNamesFromConfig() {
std::vector<std::string> sensors;
for (const auto& pmUnitSensors : *sensorConfig_.pmUnitSensorsList()) {
auto resolvedSensors = sensorServiceImpl_->resolveSensors(pmUnitSensors);
sensors.push_back(
*resolvedSensors[folly::Random::rand32(resolvedSensors.size())].name());
}
return sensors;
}

TEST_F(SensorServiceHwTest, GetAllSensors) {
auto res = getSensors(std::vector<std::string>{});
std::vector<std::string> allSensorNames = allSensorNamesFromConfig();
EXPECT_EQ(allSensorNames.size(), res.sensorData()->size());
for (const auto& sensorName : allSensorNames) {
auto it = std::find_if(
res.sensorData()->begin(),
res.sensorData()->end(),
[sensorNameCopy = sensorName](auto sensorData) {
return *sensorData.name() == sensorNameCopy;
[&](const auto& sensorData) {
return *sensorData.name() == sensorName;
});
EXPECT_NE(it, std::end(*res.sensorData()));
// only non-failed sensors will have value
Expand All @@ -78,15 +94,9 @@ TEST_F(SensorServiceHwTest, GetBogusSensor) {
}

TEST_F(SensorServiceHwTest, GetSomeSensors) {
std::vector<std::string> sensorNames;
for (const auto& pmUnitSensors : *sensorConfig_.pmUnitSensorsList()) {
if (pmUnitSensors.sensors()->size() > 0) {
sensorNames.push_back(*pmUnitSensors.sensors()->front().name());
}
}

auto response1 = getSensors(sensorNames);
EXPECT_EQ(response1.sensorData()->size(), sensorNames.size());
std::vector<std::string> someSensorNames = someSensorNamesFromConfig();
auto response1 = getSensors(someSensorNames);
EXPECT_EQ(response1.sensorData()->size(), someSensorNames.size());
for (const auto& sensorData : *response1.sensorData()) {
if (sensorReadOk(*sensorData.name())) {
EXPECT_TRUE(sensorData.value().has_value());
Expand All @@ -96,8 +106,8 @@ TEST_F(SensorServiceHwTest, GetSomeSensors) {
// Burn a second
std::this_thread::sleep_for(std::chrono::seconds(1));

auto response2 = getSensors(sensorNames);
EXPECT_EQ(response2.sensorData()->size(), sensorNames.size());
auto response2 = getSensors(someSensorNames);
EXPECT_EQ(response2.sensorData()->size(), someSensorNames.size());
for (const auto& sensorData : *response2.sensorData()) {
if (sensorReadOk(*sensorData.name())) {
EXPECT_TRUE(sensorData.value().has_value());
Expand Down Expand Up @@ -129,19 +139,14 @@ TEST_F(SensorServiceHwTest, GetSomeSensors) {
}

TEST_F(SensorServiceHwTest, GetSomeSensorsViaThrift) {
std::vector<std::string> sensorNames;
for (const auto& pmUnitSensors : *sensorConfig_.pmUnitSensorsList()) {
if (pmUnitSensors.sensors()->size() > 0) {
sensorNames.push_back(*pmUnitSensors.sensors()->front().name());
}
}
std::vector<std::string> someSensorNames = someSensorNamesFromConfig();
// Trigger a fetch before the thrift request hits the server.
sensorServiceImpl_->fetchSensorData();
apache::thrift::ScopedServerInterfaceThread server(sensorServiceHandler_);
auto client = server.newClient<apache::thrift::Client<SensorServiceThrift>>();
SensorReadResponse response;
client->sync_getSensorValuesByNames(response, sensorNames);
EXPECT_EQ(response.sensorData()->size(), sensorNames.size());
client->sync_getSensorValuesByNames(response, someSensorNames);
EXPECT_EQ(response.sensorData()->size(), someSensorNames.size());
for (const auto& sensorData : *response.sensorData()) {
if (sensorReadOk(*sensorData.name())) {
EXPECT_TRUE(sensorData.value().has_value());
Expand Down
2 changes: 2 additions & 0 deletions fboss/platform/sensor_service/hw_test/SensorServiceHwTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class SensorServiceHwTest : public ::testing::Test {

protected:
SensorReadResponse getSensors(const std::vector<std::string>& sensors);
std::vector<std::string> allSensorNamesFromConfig();
std::vector<std::string> someSensorNamesFromConfig();
std::shared_ptr<SensorServiceImpl> sensorServiceImpl_;
std::shared_ptr<SensorServiceThriftHandler> sensorServiceHandler_;
SensorConfig sensorConfig_;
Expand Down

0 comments on commit 1357ecc

Please sign in to comment.