hwmonio: Add injection point to test internal behavior
With the complexities of the various behaviors within the read() method
of HwmonIO, introduce an injection point for testing. There is a
default available, and therefore this is a surgical change that only
impacts future tests.
Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: I1ead56c7fe1a2f87ebf316488e68f435a41c9d19
diff --git a/hwmonio.cpp b/hwmonio.cpp
index 9142c18..82d1a9d 100644
--- a/hwmonio.cpp
+++ b/hwmonio.cpp
@@ -25,6 +25,40 @@
namespace hwmonio
{
+int64_t FileSystem::read(const std::string& path) const
+{
+ int64_t val;
+ std::ifstream ifs;
+ ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit |
+ std::ifstream::eofbit);
+
+ errno = 0;
+ if (!ifs.is_open())
+ ifs.open(path);
+ ifs.clear();
+ ifs.seekg(0);
+ ifs >> val;
+
+ return val;
+}
+
+void FileSystem::write(const std::string& path, uint32_t value) const
+{
+ std::ofstream ofs;
+ ofs.exceptions(std::ofstream::failbit | std::ofstream::badbit |
+ std::ofstream::eofbit);
+
+ errno = 0;
+ if (!ofs.is_open())
+ ofs.open(path);
+ ofs.clear();
+ ofs.seekp(0);
+ ofs << value;
+ ofs.flush();
+}
+
+FileSystem fileSystemImpl;
+
static constexpr auto retryableErrors = {
/*
* Retry on bus or device errors or timeouts in case
@@ -71,7 +105,8 @@
EMSGSIZE,
};
-HwmonIO::HwmonIO(const std::string& path) : _p(path)
+HwmonIO::HwmonIO(const std::string& path, const FileSystemInterface* intf) :
+ _p(path), _intf(intf)
{
}
@@ -80,22 +115,13 @@
std::chrono::milliseconds delay) const
{
int64_t val;
- std::ifstream ifs;
auto fullPath = sysfs::make_sysfs_path(_p, type, id, sensor);
- ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit |
- std::ifstream::eofbit);
-
while (true)
{
try
{
- errno = 0;
- if (!ifs.is_open())
- ifs.open(fullPath);
- ifs.clear();
- ifs.seekg(0);
- ifs >> val;
+ val = _intf->read(fullPath);
}
catch (const std::exception& e)
{
@@ -148,12 +174,8 @@
size_t retries, std::chrono::milliseconds delay) const
{
- std::ofstream ofs;
auto fullPath = sysfs::make_sysfs_path(_p, type, id, sensor);
- ofs.exceptions(std::ofstream::failbit | std::ofstream::badbit |
- std::ofstream::eofbit);
-
// See comments in the read method for an explanation of the odd exception
// handling behavior here.
@@ -161,13 +183,7 @@
{
try
{
- errno = 0;
- if (!ofs.is_open())
- ofs.open(fullPath);
- ofs.clear();
- ofs.seekp(0);
- ofs << val;
- ofs.flush();
+ _intf->write(fullPath, val);
}
catch (const std::exception& e)
{