aboutsummaryrefslogtreecommitdiff
path: root/cpp/watchdog
diff options
context:
space:
mode:
authorLakshman Annadorai <lakshmana@google.com>2022-01-31 20:23:57 +0000
committerLakshman Annadorai <lakshmana@google.com>2022-03-03 19:08:41 +0000
commitbf45e5606bbc08deed6e90755868c622cd1c3477 (patch)
tree3ce8ef6c40b1a622b5cb312755230a20eb57ab1e /cpp/watchdog
parentf0d542c284f058ac71bef268fc3ca24c47bf66cb (diff)
downloadCar-bf45e5606bbc08deed6e90755868c622cd1c3477.tar.gz
Move proc fs access check outside constructor.
The car watchdog native daemon unittest, libwatchdog_test, is killed due to sepolicy violation. This violation happens when the unittest accesses proc fs files. Move the proc fs access check outside the constructors, so classes that depend on the proc fs collectors can mock them safely after calling the real constructors. - Update UidStatsCollector to check whether UidIoStatsCollector is enabled. - Refactor WatchdogPerfServiceTest to use a test class as it makes the tests cleaner. - Add missing GUARDED_BY statements in UidStatsCollector. Test: atest libwatchdog_test Bug: 216642418 Bug: 215711169 Change-Id: If750481cb95803253c2787c7e4459b68271bb8d7 Merged-In: If750481cb95803253c2787c7e4459b68271bb8d7 (cherry picked from commit 287c8fc74d88b0c781d1ba7808730df65e2f950e)
Diffstat (limited to 'cpp/watchdog')
-rw-r--r--cpp/watchdog/server/src/ProcDiskStats.cpp2
-rw-r--r--cpp/watchdog/server/src/ProcDiskStats.h33
-rw-r--r--cpp/watchdog/server/src/ProcStat.cpp2
-rw-r--r--cpp/watchdog/server/src/ProcStat.h31
-rw-r--r--cpp/watchdog/server/src/UidIoStatsCollector.cpp2
-rw-r--r--cpp/watchdog/server/src/UidIoStatsCollector.h30
-rw-r--r--cpp/watchdog/server/src/UidProcStatsCollector.cpp14
-rw-r--r--cpp/watchdog/server/src/UidProcStatsCollector.h62
-rw-r--r--cpp/watchdog/server/src/UidStatsCollector.cpp3
-rw-r--r--cpp/watchdog/server/src/UidStatsCollector.h16
-rw-r--r--cpp/watchdog/server/src/WatchdogPerfService.cpp3
-rw-r--r--cpp/watchdog/server/tests/MockProcDiskStats.h1
-rw-r--r--cpp/watchdog/server/tests/MockProcStat.h1
-rw-r--r--cpp/watchdog/server/tests/MockUidIoStatsCollector.h1
-rw-r--r--cpp/watchdog/server/tests/MockUidProcStatsCollector.h1
-rw-r--r--cpp/watchdog/server/tests/MockUidStatsCollector.h1
-rw-r--r--cpp/watchdog/server/tests/ProcDiskStatsTest.cpp4
-rw-r--r--cpp/watchdog/server/tests/ProcStatTest.cpp14
-rw-r--r--cpp/watchdog/server/tests/UidIoStatsCollectorTest.cpp4
-rw-r--r--cpp/watchdog/server/tests/UidProcStatsCollectorTest.cpp9
-rw-r--r--cpp/watchdog/server/tests/UidStatsCollectorTest.cpp10
-rw-r--r--cpp/watchdog/server/tests/WatchdogPerfServiceTest.cpp586
22 files changed, 464 insertions, 366 deletions
diff --git a/cpp/watchdog/server/src/ProcDiskStats.cpp b/cpp/watchdog/server/src/ProcDiskStats.cpp
index d0d5c0fa25..bf3e4e0908 100644
--- a/cpp/watchdog/server/src/ProcDiskStats.cpp
+++ b/cpp/watchdog/server/src/ProcDiskStats.cpp
@@ -202,7 +202,7 @@ bool DiskStats::EqualByPartition::operator()(const DiskStats& lhs, const DiskSta
}
Result<void> ProcDiskStats::collect() {
- if (!kEnabled) {
+ if (!mEnabled) {
return Error() << "Failed to access " << kPath;
}
diff --git a/cpp/watchdog/server/src/ProcDiskStats.h b/cpp/watchdog/server/src/ProcDiskStats.h
index ffdf8ffcd1..5169c21f1a 100644
--- a/cpp/watchdog/server/src/ProcDiskStats.h
+++ b/cpp/watchdog/server/src/ProcDiskStats.h
@@ -78,6 +78,8 @@ class IProcDiskStatsInterface : public android::RefBase {
public:
using PerPartitionDiskStats = ::std::unordered_set<DiskStats, DiskStats::HashByPartition,
DiskStats::EqualByPartition>;
+ // Initializes the collector.
+ virtual void init() = 0;
// Collects the system-wide block devices statistics.
virtual android::base::Result<void> collect() = 0;
@@ -95,14 +97,20 @@ public:
virtual std::string filePath() const = 0;
};
-class ProcDiskStats : public IProcDiskStatsInterface {
+class ProcDiskStats final : public IProcDiskStatsInterface {
public:
- explicit ProcDiskStats(const std::string& path = kProcDiskStatsPath) :
- kEnabled(!access(path.c_str(), R_OK)),
- kPath(path) {}
+ explicit ProcDiskStats(const std::string& path = kProcDiskStatsPath) : kPath(path) {}
~ProcDiskStats() {}
+ void init() {
+ Mutex::Autolock lock(mMutex);
+ // Note: Verify proc file access outside the constructor. Otherwise, the unittests of
+ // dependent classes would call the constructor before mocking and get killed due to
+ // sepolicy violation.
+ mEnabled = access(kPath.c_str(), R_OK) == 0;
+ }
+
android::base::Result<void> collect();
PerPartitionDiskStats latestPerPartitionDiskStats() const {
@@ -115,14 +123,23 @@ public:
return mDeltaSystemWideDiskStats;
}
- bool enabled() const { return kEnabled; }
+ bool enabled() const {
+ Mutex::Autolock lock(mMutex);
+ return mEnabled;
+ }
std::string filePath() const { return kPath; }
private:
+ // Path to disk stats file.
+ const std::string kPath;
+
// Makes sure only one collection is running at any given time.
mutable Mutex mMutex;
+ // True if |kPath| is accessible.
+ bool mEnabled GUARDED_BY(mMutex);
+
// Delta of per-UID I/O usage since last before collection.
DiskStats mDeltaSystemWideDiskStats GUARDED_BY(mMutex);
@@ -132,12 +149,6 @@ private:
* storing the stats per-disk helps to deal with this issue.
*/
PerPartitionDiskStats mLatestPerPartitionDiskStats GUARDED_BY(mMutex);
-
- // True if |kPath| is accessible.
- const bool kEnabled;
-
- // Path to disk stats file.
- const std::string kPath;
};
} // namespace watchdog
diff --git a/cpp/watchdog/server/src/ProcStat.cpp b/cpp/watchdog/server/src/ProcStat.cpp
index 7c5337b382..8777c7e078 100644
--- a/cpp/watchdog/server/src/ProcStat.cpp
+++ b/cpp/watchdog/server/src/ProcStat.cpp
@@ -73,7 +73,7 @@ bool parseProcsCount(const std::string& data, uint32_t* out) {
} // namespace
Result<void> ProcStat::collect() {
- if (!kEnabled) {
+ if (!mEnabled) {
return Error() << "Can not access " << kPath;
}
diff --git a/cpp/watchdog/server/src/ProcStat.h b/cpp/watchdog/server/src/ProcStat.h
index 3bb03b4eca..d8529824c6 100644
--- a/cpp/watchdog/server/src/ProcStat.h
+++ b/cpp/watchdog/server/src/ProcStat.h
@@ -90,20 +90,29 @@ public:
// Collector/parser for `/proc/stat` file.
class ProcStat : public RefBase {
public:
- explicit ProcStat(const std::string& path = kProcStatPath) :
- mLatestStats({}),
- kEnabled(!access(path.c_str(), R_OK)),
- kPath(path) {}
+ explicit ProcStat(const std::string& path = kProcStatPath) : kPath(path), mLatestStats({}) {}
virtual ~ProcStat() {}
+ // Initializes the collector.
+ virtual void init() {
+ Mutex::Autolock lock(mMutex);
+ // Note: Verify proc file access outside the constructor. Otherwise, the unittests of
+ // dependent classes would call the constructor before mocking and get killed due to
+ // sepolicy violation.
+ mEnabled = access(kPath.c_str(), R_OK) == 0;
+ }
+
// Collects proc stat delta since the last collection.
virtual android::base::Result<void> collect();
/* Returns true when the proc stat file is accessible. Otherwise, returns false.
* Called by WatchdogPerfService and tests.
*/
- virtual bool enabled() { return kEnabled; }
+ virtual bool enabled() {
+ Mutex::Autolock lock(mMutex);
+ return mEnabled;
+ }
virtual std::string filePath() { return kProcStatPath; }
@@ -123,20 +132,20 @@ private:
// Reads the contents of |kPath|.
android::base::Result<ProcStatInfo> getProcStatLocked() const;
+ // Path to proc stat file. Default path is |kProcStatPath|.
+ const std::string kPath;
+
// Makes sure only one collection is running at any given time.
mutable Mutex mMutex;
+ // True if |kPath| is accessible.
+ bool mEnabled GUARDED_BY(mMutex);
+
// Latest dump of CPU stats from the file at |kPath|.
ProcStatInfo mLatestStats GUARDED_BY(mMutex);
// Delta of CPU stats from the latest collection.
ProcStatInfo mDeltaStats GUARDED_BY(mMutex);
-
- // True if |kPath| is accessible.
- const bool kEnabled;
-
- // Path to proc stat file. Default path is |kProcStatPath|.
- const std::string kPath;
};
} // namespace watchdog
diff --git a/cpp/watchdog/server/src/UidIoStatsCollector.cpp b/cpp/watchdog/server/src/UidIoStatsCollector.cpp
index 9e8bc32623..c609f5dfcb 100644
--- a/cpp/watchdog/server/src/UidIoStatsCollector.cpp
+++ b/cpp/watchdog/server/src/UidIoStatsCollector.cpp
@@ -99,7 +99,7 @@ std::string UidIoStats::toString() const {
}
Result<void> UidIoStatsCollector::collect() {
- if (!kEnabled) {
+ if (!mEnabled) {
return Error() << "Can not access " << kPath;
}
diff --git a/cpp/watchdog/server/src/UidIoStatsCollector.h b/cpp/watchdog/server/src/UidIoStatsCollector.h
index b5f4494574..cf7c6b7d72 100644
--- a/cpp/watchdog/server/src/UidIoStatsCollector.h
+++ b/cpp/watchdog/server/src/UidIoStatsCollector.h
@@ -85,6 +85,8 @@ public:
// Collector/Parser for `/proc/uid_io/stats`.
class UidIoStatsCollectorInterface : public RefBase {
public:
+ // Initializes the collector.
+ virtual void init() = 0;
// Collects the per-UID I/O stats.
virtual android::base::Result<void> collect() = 0;
// Returns the latest per-uid I/O stats.
@@ -99,11 +101,18 @@ public:
class UidIoStatsCollector final : public UidIoStatsCollectorInterface {
public:
- explicit UidIoStatsCollector(const std::string& path = kUidIoStatsPath) :
- kEnabled(!access(path.c_str(), R_OK)), kPath(path) {}
+ explicit UidIoStatsCollector(const std::string& path = kUidIoStatsPath) : kPath(path) {}
~UidIoStatsCollector() {}
+ void init() override {
+ Mutex::Autolock lock(mMutex);
+ // Note: Verify proc file access outside the constructor. Otherwise, the unittests of
+ // dependent classes would call the constructor before mocking and get killed due to
+ // sepolicy violation.
+ mEnabled = access(kPath.c_str(), R_OK) == 0;
+ }
+
android::base::Result<void> collect() override;
const std::unordered_map<uid_t, UidIoStats> latestStats() const override {
@@ -116,7 +125,10 @@ public:
return mDeltaStats;
}
- bool enabled() const override { return kEnabled; }
+ bool enabled() const override {
+ Mutex::Autolock lock(mMutex);
+ return mEnabled;
+ }
const std::string filePath() const override { return kPath; }
@@ -124,20 +136,20 @@ private:
// Reads the contents of |kPath|.
android::base::Result<std::unordered_map<uid_t, UidIoStats>> readUidIoStatsLocked() const;
+ // Path to uid_io stats file. Default path is |kUidIoStatsPath|.
+ const std::string kPath;
+
// Makes sure only one collection is running at any given time.
mutable Mutex mMutex;
+ // True if |kPath| is accessible.
+ bool mEnabled GUARDED_BY(mMutex);
+
// Latest dump from the file at |kPath|.
std::unordered_map<uid_t, UidIoStats> mLatestStats GUARDED_BY(mMutex);
// Delta of per-UID I/O stats since last before collection.
std::unordered_map<uid_t, UidIoStats> mDeltaStats GUARDED_BY(mMutex);
-
- // True if kPath is accessible.
- const bool kEnabled;
-
- // Path to uid_io stats file. Default path is |kUidIoStatsPath|.
- const std::string kPath;
};
} // namespace watchdog
diff --git a/cpp/watchdog/server/src/UidProcStatsCollector.cpp b/cpp/watchdog/server/src/UidProcStatsCollector.cpp
index da3bffc05d..527d9454b2 100644
--- a/cpp/watchdog/server/src/UidProcStatsCollector.cpp
+++ b/cpp/watchdog/server/src/UidProcStatsCollector.cpp
@@ -205,6 +205,20 @@ std::string UidProcStats::toString() const {
return buffer;
}
+void UidProcStatsCollector::init() {
+ // Note: Verify proc file access outside the constructor. Otherwise, the unittests of
+ // dependent classes would call the constructor before mocking and get killed due to
+ // sepolicy violation.
+ std::string pidStatPath = StringPrintf((mPath + kStatFileFormat).c_str(), PID_FOR_INIT);
+ std::string tidStatPath = StringPrintf((mPath + kTaskDirFormat + kStatFileFormat).c_str(),
+ PID_FOR_INIT, PID_FOR_INIT);
+ std::string pidStatusPath = StringPrintf((mPath + kStatusFileFormat).c_str(), PID_FOR_INIT);
+
+ Mutex::Autolock lock(mMutex);
+ mEnabled = access(pidStatPath.c_str(), R_OK) == 0 && access(tidStatPath.c_str(), R_OK) == 0 &&
+ access(pidStatusPath.c_str(), R_OK) == 0;
+}
+
Result<void> UidProcStatsCollector::collect() {
if (!mEnabled) {
return Error() << "Can not access PID stat files under " << kProcDirPath;
diff --git a/cpp/watchdog/server/src/UidProcStatsCollector.h b/cpp/watchdog/server/src/UidProcStatsCollector.h
index d0ec3c03e4..ffe5c8e1e7 100644
--- a/cpp/watchdog/server/src/UidProcStatsCollector.h
+++ b/cpp/watchdog/server/src/UidProcStatsCollector.h
@@ -62,12 +62,12 @@ struct UidProcStats {
std::string toString() const;
};
-/**
- * Collector/parser for `/proc/[pid]/stat`, `/proc/[pid]/task/[tid]/stat` and /proc/[pid]/status`
- * files.
- */
+// Collector/parser for `/proc/[pid]/stat`, `/proc/[pid]/task/[tid]/stat` and /proc/[pid]/status`
+// files.
class UidProcStatsCollectorInterface : public RefBase {
public:
+ // Initializes the collector.
+ virtual void init() = 0;
// Collects the per-uid stats from /proc directory.
virtual android::base::Result<void> collect() = 0;
// Returns the latest per-uid process stats.
@@ -83,19 +83,12 @@ public:
class UidProcStatsCollector final : public UidProcStatsCollectorInterface {
public:
explicit UidProcStatsCollector(const std::string& path = kProcDirPath) :
- mLatestStats({}),
- mPath(path) {
- std::string pidStatPath = StringPrintf((mPath + kStatFileFormat).c_str(), PID_FOR_INIT);
- std::string tidStatPath = StringPrintf((mPath + kTaskDirFormat + kStatFileFormat).c_str(),
- PID_FOR_INIT, PID_FOR_INIT);
- std::string pidStatusPath = StringPrintf((mPath + kStatusFileFormat).c_str(), PID_FOR_INIT);
-
- mEnabled = !access(pidStatPath.c_str(), R_OK) && !access(tidStatPath.c_str(), R_OK) &&
- !access(pidStatusPath.c_str(), R_OK);
- }
+ mPath(path), mLatestStats({}) {}
~UidProcStatsCollector() {}
+ void init() override;
+
android::base::Result<void> collect() override;
const std::unordered_map<uid_t, UidProcStats> latestStats() const {
@@ -108,45 +101,42 @@ public:
return mDeltaStats;
}
- bool enabled() const { return mEnabled; }
+ bool enabled() const {
+ Mutex::Autolock lock(mMutex);
+ return mEnabled;
+ }
const std::string dirPath() const { return mPath; }
private:
android::base::Result<std::unordered_map<uid_t, UidProcStats>> readUidProcStatsLocked() const;
- /**
- * Reads the contents of the below files:
- * 1. Pid stat file at |mPath| + |kStatFileFormat|
- * 2. Aggregated per-process status at |mPath| + |kStatusFileFormat|
- * 3. Tid stat file at |mPath| + |kTaskDirFormat| + |kStatFileFormat|
- */
+ // Reads the contents of the below files:
+ // 1. Pid stat file at |mPath| + |kStatFileFormat|
+ // 2. Aggregated per-process status at |mPath| + |kStatusFileFormat|
+ // 3. Tid stat file at |mPath| + |kTaskDirFormat| + |kStatFileFormat|
android::base::Result<std::tuple<uid_t, ProcessStats>> readProcessStatsLocked(pid_t pid) const;
+ // Proc directory path. Default value is |kProcDirPath|.
+ // Updated by tests to point to a different location when needed.
+ std::string mPath;
+
// Makes sure only one collection is running at any given time.
mutable Mutex mMutex;
+ // True if the below files are accessible:
+ // 1. Pid stat file at |mPath| + |kTaskStatFileFormat|
+ // 2. Tid stat file at |mPath| + |kTaskDirFormat| + |kStatFileFormat|
+ // 3. Pid status file at |mPath| + |kStatusFileFormat|
+ // Otherwise, set to false.
+ bool mEnabled GUARDED_BY(mMutex);
+
// Latest dump of per-UID stats.
std::unordered_map<uid_t, UidProcStats> mLatestStats GUARDED_BY(mMutex);
// Latest delta of per-uid stats.
std::unordered_map<uid_t, UidProcStats> mDeltaStats GUARDED_BY(mMutex);
- /**
- * True if the below files are accessible:
- * 1. Pid stat file at |mPath| + |kTaskStatFileFormat|
- * 2. Tid stat file at |mPath| + |kTaskDirFormat| + |kStatFileFormat|
- * 3. Pid status file at |mPath| + |kStatusFileFormat|
- * Otherwise, set to false.
- */
- bool mEnabled;
-
- /**
- * Proc directory path. Default value is |kProcDirPath|.
- * Updated by tests to point to a different location when needed.
- */
- std::string mPath;
-
FRIEND_TEST(IoPerfCollectionTest, TestValidProcPidContents);
FRIEND_TEST(UidProcStatsCollectorTest, TestValidStatFiles);
FRIEND_TEST(UidProcStatsCollectorTest, TestHandlesProcessTerminationBetweenScanningAndParsing);
diff --git a/cpp/watchdog/server/src/UidStatsCollector.cpp b/cpp/watchdog/server/src/UidStatsCollector.cpp
index 19d0fc94f5..a7fef59404 100644
--- a/cpp/watchdog/server/src/UidStatsCollector.cpp
+++ b/cpp/watchdog/server/src/UidStatsCollector.cpp
@@ -46,7 +46,8 @@ std::string UidStats::genericPackageName() const {
}
Result<void> UidStatsCollector::collect() {
- if (mUidProcStatsCollector->enabled()) {
+ Mutex::Autolock lock(mMutex);
+ if (mUidIoStatsCollector->enabled()) {
if (const auto& result = mUidIoStatsCollector->collect(); !result.ok()) {
return Error() << "Failed to collect per-uid I/O stats: " << result.error();
}
diff --git a/cpp/watchdog/server/src/UidStatsCollector.h b/cpp/watchdog/server/src/UidStatsCollector.h
index 9a81abecdf..09ddc0a60b 100644
--- a/cpp/watchdog/server/src/UidStatsCollector.h
+++ b/cpp/watchdog/server/src/UidStatsCollector.h
@@ -56,6 +56,8 @@ struct UidStats {
// Collector/Aggregator for per-UID I/O and proc stats.
class UidStatsCollectorInterface : public RefBase {
public:
+ // Initializes the collector.
+ virtual void init() = 0;
// Collects the per-UID I/O and proc stats.
virtual android::base::Result<void> collect() = 0;
// Returns the latest per-uid I/O and proc stats.
@@ -73,6 +75,12 @@ public:
mUidIoStatsCollector(android::sp<UidIoStatsCollector>::make()),
mUidProcStatsCollector(android::sp<UidProcStatsCollector>::make()) {}
+ void init() override {
+ Mutex::Autolock lock(mMutex);
+ mUidIoStatsCollector->init();
+ mUidProcStatsCollector->init();
+ }
+
android::base::Result<void> collect() override;
const std::vector<UidStats> latestStats() const override {
@@ -98,13 +106,13 @@ private:
mutable Mutex mMutex;
- android::sp<UidIoStatsCollectorInterface> mUidIoStatsCollector;
+ android::sp<UidIoStatsCollectorInterface> mUidIoStatsCollector GUARDED_BY(mMutex);
- android::sp<UidProcStatsCollectorInterface> mUidProcStatsCollector;
+ android::sp<UidProcStatsCollectorInterface> mUidProcStatsCollector GUARDED_BY(mMutex);
- std::vector<UidStats> mLatestStats;
+ std::vector<UidStats> mLatestStats GUARDED_BY(mMutex);
- std::vector<UidStats> mDeltaStats;
+ std::vector<UidStats> mDeltaStats GUARDED_BY(mMutex);
// For unit tests.
friend class internal::UidStatsCollectorPeer;
diff --git a/cpp/watchdog/server/src/WatchdogPerfService.cpp b/cpp/watchdog/server/src/WatchdogPerfService.cpp
index 2761b34b4d..27493f698f 100644
--- a/cpp/watchdog/server/src/WatchdogPerfService.cpp
+++ b/cpp/watchdog/server/src/WatchdogPerfService.cpp
@@ -198,6 +198,9 @@ Result<void> WatchdogPerfService::start() {
mCurrCollectionEvent = EventType::TERMINATED;
return Error() << "No data processor is registered";
}
+ mUidStatsCollector->init();
+ mProcStat->init();
+ mProcDiskStats->init();
}
mCollectionThread = std::thread([&]() {
diff --git a/cpp/watchdog/server/tests/MockProcDiskStats.h b/cpp/watchdog/server/tests/MockProcDiskStats.h
index c8c9d6a630..93cdf00bc7 100644
--- a/cpp/watchdog/server/tests/MockProcDiskStats.h
+++ b/cpp/watchdog/server/tests/MockProcDiskStats.h
@@ -31,6 +31,7 @@ namespace watchdog {
class MockProcDiskStats : public IProcDiskStatsInterface {
public:
MockProcDiskStats() { ON_CALL(*this, enabled()).WillByDefault(::testing::Return(true)); }
+ MOCK_METHOD(void, init, (), (override));
MOCK_METHOD(android::base::Result<void>, collect, (), (override));
MOCK_METHOD(PerPartitionDiskStats, latestPerPartitionDiskStats, (), (const, override));
MOCK_METHOD(DiskStats, deltaSystemWideDiskStats, (), (const, override));
diff --git a/cpp/watchdog/server/tests/MockProcStat.h b/cpp/watchdog/server/tests/MockProcStat.h
index 81fd6a8b0b..9cd0d2e72b 100644
--- a/cpp/watchdog/server/tests/MockProcStat.h
+++ b/cpp/watchdog/server/tests/MockProcStat.h
@@ -31,6 +31,7 @@ namespace watchdog {
class MockProcStat : public ProcStat {
public:
MockProcStat() { ON_CALL(*this, enabled()).WillByDefault(::testing::Return(true)); }
+ MOCK_METHOD(void, init, (), (override));
MOCK_METHOD(bool, enabled, (), (override));
MOCK_METHOD(android::base::Result<void>, collect, (), (override));
MOCK_METHOD(const ProcStatInfo, latestStats, (), (const, override));
diff --git a/cpp/watchdog/server/tests/MockUidIoStatsCollector.h b/cpp/watchdog/server/tests/MockUidIoStatsCollector.h
index 7fe8fc9aa9..466237fb25 100644
--- a/cpp/watchdog/server/tests/MockUidIoStatsCollector.h
+++ b/cpp/watchdog/server/tests/MockUidIoStatsCollector.h
@@ -32,6 +32,7 @@ namespace watchdog {
class MockUidIoStatsCollector : public UidIoStatsCollectorInterface {
public:
MockUidIoStatsCollector() { ON_CALL(*this, enabled()).WillByDefault(::testing::Return(true)); }
+ MOCK_METHOD(void, init, (), (override));
MOCK_METHOD(android::base::Result<void>, collect, (), (override));
MOCK_METHOD((const std::unordered_map<uid_t, UidIoStats>), latestStats, (), (const, override));
MOCK_METHOD((const std::unordered_map<uid_t, UidIoStats>), deltaStats, (), (const, override));
diff --git a/cpp/watchdog/server/tests/MockUidProcStatsCollector.h b/cpp/watchdog/server/tests/MockUidProcStatsCollector.h
index a16fa371d2..b65dc10468 100644
--- a/cpp/watchdog/server/tests/MockUidProcStatsCollector.h
+++ b/cpp/watchdog/server/tests/MockUidProcStatsCollector.h
@@ -34,6 +34,7 @@ public:
MockUidProcStatsCollector() {
ON_CALL(*this, enabled()).WillByDefault(::testing::Return(true));
}
+ MOCK_METHOD(void, init, (), (override));
MOCK_METHOD(android::base::Result<void>, collect, (), (override));
MOCK_METHOD((const std::unordered_map<uid_t, UidProcStats>), latestStats, (),
(const, override));
diff --git a/cpp/watchdog/server/tests/MockUidStatsCollector.h b/cpp/watchdog/server/tests/MockUidStatsCollector.h
index 45671c156b..7957eeb0db 100644
--- a/cpp/watchdog/server/tests/MockUidStatsCollector.h
+++ b/cpp/watchdog/server/tests/MockUidStatsCollector.h
@@ -32,6 +32,7 @@ namespace watchdog {
class MockUidStatsCollector : public UidStatsCollectorInterface {
public:
MockUidStatsCollector() { ON_CALL(*this, enabled()).WillByDefault(::testing::Return(true)); }
+ MOCK_METHOD(void, init, (), (override));
MOCK_METHOD(android::base::Result<void>, collect, (), (override));
MOCK_METHOD((const std::vector<UidStats>), latestStats, (), (const, override));
MOCK_METHOD((const std::vector<UidStats>), deltaStats, (), (const, override));
diff --git a/cpp/watchdog/server/tests/ProcDiskStatsTest.cpp b/cpp/watchdog/server/tests/ProcDiskStatsTest.cpp
index 9c3689777c..7e5e71b44b 100644
--- a/cpp/watchdog/server/tests/ProcDiskStatsTest.cpp
+++ b/cpp/watchdog/server/tests/ProcDiskStatsTest.cpp
@@ -97,6 +97,8 @@ TEST(ProcDiskStatsTest, TestValidStatsFile) {
DiskStats expectedDiskStats = aggregateSystemWideDiskStats(latestDiskStats);
ProcDiskStats procDiskStats(tf.path);
+ procDiskStats.init();
+
ASSERT_TRUE(procDiskStats.enabled()) << "Temporary file is inaccessible";
ASSERT_RESULT_OK(procDiskStats.collect());
@@ -135,6 +137,8 @@ TEST(ProcDiskStatsTest, TestErrorOnInvalidStatsFile) {
ASSERT_TRUE(WriteStringToFile(contents, tf.path));
ProcDiskStats procDiskStats(tf.path);
+ procDiskStats.init();
+
ASSERT_TRUE(procDiskStats.enabled()) << "Temporary file is inaccessible";
EXPECT_FALSE(procDiskStats.collect().ok()) << "No error returned for invalid file";
}
diff --git a/cpp/watchdog/server/tests/ProcStatTest.cpp b/cpp/watchdog/server/tests/ProcStatTest.cpp
index ee3316289e..3384ad9f8c 100644
--- a/cpp/watchdog/server/tests/ProcStatTest.cpp
+++ b/cpp/watchdog/server/tests/ProcStatTest.cpp
@@ -86,6 +86,8 @@ TEST(ProcStatTest, TestValidStatFile) {
ASSERT_TRUE(WriteStringToFile(firstSnapshot, tf.path));
ProcStat procStat(tf.path);
+ procStat.init();
+
ASSERT_TRUE(procStat.enabled()) << "Temporary file is inaccessible";
ASSERT_RESULT_OK(procStat.collect());
@@ -154,6 +156,8 @@ TEST(ProcStatTest, TestErrorOnCorruptedStatFile) {
ASSERT_TRUE(WriteStringToFile(contents, tf.path));
ProcStat procStat(tf.path);
+ procStat.init();
+
ASSERT_TRUE(procStat.enabled()) << "Temporary file is inaccessible";
EXPECT_FALSE(procStat.collect().ok()) << "No error returned for corrupted file";
}
@@ -177,6 +181,8 @@ TEST(ProcStatTest, TestErrorOnMissingCpuLine) {
ASSERT_TRUE(WriteStringToFile(contents, tf.path));
ProcStat procStat(tf.path);
+ procStat.init();
+
ASSERT_TRUE(procStat.enabled()) << "Temporary file is inaccessible";
EXPECT_FALSE(procStat.collect().ok()) << "No error returned due to missing cpu line";
}
@@ -200,6 +206,8 @@ TEST(ProcStatTest, TestErrorOnMissingProcsRunningLine) {
ASSERT_TRUE(WriteStringToFile(contents, tf.path));
ProcStat procStat(tf.path);
+ procStat.init();
+
ASSERT_TRUE(procStat.enabled()) << "Temporary file is inaccessible";
EXPECT_FALSE(procStat.collect().ok()) << "No error returned due to missing procs_running line";
}
@@ -223,6 +231,8 @@ TEST(ProcStatTest, TestErrorOnMissingProcsBlockedLine) {
ASSERT_TRUE(WriteStringToFile(contents, tf.path));
ProcStat procStat(tf.path);
+ procStat.init();
+
ASSERT_TRUE(procStat.enabled()) << "Temporary file is inaccessible";
EXPECT_FALSE(procStat.collect().ok()) << "No error returned due to missing procs_blocked line";
}
@@ -248,12 +258,16 @@ TEST(ProcStatTest, TestErrorOnUnknownProcsLine) {
ASSERT_TRUE(WriteStringToFile(contents, tf.path));
ProcStat procStat(tf.path);
+ procStat.init();
+
ASSERT_TRUE(procStat.enabled()) << "Temporary file is inaccessible";
EXPECT_FALSE(procStat.collect().ok()) << "No error returned due to unknown procs line";
}
TEST(ProcStatTest, TestProcStatContentsFromDevice) {
ProcStat procStat;
+ procStat.init();
+
ASSERT_TRUE(procStat.enabled()) << kProcStatPath << " file is inaccessible";
ASSERT_RESULT_OK(procStat.collect());
diff --git a/cpp/watchdog/server/tests/UidIoStatsCollectorTest.cpp b/cpp/watchdog/server/tests/UidIoStatsCollectorTest.cpp
index fe30844795..1deb47c4cb 100644
--- a/cpp/watchdog/server/tests/UidIoStatsCollectorTest.cpp
+++ b/cpp/watchdog/server/tests/UidIoStatsCollectorTest.cpp
@@ -68,6 +68,8 @@ TEST(UidIoStatsCollectorTest, TestValidStatFile) {
ASSERT_TRUE(WriteStringToFile(firstSnapshot, tf.path));
UidIoStatsCollector collector(tf.path);
+ collector.init();
+
ASSERT_TRUE(collector.enabled()) << "Temporary file is inaccessible";
ASSERT_RESULT_OK(collector.collect());
@@ -112,6 +114,8 @@ TEST(UidIoStatsCollectorTest, TestErrorOnInvalidStatFile) {
ASSERT_TRUE(WriteStringToFile(contents, tf.path));
UidIoStatsCollector collector(tf.path);
+ collector.init();
+
ASSERT_TRUE(collector.enabled()) << "Temporary file is inaccessible";
EXPECT_FALSE(collector.collect().ok()) << "No error returned for invalid file";
}
diff --git a/cpp/watchdog/server/tests/UidProcStatsCollectorTest.cpp b/cpp/watchdog/server/tests/UidProcStatsCollectorTest.cpp
index c9eb6d4b94..53ee19c7b7 100644
--- a/cpp/watchdog/server/tests/UidProcStatsCollectorTest.cpp
+++ b/cpp/watchdog/server/tests/UidProcStatsCollectorTest.cpp
@@ -102,6 +102,7 @@ TEST(UidProcStatsCollectorTest, TestValidStatFiles) {
perProcessStatus, perThreadStat));
UidProcStatsCollector collector(firstSnapshot.path);
+ collector.init();
ASSERT_TRUE(collector.enabled())
<< "Files under the path `" << firstSnapshot.path << "` are inaccessible";
@@ -208,6 +209,7 @@ TEST(UidProcStatsCollectorTest, TestHandlesProcessTerminationBetweenScanningAndP
perThreadStat));
UidProcStatsCollector collector(procDir.path);
+ collector.init();
ASSERT_TRUE(collector.enabled())
<< "Files under the path `" << procDir.path << "` are inaccessible";
@@ -266,6 +268,7 @@ TEST(UidProcStatsCollectorTest, TestHandlesPidTidReuse) {
perProcessStatus, perThreadStat));
UidProcStatsCollector collector(firstSnapshot.path);
+ collector.init();
ASSERT_TRUE(collector.enabled())
<< "Files under the path `" << firstSnapshot.path << "` are inaccessible";
@@ -358,6 +361,7 @@ TEST(UidProcStatsCollectorTest, TestErrorOnCorruptedProcessStatFile) {
perThreadStat));
UidProcStatsCollector collector(procDir.path);
+ collector.init();
ASSERT_TRUE(collector.enabled())
<< "Files under the path `" << procDir.path << "` are inaccessible";
@@ -386,6 +390,7 @@ TEST(UidProcStatsCollectorTest, TestErrorOnCorruptedProcessStatusFile) {
perThreadStat));
UidProcStatsCollector collector(procDir.path);
+ collector.init();
ASSERT_TRUE(collector.enabled())
<< "Files under the path `" << procDir.path << "` are inaccessible";
@@ -415,6 +420,7 @@ TEST(UidProcStatsCollectorTest, TestErrorOnCorruptedThreadStatFile) {
perThreadStat));
UidProcStatsCollector collector(procDir.path);
+ collector.init();
ASSERT_TRUE(collector.enabled())
<< "Files under the path `" << procDir.path << "` are inaccessible";
@@ -451,6 +457,7 @@ TEST(UidProcStatsCollectorTest, TestHandlesSpaceInCommName) {
perThreadStat));
UidProcStatsCollector collector(procDir.path);
+ collector.init();
ASSERT_TRUE(collector.enabled())
<< "Files under the path `" << procDir.path << "` are inaccessible";
@@ -466,6 +473,8 @@ TEST(UidProcStatsCollectorTest, TestHandlesSpaceInCommName) {
TEST(UidProcStatsCollectorTest, TestUidProcStatsCollectorContentsFromDevice) {
UidProcStatsCollector collector;
+ collector.init();
+
ASSERT_TRUE(collector.enabled()) << "/proc/[pid]/.* files are inaccessible";
ASSERT_RESULT_OK(collector.collect());
diff --git a/cpp/watchdog/server/tests/UidStatsCollectorTest.cpp b/cpp/watchdog/server/tests/UidStatsCollectorTest.cpp
index 7d3bcb121c..225e1a2ef1 100644
--- a/cpp/watchdog/server/tests/UidStatsCollectorTest.cpp
+++ b/cpp/watchdog/server/tests/UidStatsCollectorTest.cpp
@@ -186,7 +186,17 @@ protected:
sp<MockUidProcStatsCollector> mMockUidProcStatsCollector;
};
+TEST_F(UidStatsCollectorTest, TestInit) {
+ EXPECT_CALL(*mMockUidIoStatsCollector, init()).Times(1);
+ EXPECT_CALL(*mMockUidProcStatsCollector, init()).Times(1);
+
+ mUidStatsCollector->init();
+}
+
TEST_F(UidStatsCollectorTest, TestCollect) {
+ EXPECT_CALL(*mMockUidIoStatsCollector, enabled()).WillOnce(Return(true));
+ EXPECT_CALL(*mMockUidProcStatsCollector, enabled()).WillOnce(Return(true));
+
EXPECT_CALL(*mMockUidIoStatsCollector, collect()).WillOnce(Return(Result<void>()));
EXPECT_CALL(*mMockUidProcStatsCollector, collect()).WillOnce(Return(Result<void>()));
diff --git a/cpp/watchdog/server/tests/WatchdogPerfServiceTest.cpp b/cpp/watchdog/server/tests/WatchdogPerfServiceTest.cpp
index 864d6d1749..bcc2eb491a 100644
--- a/cpp/watchdog/server/tests/WatchdogPerfServiceTest.cpp
+++ b/cpp/watchdog/server/tests/WatchdogPerfServiceTest.cpp
@@ -26,6 +26,7 @@
#include <WatchdogProperties.sysprop.h>
#include <android-base/file.h>
#include <gmock/gmock.h>
+#include <utils/RefBase.h>
#include <future> // NOLINT(build/c++11)
#include <queue>
@@ -36,6 +37,7 @@ namespace android {
namespace automotive {
namespace watchdog {
+using ::android::RefBase;
using ::android::sp;
using ::android::String16;
using ::android::wp;
@@ -43,6 +45,7 @@ using ::android::automotive::watchdog::testing::LooperStub;
using ::android::base::Error;
using ::android::base::Result;
using ::testing::_;
+using ::testing::Eq;
using ::testing::InSequence;
using ::testing::Mock;
using ::testing::NiceMock;
@@ -58,234 +61,264 @@ constexpr std::chrono::seconds kTestPeriodicMonitorInterval = 2s;
namespace internal {
-class WatchdogPerfServicePeer {
+class WatchdogPerfServicePeer : public RefBase {
public:
- explicit WatchdogPerfServicePeer(sp<WatchdogPerfService> service) : service(service) {}
+ explicit WatchdogPerfServicePeer(const sp<WatchdogPerfService>& service) : mService(service) {}
WatchdogPerfServicePeer() = delete;
- ~WatchdogPerfServicePeer() { service->terminate(); }
-
- void injectFakes() {
- looperStub = sp<LooperStub>::make();
- mockUidStatsCollector = sp<MockUidStatsCollector>::make();
- mockProcDiskStats = sp<NiceMock<MockProcDiskStats>>::make();
- mockProcStat = sp<NiceMock<MockProcStat>>::make();
- mockDataProcessor = sp<StrictMock<MockDataProcessor>>::make();
-
- {
- Mutex::Autolock lock(service->mMutex);
- service->mHandlerLooper = looperStub;
- service->mUidStatsCollector = mockUidStatsCollector;
- service->mProcDiskStats = mockProcDiskStats;
- service->mProcStat = mockProcStat;
- }
- EXPECT_CALL(*mockDataProcessor, init()).Times(1);
- ASSERT_RESULT_OK(service->registerDataProcessor(mockDataProcessor));
+
+ void init(const sp<LooperWrapper>& looper,
+ const sp<UidStatsCollectorInterface>& uidStatsCollector, const sp<ProcStat>& procStat,
+ const sp<IProcDiskStatsInterface>& procDiskStats) {
+ Mutex::Autolock lock(mService->mMutex);
+ mService->mHandlerLooper = looper;
+ mService->mUidStatsCollector = uidStatsCollector;
+ mService->mProcStat = procStat;
+ mService->mProcDiskStats = procDiskStats;
}
- Result<void> start() {
- if (auto ret = service->start(); !ret.ok()) {
- return ret;
- }
- Mutex::Autolock lock(service->mMutex);
- service->mBoottimeCollection.interval = kTestBoottimeCollectionInterval;
- service->mPeriodicCollection.interval = kTestPeriodicCollectionInterval;
- service->mPeriodicMonitor.interval = kTestPeriodicMonitorInterval;
- return {};
+ void updateIntervals() {
+ Mutex::Autolock lock(mService->mMutex);
+ mService->mBoottimeCollection.interval = kTestBoottimeCollectionInterval;
+ mService->mPeriodicCollection.interval = kTestPeriodicCollectionInterval;
+ mService->mPeriodicMonitor.interval = kTestPeriodicMonitorInterval;
}
EventType getCurrCollectionEvent() {
- Mutex::Autolock lock(service->mMutex);
- return service->mCurrCollectionEvent;
+ Mutex::Autolock lock(mService->mMutex);
+ return mService->mCurrCollectionEvent;
}
std::future<void> joinCollectionThread() {
return std::async([&]() {
- if (service->mCollectionThread.joinable()) {
- service->mCollectionThread.join();
+ if (mService->mCollectionThread.joinable()) {
+ mService->mCollectionThread.join();
}
});
}
- void verifyAndClearExpectations() {
- Mock::VerifyAndClearExpectations(mockUidStatsCollector.get());
- Mock::VerifyAndClearExpectations(mockProcStat.get());
- Mock::VerifyAndClearExpectations(mockDataProcessor.get());
- }
-
- sp<WatchdogPerfService> service;
- // Below fields are populated only on injectFakes.
- sp<LooperStub> looperStub;
- sp<MockUidStatsCollector> mockUidStatsCollector;
- sp<MockProcDiskStats> mockProcDiskStats;
- sp<MockProcStat> mockProcStat;
- sp<MockDataProcessor> mockDataProcessor;
+protected:
+ sp<WatchdogPerfService> mService;
};
} // namespace internal
namespace {
-void startPeriodicCollection(internal::WatchdogPerfServicePeer* servicePeer) {
- ASSERT_NO_FATAL_FAILURE(servicePeer->injectFakes());
+class WatchdogPerfServiceTest : public ::testing::Test {
+protected:
+ virtual void SetUp() {
+ mService = sp<WatchdogPerfService>::make();
+ mServicePeer = sp<internal::WatchdogPerfServicePeer>::make(mService);
+ mLooperStub = sp<LooperStub>::make();
+ mMockUidStatsCollector = sp<MockUidStatsCollector>::make();
+ mMockDataProcessor = sp<StrictMock<MockDataProcessor>>::make();
+ mMockProcDiskStats = sp<NiceMock<MockProcDiskStats>>::make();
+ mMockProcStat = sp<NiceMock<MockProcStat>>::make();
+ }
- ASSERT_RESULT_OK(servicePeer->start());
+ virtual void TearDown() {
+ if (auto event = mServicePeer->getCurrCollectionEvent();
+ event != EventType::INIT && event != EventType::TERMINATED) {
+ EXPECT_CALL(*mMockDataProcessor, terminate()).Times(1);
+ mService->terminate();
+ }
+ mService.clear();
+ mServicePeer.clear();
+ mLooperStub.clear();
+ mMockUidStatsCollector.clear();
+ mMockDataProcessor.clear();
+ mMockProcDiskStats.clear();
+ mMockProcStat.clear();
+ }
- EXPECT_CALL(*servicePeer->mockUidStatsCollector, collect()).Times(2);
- EXPECT_CALL(*servicePeer->mockProcStat, collect()).Times(2);
- EXPECT_CALL(*servicePeer->mockDataProcessor,
- onBoottimeCollection(_,
- wp<UidStatsCollectorInterface>(
- servicePeer->mockUidStatsCollector),
- wp<ProcStat>(servicePeer->mockProcStat)))
- .Times(2);
+ void startService() {
+ mServicePeer->init(mLooperStub, mMockUidStatsCollector, mMockProcStat, mMockProcDiskStats);
- // Make sure the collection event changes from EventType::INIT to
- // EventType::BOOT_TIME_COLLECTION.
- ASSERT_RESULT_OK(servicePeer->looperStub->pollCache());
+ EXPECT_CALL(*mMockDataProcessor, init()).Times(1);
- // Mark boot complete.
- ASSERT_RESULT_OK(servicePeer->service->onBootFinished());
+ ASSERT_RESULT_OK(mService->registerDataProcessor(mMockDataProcessor));
- // Process |SwitchMessage::END_BOOTTIME_COLLECTION| and switch to periodic collection.
- ASSERT_RESULT_OK(servicePeer->looperStub->pollCache());
+ EXPECT_CALL(*mMockUidStatsCollector, init()).Times(1);
+ EXPECT_CALL(*mMockProcStat, init()).Times(1);
+ EXPECT_CALL(*mMockProcDiskStats, init()).Times(1);
- ASSERT_EQ(servicePeer->getCurrCollectionEvent(), EventType::PERIODIC_COLLECTION)
- << "Invalid collection event";
+ ASSERT_RESULT_OK(mService->start());
- servicePeer->verifyAndClearExpectations();
-}
+ mServicePeer->updateIntervals();
+ }
-void skipPeriodicMonitorEvents(internal::WatchdogPerfServicePeer* servicePeer) {
- EXPECT_CALL(*servicePeer->mockDataProcessor, onPeriodicMonitor(_, _, _)).Times(2);
- ASSERT_RESULT_OK(servicePeer->looperStub->pollCache());
- ASSERT_RESULT_OK(servicePeer->looperStub->pollCache());
-}
+ void startPeriodicCollection() {
+ EXPECT_CALL(*mMockUidStatsCollector, collect()).Times(2);
+ EXPECT_CALL(*mMockProcStat, collect()).Times(2);
+ EXPECT_CALL(*mMockDataProcessor,
+ onBoottimeCollection(_, Eq(mMockUidStatsCollector), Eq(mMockProcStat)))
+ .Times(2);
+
+ // Make sure the collection event changes from EventType::INIT to
+ // EventType::BOOT_TIME_COLLECTION.
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
+
+ // Mark boot complete.
+ ASSERT_RESULT_OK(mService->onBootFinished());
+
+ // Process |SwitchMessage::END_BOOTTIME_COLLECTION| and switch to periodic collection.
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
+
+ ASSERT_EQ(mServicePeer->getCurrCollectionEvent(), EventType::PERIODIC_COLLECTION)
+ << "Invalid collection event";
+
+ ASSERT_NO_FATAL_FAILURE(verifyAndClearExpectations());
+ }
+
+ void skipPeriodicMonitorEvents() {
+ EXPECT_CALL(*mMockDataProcessor, onPeriodicMonitor(_, _, _)).Times(2);
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
+ }
+
+ void verifyAndClearExpectations() {
+ Mock::VerifyAndClearExpectations(mMockUidStatsCollector.get());
+ Mock::VerifyAndClearExpectations(mMockProcStat.get());
+ Mock::VerifyAndClearExpectations(mMockProcDiskStats.get());
+ Mock::VerifyAndClearExpectations(mMockDataProcessor.get());
+ }
+
+ sp<WatchdogPerfService> mService;
+ sp<internal::WatchdogPerfServicePeer> mServicePeer;
+ sp<LooperStub> mLooperStub;
+ sp<MockUidStatsCollector> mMockUidStatsCollector;
+ sp<MockProcStat> mMockProcStat;
+ sp<MockProcDiskStats> mMockProcDiskStats;
+ sp<MockDataProcessor> mMockDataProcessor;
+};
} // namespace
-TEST(WatchdogPerfServiceTest, TestServiceStartAndTerminate) {
- sp<WatchdogPerfService> service = sp<WatchdogPerfService>::make();
- sp<MockDataProcessor> mockDataProcessor = sp<MockDataProcessor>::make();
+TEST_F(WatchdogPerfServiceTest, TestServiceStartAndTerminate) {
+ mServicePeer->init(mLooperStub, mMockUidStatsCollector, mMockProcStat, mMockProcDiskStats);
+
+ EXPECT_CALL(*mMockDataProcessor, init()).Times(1);
+
+ ASSERT_RESULT_OK(mService->registerDataProcessor(mMockDataProcessor));
+
+ EXPECT_CALL(*mMockUidStatsCollector, init()).Times(1);
+ EXPECT_CALL(*mMockProcStat, init()).Times(1);
+ EXPECT_CALL(*mMockProcDiskStats, init()).Times(1);
- EXPECT_CALL(*mockDataProcessor, init()).Times(1);
+ ASSERT_RESULT_OK(mService->start());
- ASSERT_RESULT_OK(service->registerDataProcessor(mockDataProcessor));
- ASSERT_RESULT_OK(service->start());
- ASSERT_TRUE(service->mCollectionThread.joinable()) << "Collection thread not created";
- ASSERT_FALSE(service->start().ok())
+ ASSERT_TRUE(mService->mCollectionThread.joinable()) << "Collection thread not created";
+
+ ASSERT_FALSE(mService->start().ok())
<< "No error returned when WatchdogPerfService was started more than once";
+
ASSERT_TRUE(sysprop::boottimeCollectionInterval().has_value());
ASSERT_EQ(std::chrono::duration_cast<std::chrono::seconds>(
- service->mBoottimeCollection.interval)
+ mService->mBoottimeCollection.interval)
.count(),
sysprop::boottimeCollectionInterval().value());
ASSERT_TRUE(sysprop::periodicCollectionInterval().has_value());
ASSERT_EQ(std::chrono::duration_cast<std::chrono::seconds>(
- service->mPeriodicCollection.interval)
+ mService->mPeriodicCollection.interval)
.count(),
sysprop::periodicCollectionInterval().value());
- service->terminate();
- ASSERT_FALSE(service->mCollectionThread.joinable()) << "Collection thread did not terminate";
-}
-
-TEST(WatchdogPerfServiceTest, TestValidCollectionSequence) {
- sp<WatchdogPerfService> service = sp<WatchdogPerfService>::make();
+ EXPECT_CALL(*mMockDataProcessor, terminate()).Times(1);
- internal::WatchdogPerfServicePeer servicePeer(service);
- ASSERT_NO_FATAL_FAILURE(servicePeer.injectFakes());
+ mService->terminate();
- ASSERT_RESULT_OK(servicePeer.start());
+ ASSERT_FALSE(mService->mCollectionThread.joinable()) << "Collection thread did not terminate";
+}
- wp<UidStatsCollectorInterface> uidStatsCollector(servicePeer.mockUidStatsCollector);
- wp<IProcDiskStatsInterface> procDiskStats(servicePeer.mockProcDiskStats);
- wp<ProcStat> procStat(servicePeer.mockProcStat);
+TEST_F(WatchdogPerfServiceTest, TestValidCollectionSequence) {
+ ASSERT_NO_FATAL_FAILURE(startService());
// #1 Boot-time collection
- EXPECT_CALL(*servicePeer.mockUidStatsCollector, collect()).Times(1);
- EXPECT_CALL(*servicePeer.mockProcStat, collect()).Times(1);
- EXPECT_CALL(*servicePeer.mockDataProcessor,
- onBoottimeCollection(_, uidStatsCollector, procStat))
+ EXPECT_CALL(*mMockUidStatsCollector, collect()).Times(1);
+ EXPECT_CALL(*mMockProcStat, collect()).Times(1);
+ EXPECT_CALL(*mMockDataProcessor,
+ onBoottimeCollection(_, Eq(mMockUidStatsCollector), Eq(mMockProcStat)))
.Times(1);
- ASSERT_RESULT_OK(servicePeer.looperStub->pollCache());
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
- ASSERT_EQ(servicePeer.looperStub->numSecondsElapsed(), 0)
+ ASSERT_EQ(mLooperStub->numSecondsElapsed(), 0)
<< "Boot-time collection didn't start immediately";
- ASSERT_EQ(servicePeer.getCurrCollectionEvent(), EventType::BOOT_TIME_COLLECTION)
+ ASSERT_EQ(mServicePeer->getCurrCollectionEvent(), EventType::BOOT_TIME_COLLECTION)
<< "Invalid collection event";
- servicePeer.verifyAndClearExpectations();
+ ASSERT_NO_FATAL_FAILURE(verifyAndClearExpectations());
// #2 Boot-time collection
- EXPECT_CALL(*servicePeer.mockUidStatsCollector, collect()).Times(1);
- EXPECT_CALL(*servicePeer.mockProcStat, collect()).Times(1);
- EXPECT_CALL(*servicePeer.mockDataProcessor,
- onBoottimeCollection(_, uidStatsCollector, procStat))
+ EXPECT_CALL(*mMockUidStatsCollector, collect()).Times(1);
+ EXPECT_CALL(*mMockProcStat, collect()).Times(1);
+ EXPECT_CALL(*mMockDataProcessor,
+ onBoottimeCollection(_, Eq(mMockUidStatsCollector), Eq(mMockProcStat)))
.Times(1);
- ASSERT_RESULT_OK(servicePeer.looperStub->pollCache());
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
- ASSERT_EQ(servicePeer.looperStub->numSecondsElapsed(), kTestBoottimeCollectionInterval.count())
+ ASSERT_EQ(mLooperStub->numSecondsElapsed(), kTestBoottimeCollectionInterval.count())
<< "Subsequent boot-time collection didn't happen at "
<< kTestBoottimeCollectionInterval.count() << " seconds interval";
- ASSERT_EQ(servicePeer.getCurrCollectionEvent(), EventType::BOOT_TIME_COLLECTION)
+ ASSERT_EQ(mServicePeer->getCurrCollectionEvent(), EventType::BOOT_TIME_COLLECTION)
<< "Invalid collection event";
- servicePeer.verifyAndClearExpectations();
+ ASSERT_NO_FATAL_FAILURE(verifyAndClearExpectations());
// #3 Last boot-time collection
- EXPECT_CALL(*servicePeer.mockUidStatsCollector, collect()).Times(1);
- EXPECT_CALL(*servicePeer.mockProcStat, collect()).Times(1);
- EXPECT_CALL(*servicePeer.mockDataProcessor,
- onBoottimeCollection(_, uidStatsCollector, procStat))
+ EXPECT_CALL(*mMockUidStatsCollector, collect()).Times(1);
+ EXPECT_CALL(*mMockProcStat, collect()).Times(1);
+ EXPECT_CALL(*mMockDataProcessor,
+ onBoottimeCollection(_, Eq(mMockUidStatsCollector), Eq(mMockProcStat)))
.Times(1);
- ASSERT_RESULT_OK(service->onBootFinished());
+ ASSERT_RESULT_OK(mService->onBootFinished());
- ASSERT_RESULT_OK(servicePeer.looperStub->pollCache());
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
- ASSERT_EQ(servicePeer.looperStub->numSecondsElapsed(), 0)
+ ASSERT_EQ(mLooperStub->numSecondsElapsed(), 0)
<< "Last boot-time collection didn't happen immediately after receiving boot complete "
<< "notification";
- ASSERT_EQ(servicePeer.getCurrCollectionEvent(), EventType::PERIODIC_COLLECTION)
+ ASSERT_EQ(mServicePeer->getCurrCollectionEvent(), EventType::PERIODIC_COLLECTION)
<< "Invalid collection event";
- servicePeer.verifyAndClearExpectations();
+ ASSERT_NO_FATAL_FAILURE(verifyAndClearExpectations());
// #4 Periodic monitor
- EXPECT_CALL(*servicePeer.mockProcDiskStats, collect()).Times(1);
- EXPECT_CALL(*servicePeer.mockDataProcessor, onPeriodicMonitor(_, procDiskStats, _)).Times(1);
+ EXPECT_CALL(*mMockProcDiskStats, collect()).Times(1);
+ EXPECT_CALL(*mMockDataProcessor, onPeriodicMonitor(_, Eq(mMockProcDiskStats), _)).Times(1);
- ASSERT_RESULT_OK(servicePeer.looperStub->pollCache());
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
- ASSERT_EQ(servicePeer.looperStub->numSecondsElapsed(), kTestPeriodicMonitorInterval.count())
+ ASSERT_EQ(mLooperStub->numSecondsElapsed(), kTestPeriodicMonitorInterval.count())
<< "First periodic monitor didn't happen at " << kTestPeriodicMonitorInterval.count()
<< " seconds interval";
- servicePeer.verifyAndClearExpectations();
+ ASSERT_NO_FATAL_FAILURE(verifyAndClearExpectations());
// #5 Periodic monitor
- EXPECT_CALL(*servicePeer.mockProcDiskStats, collect()).Times(1);
- EXPECT_CALL(*servicePeer.mockDataProcessor, onPeriodicMonitor(_, procDiskStats, _)).Times(1);
+ EXPECT_CALL(*mMockProcDiskStats, collect()).Times(1);
+ EXPECT_CALL(*mMockDataProcessor, onPeriodicMonitor(_, Eq(mMockProcDiskStats), _)).Times(1);
- ASSERT_RESULT_OK(servicePeer.looperStub->pollCache());
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
- ASSERT_EQ(servicePeer.looperStub->numSecondsElapsed(), kTestPeriodicMonitorInterval.count())
+ ASSERT_EQ(mLooperStub->numSecondsElapsed(), kTestPeriodicMonitorInterval.count())
<< "Second periodic monitor didn't happen at " << kTestPeriodicMonitorInterval.count()
<< " seconds interval";
- servicePeer.verifyAndClearExpectations();
+ ASSERT_NO_FATAL_FAILURE(verifyAndClearExpectations());
// #6 Periodic collection
- EXPECT_CALL(*servicePeer.mockUidStatsCollector, collect()).Times(1);
- EXPECT_CALL(*servicePeer.mockProcStat, collect()).Times(1);
- EXPECT_CALL(*servicePeer.mockDataProcessor,
- onPeriodicCollection(_, SystemState::NORMAL_MODE, uidStatsCollector, procStat))
+ EXPECT_CALL(*mMockUidStatsCollector, collect()).Times(1);
+ EXPECT_CALL(*mMockProcStat, collect()).Times(1);
+ EXPECT_CALL(*mMockDataProcessor,
+ onPeriodicCollection(_, SystemState::NORMAL_MODE, Eq(mMockUidStatsCollector),
+ Eq(mMockProcStat)))
.Times(1);
- ASSERT_RESULT_OK(servicePeer.looperStub->pollCache());
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
- ASSERT_EQ(servicePeer.looperStub->numSecondsElapsed(), 1)
+ ASSERT_EQ(mLooperStub->numSecondsElapsed(), 1)
<< "First periodic collection didn't happen at 1 second interval";
- ASSERT_EQ(servicePeer.getCurrCollectionEvent(), EventType::PERIODIC_COLLECTION)
+ ASSERT_EQ(mServicePeer->getCurrCollectionEvent(), EventType::PERIODIC_COLLECTION)
<< "Invalid collection event";
- servicePeer.verifyAndClearExpectations();
+ ASSERT_NO_FATAL_FAILURE(verifyAndClearExpectations());
// #7 Custom collection
Vector<String16> args;
@@ -294,158 +327,138 @@ TEST(WatchdogPerfServiceTest, TestValidCollectionSequence) {
args.push_back(String16(std::to_string(kTestCustomCollectionInterval.count()).c_str()));
args.push_back(String16(kMaxDurationFlag));
args.push_back(String16(std::to_string(kTestCustomCollectionDuration.count()).c_str()));
+ ASSERT_RESULT_OK(mService->onCustomCollection(-1, args));
- ASSERT_RESULT_OK(service->onCustomCollection(-1, args));
-
- EXPECT_CALL(*servicePeer.mockUidStatsCollector, collect()).Times(1);
- EXPECT_CALL(*servicePeer.mockProcStat, collect()).Times(1);
- EXPECT_CALL(*servicePeer.mockDataProcessor,
- onCustomCollection(_, SystemState::NORMAL_MODE, _, uidStatsCollector, procStat))
+ EXPECT_CALL(*mMockUidStatsCollector, collect()).Times(1);
+ EXPECT_CALL(*mMockProcStat, collect()).Times(1);
+ EXPECT_CALL(*mMockDataProcessor,
+ onCustomCollection(_, SystemState::NORMAL_MODE, _, Eq(mMockUidStatsCollector),
+ Eq(mMockProcStat)))
.Times(1);
- ASSERT_RESULT_OK(servicePeer.looperStub->pollCache());
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
- ASSERT_EQ(servicePeer.looperStub->numSecondsElapsed(), 0)
- << "Custom collection didn't start immediately";
- ASSERT_EQ(servicePeer.getCurrCollectionEvent(), EventType::CUSTOM_COLLECTION)
+ ASSERT_EQ(mLooperStub->numSecondsElapsed(), 0) << "Custom collection didn't start immediately";
+ ASSERT_EQ(mServicePeer->getCurrCollectionEvent(), EventType::CUSTOM_COLLECTION)
<< "Invalid collection event";
- servicePeer.verifyAndClearExpectations();
+ ASSERT_NO_FATAL_FAILURE(verifyAndClearExpectations());
// #8 Custom collection
- EXPECT_CALL(*servicePeer.mockUidStatsCollector, collect()).Times(1);
- EXPECT_CALL(*servicePeer.mockProcStat, collect()).Times(1);
- EXPECT_CALL(*servicePeer.mockDataProcessor,
- onCustomCollection(_, SystemState::NORMAL_MODE, _, uidStatsCollector, procStat))
+ EXPECT_CALL(*mMockUidStatsCollector, collect()).Times(1);
+ EXPECT_CALL(*mMockProcStat, collect()).Times(1);
+ EXPECT_CALL(*mMockDataProcessor,
+ onCustomCollection(_, SystemState::NORMAL_MODE, _, Eq(mMockUidStatsCollector),
+ Eq(mMockProcStat)))
.Times(1);
- ASSERT_RESULT_OK(servicePeer.looperStub->pollCache());
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
- ASSERT_EQ(servicePeer.looperStub->numSecondsElapsed(), kTestCustomCollectionInterval.count())
+ ASSERT_EQ(mLooperStub->numSecondsElapsed(), kTestCustomCollectionInterval.count())
<< "Subsequent custom collection didn't happen at "
<< kTestCustomCollectionInterval.count() << " seconds interval";
- ASSERT_EQ(servicePeer.getCurrCollectionEvent(), EventType::CUSTOM_COLLECTION)
+ ASSERT_EQ(mServicePeer->getCurrCollectionEvent(), EventType::CUSTOM_COLLECTION)
<< "Invalid collection event";
- servicePeer.verifyAndClearExpectations();
+ ASSERT_NO_FATAL_FAILURE(verifyAndClearExpectations());
// #9 End custom collection
TemporaryFile customDump;
{
InSequence s;
- EXPECT_CALL(*servicePeer.mockDataProcessor, onCustomCollectionDump(customDump.fd)).Times(1);
- EXPECT_CALL(*servicePeer.mockDataProcessor, onCustomCollectionDump(-1)).Times(1);
+ EXPECT_CALL(*mMockDataProcessor, onCustomCollectionDump(customDump.fd)).Times(1);
+ EXPECT_CALL(*mMockDataProcessor, onCustomCollectionDump(-1)).Times(1);
}
args.clear();
args.push_back(String16(kEndCustomCollectionFlag));
- ASSERT_RESULT_OK(service->onCustomCollection(customDump.fd, args));
- ASSERT_RESULT_OK(servicePeer.looperStub->pollCache());
- ASSERT_EQ(servicePeer.getCurrCollectionEvent(), EventType::PERIODIC_COLLECTION)
+ ASSERT_RESULT_OK(mService->onCustomCollection(customDump.fd, args));
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
+ ASSERT_EQ(mServicePeer->getCurrCollectionEvent(), EventType::PERIODIC_COLLECTION)
<< "Invalid collection event";
// #10 Switch to periodic collection
- EXPECT_CALL(*servicePeer.mockUidStatsCollector, collect()).Times(1);
- EXPECT_CALL(*servicePeer.mockProcStat, collect()).Times(1);
- EXPECT_CALL(*servicePeer.mockDataProcessor,
- onPeriodicCollection(_, SystemState::NORMAL_MODE, uidStatsCollector, procStat))
+ EXPECT_CALL(*mMockUidStatsCollector, collect()).Times(1);
+ EXPECT_CALL(*mMockProcStat, collect()).Times(1);
+ EXPECT_CALL(*mMockDataProcessor,
+ onPeriodicCollection(_, SystemState::NORMAL_MODE, Eq(mMockUidStatsCollector),
+ Eq(mMockProcStat)))
.Times(1);
- ASSERT_RESULT_OK(servicePeer.looperStub->pollCache());
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
- ASSERT_EQ(servicePeer.looperStub->numSecondsElapsed(), 0)
+ ASSERT_EQ(mLooperStub->numSecondsElapsed(), 0)
<< "Periodic collection didn't start immediately after ending custom collection";
- ASSERT_EQ(servicePeer.getCurrCollectionEvent(), EventType::PERIODIC_COLLECTION)
+ ASSERT_EQ(mServicePeer->getCurrCollectionEvent(), EventType::PERIODIC_COLLECTION)
<< "Invalid collection event";
- servicePeer.verifyAndClearExpectations();
+ ASSERT_NO_FATAL_FAILURE(verifyAndClearExpectations());
// #11 Periodic monitor.
- EXPECT_CALL(*servicePeer.mockProcDiskStats, collect()).Times(1);
- EXPECT_CALL(*servicePeer.mockDataProcessor, onPeriodicMonitor(_, procDiskStats, _)).Times(1);
+ EXPECT_CALL(*mMockProcDiskStats, collect()).Times(1);
+ EXPECT_CALL(*mMockDataProcessor, onPeriodicMonitor(_, Eq(mMockProcDiskStats), _)).Times(1);
- ASSERT_RESULT_OK(servicePeer.looperStub->pollCache());
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
- ASSERT_EQ(servicePeer.looperStub->numSecondsElapsed(), kTestPeriodicMonitorInterval.count());
- servicePeer.verifyAndClearExpectations();
+ ASSERT_EQ(mLooperStub->numSecondsElapsed(), kTestPeriodicMonitorInterval.count());
+ ASSERT_NO_FATAL_FAILURE(verifyAndClearExpectations());
- EXPECT_CALL(*servicePeer.mockDataProcessor, terminate()).Times(1);
+ EXPECT_CALL(*mMockDataProcessor, terminate()).Times(1);
}
-TEST(WatchdogPerfServiceTest, TestCollectionTerminatesOnZeroEnabledCollectors) {
- sp<WatchdogPerfService> service = sp<WatchdogPerfService>::make();
+TEST_F(WatchdogPerfServiceTest, TestCollectionTerminatesOnZeroEnabledCollectors) {
+ ASSERT_NO_FATAL_FAILURE(startService());
- internal::WatchdogPerfServicePeer servicePeer(service);
- ASSERT_NO_FATAL_FAILURE(servicePeer.injectFakes());
-
- ASSERT_RESULT_OK(servicePeer.start());
-
- ON_CALL(*servicePeer.mockUidStatsCollector, enabled()).WillByDefault(Return(false));
- ON_CALL(*servicePeer.mockProcStat, enabled()).WillByDefault(Return(false));
+ ON_CALL(*mMockUidStatsCollector, enabled()).WillByDefault(Return(false));
+ ON_CALL(*mMockProcStat, enabled()).WillByDefault(Return(false));
// Collection should terminate and call data processor's terminate method on error.
- EXPECT_CALL(*servicePeer.mockDataProcessor, terminate()).Times(1);
+ EXPECT_CALL(*mMockDataProcessor, terminate()).Times(1);
- ASSERT_RESULT_OK(servicePeer.looperStub->pollCache());
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
- ASSERT_EQ(servicePeer.joinCollectionThread().wait_for(1s), std::future_status::ready)
+ ASSERT_EQ(mServicePeer->joinCollectionThread().wait_for(1s), std::future_status::ready)
<< "Collection thread didn't terminate within 1 second.";
- ASSERT_EQ(servicePeer.getCurrCollectionEvent(), EventType::TERMINATED);
+ ASSERT_EQ(mServicePeer->getCurrCollectionEvent(), EventType::TERMINATED);
}
-TEST(WatchdogPerfServiceTest, TestCollectionTerminatesOnDataCollectorError) {
- sp<WatchdogPerfService> service = sp<WatchdogPerfService>::make();
-
- internal::WatchdogPerfServicePeer servicePeer(service);
- ASSERT_NO_FATAL_FAILURE(servicePeer.injectFakes());
-
- ASSERT_RESULT_OK(servicePeer.start());
+TEST_F(WatchdogPerfServiceTest, TestCollectionTerminatesOnDataCollectorError) {
+ ASSERT_NO_FATAL_FAILURE(startService());
// Inject data collector error.
Result<void> errorRes = Error() << "Failed to collect data";
- EXPECT_CALL(*servicePeer.mockUidStatsCollector, collect()).WillOnce(Return(errorRes));
+ EXPECT_CALL(*mMockUidStatsCollector, collect()).WillOnce(Return(errorRes));
// Collection should terminate and call data processor's terminate method on error.
- EXPECT_CALL(*servicePeer.mockDataProcessor, terminate()).Times(1);
+ EXPECT_CALL(*mMockDataProcessor, terminate()).Times(1);
- ASSERT_RESULT_OK(servicePeer.looperStub->pollCache());
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
- ASSERT_EQ(servicePeer.joinCollectionThread().wait_for(1s), std::future_status::ready)
+ ASSERT_EQ(mServicePeer->joinCollectionThread().wait_for(1s), std::future_status::ready)
<< "Collection thread didn't terminate within 1 second.";
- ASSERT_EQ(servicePeer.getCurrCollectionEvent(), EventType::TERMINATED);
+ ASSERT_EQ(mServicePeer->getCurrCollectionEvent(), EventType::TERMINATED);
}
-TEST(WatchdogPerfServiceTest, TestCollectionTerminatesOnDataProcessorError) {
- sp<WatchdogPerfService> service = sp<WatchdogPerfService>::make();
-
- internal::WatchdogPerfServicePeer servicePeer(service);
- ASSERT_NO_FATAL_FAILURE(servicePeer.injectFakes());
-
- EXPECT_CALL(*servicePeer.mockDataProcessor, name()).Times(1);
-
- ASSERT_RESULT_OK(servicePeer.start());
+TEST_F(WatchdogPerfServiceTest, TestCollectionTerminatesOnDataProcessorError) {
+ ASSERT_NO_FATAL_FAILURE(startService());
// Inject data processor error.
Result<void> errorRes = Error() << "Failed to process data";
- EXPECT_CALL(*servicePeer.mockDataProcessor,
- onBoottimeCollection(_,
- wp<UidStatsCollectorInterface>(
- servicePeer.mockUidStatsCollector),
- wp<ProcStat>(servicePeer.mockProcStat)))
+ EXPECT_CALL(*mMockDataProcessor,
+ onBoottimeCollection(_, Eq(mMockUidStatsCollector), Eq(mMockProcStat)))
.WillOnce(Return(errorRes));
// Collection should terminate and call data processor's terminate method on error.
- EXPECT_CALL(*servicePeer.mockDataProcessor, terminate()).Times(1);
+ EXPECT_CALL(*mMockDataProcessor, terminate()).Times(1);
- ASSERT_RESULT_OK(servicePeer.looperStub->pollCache());
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
- ASSERT_EQ(servicePeer.joinCollectionThread().wait_for(1s), std::future_status::ready)
+ ASSERT_EQ(mServicePeer->joinCollectionThread().wait_for(1s), std::future_status::ready)
<< "Collection thread didn't terminate within 1 second.";
- ASSERT_EQ(servicePeer.getCurrCollectionEvent(), EventType::TERMINATED);
+ ASSERT_EQ(mServicePeer->getCurrCollectionEvent(), EventType::TERMINATED);
}
-TEST(WatchdogPerfServiceTest, TestCustomCollection) {
- sp<WatchdogPerfService> service = sp<WatchdogPerfService>::make();
- internal::WatchdogPerfServicePeer servicePeer(service);
+TEST_F(WatchdogPerfServiceTest, TestCustomCollection) {
+ ASSERT_NO_FATAL_FAILURE(startService());
- ASSERT_NO_FATAL_FAILURE(startPeriodicCollection(&servicePeer));
+ ASSERT_NO_FATAL_FAILURE(startPeriodicCollection());
// Start custom collection with filter packages option.
Vector<String16> args;
@@ -456,164 +469,155 @@ TEST(WatchdogPerfServiceTest, TestCustomCollection) {
args.push_back(String16(std::to_string(kTestCustomCollectionDuration.count()).c_str()));
args.push_back(String16(kFilterPackagesFlag));
args.push_back(String16("android.car.cts,system_server"));
-
- ASSERT_RESULT_OK(service->onCustomCollection(-1, args));
+ ASSERT_RESULT_OK(mService->onCustomCollection(-1, args));
// Poll until custom collection auto terminates.
int maxIterations = static_cast<int>(kTestCustomCollectionDuration.count() /
kTestCustomCollectionInterval.count());
for (int i = 0; i <= maxIterations; ++i) {
- EXPECT_CALL(*servicePeer.mockUidStatsCollector, collect()).Times(1);
- EXPECT_CALL(*servicePeer.mockProcStat, collect()).Times(1);
- EXPECT_CALL(*servicePeer.mockDataProcessor,
+ EXPECT_CALL(*mMockUidStatsCollector, collect()).Times(1);
+ EXPECT_CALL(*mMockProcStat, collect()).Times(1);
+ EXPECT_CALL(*mMockDataProcessor,
onCustomCollection(_, SystemState::NORMAL_MODE,
UnorderedElementsAreArray(
{"android.car.cts", "system_server"}),
- wp<UidStatsCollectorInterface>(
- servicePeer.mockUidStatsCollector),
- wp<ProcStat>(servicePeer.mockProcStat)))
+ Eq(mMockUidStatsCollector), Eq(mMockProcStat)))
.Times(1);
- ASSERT_RESULT_OK(servicePeer.looperStub->pollCache());
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
int secondsElapsed = (i == 0 ? 0 : kTestCustomCollectionInterval.count());
- ASSERT_EQ(servicePeer.looperStub->numSecondsElapsed(), secondsElapsed)
+ ASSERT_EQ(mLooperStub->numSecondsElapsed(), secondsElapsed)
<< "Custom collection didn't happen at " << secondsElapsed
<< " seconds interval in iteration " << i;
- ASSERT_EQ(servicePeer.getCurrCollectionEvent(), EventType::CUSTOM_COLLECTION)
+ ASSERT_EQ(mServicePeer->getCurrCollectionEvent(), EventType::CUSTOM_COLLECTION)
<< "Invalid collection event";
- servicePeer.verifyAndClearExpectations();
+ ASSERT_NO_FATAL_FAILURE(verifyAndClearExpectations());
}
- EXPECT_CALL(*servicePeer.mockDataProcessor, onCustomCollectionDump(-1)).Times(1);
+ EXPECT_CALL(*mMockDataProcessor, onCustomCollectionDump(-1)).Times(1);
// Next looper message was injected during startCustomCollection to end the custom collection
// after |kTestCustomCollectionDuration|. On processing this message, the custom collection
// should auto terminate.
- ASSERT_RESULT_OK(servicePeer.looperStub->pollCache());
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
- ASSERT_EQ(servicePeer.looperStub->numSecondsElapsed(),
+ ASSERT_EQ(mLooperStub->numSecondsElapsed(),
kTestCustomCollectionDuration.count() % kTestCustomCollectionInterval.count())
<< "Custom collection did't end after " << kTestCustomCollectionDuration.count()
<< " seconds";
- ASSERT_EQ(servicePeer.getCurrCollectionEvent(), EventType::PERIODIC_COLLECTION)
+ ASSERT_EQ(mServicePeer->getCurrCollectionEvent(), EventType::PERIODIC_COLLECTION)
<< "Invalid collection event";
- EXPECT_CALL(*servicePeer.mockDataProcessor, terminate()).Times(1);
+ EXPECT_CALL(*mMockDataProcessor, terminate()).Times(1);
}
-TEST(WatchdogPerfServiceTest, TestPeriodicMonitorRequestsCollection) {
- sp<WatchdogPerfService> service = sp<WatchdogPerfService>::make();
- internal::WatchdogPerfServicePeer servicePeer(service);
-
- ASSERT_NO_FATAL_FAILURE(startPeriodicCollection(&servicePeer));
+TEST_F(WatchdogPerfServiceTest, TestPeriodicMonitorRequestsCollection) {
+ ASSERT_NO_FATAL_FAILURE(startService());
- wp<UidStatsCollectorInterface> uidStatsCollector(servicePeer.mockUidStatsCollector);
- wp<IProcDiskStatsInterface> procDiskStats(servicePeer.mockProcDiskStats);
- wp<ProcStat> procStat(servicePeer.mockProcStat);
+ ASSERT_NO_FATAL_FAILURE(startPeriodicCollection());
// Periodic monitor issuing an alert to start new collection.
- EXPECT_CALL(*servicePeer.mockProcDiskStats, collect()).Times(1);
- EXPECT_CALL(*servicePeer.mockDataProcessor, onPeriodicMonitor(_, procDiskStats, _))
+ EXPECT_CALL(*mMockProcDiskStats, collect()).Times(1);
+ EXPECT_CALL(*mMockDataProcessor, onPeriodicMonitor(_, Eq(mMockProcDiskStats), _))
.WillOnce([&](auto, auto, const auto& alertHandler) -> Result<void> {
alertHandler();
return {};
});
- ASSERT_RESULT_OK(servicePeer.looperStub->pollCache());
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
- ASSERT_EQ(servicePeer.looperStub->numSecondsElapsed(), kTestPeriodicMonitorInterval.count())
+ ASSERT_EQ(mLooperStub->numSecondsElapsed(), kTestPeriodicMonitorInterval.count())
<< "First periodic monitor didn't happen at " << kTestPeriodicMonitorInterval.count()
<< " seconds interval";
- servicePeer.verifyAndClearExpectations();
+ ASSERT_NO_FATAL_FAILURE(verifyAndClearExpectations());
- EXPECT_CALL(*servicePeer.mockUidStatsCollector, collect()).Times(1);
- EXPECT_CALL(*servicePeer.mockProcStat, collect()).Times(1);
- EXPECT_CALL(*servicePeer.mockDataProcessor,
- onPeriodicCollection(_, SystemState::NORMAL_MODE, uidStatsCollector, procStat))
+ EXPECT_CALL(*mMockUidStatsCollector, collect()).Times(1);
+ EXPECT_CALL(*mMockProcStat, collect()).Times(1);
+ EXPECT_CALL(*mMockDataProcessor,
+ onPeriodicCollection(_, SystemState::NORMAL_MODE, Eq(mMockUidStatsCollector),
+ Eq(mMockProcStat)))
.Times(1);
- ASSERT_RESULT_OK(servicePeer.looperStub->pollCache());
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
- ASSERT_EQ(servicePeer.looperStub->numSecondsElapsed(), 0)
+ ASSERT_EQ(mLooperStub->numSecondsElapsed(), 0)
<< "First periodic collection didn't happen immediately after the alert";
- servicePeer.verifyAndClearExpectations();
+ ASSERT_NO_FATAL_FAILURE(verifyAndClearExpectations());
- EXPECT_CALL(*servicePeer.mockDataProcessor, terminate()).Times(1);
+ EXPECT_CALL(*mMockDataProcessor, terminate()).Times(1);
}
-TEST(WatchdogPerfServiceTest, TestSystemStateSwitch) {
- sp<WatchdogPerfService> service = sp<WatchdogPerfService>::make();
- internal::WatchdogPerfServicePeer servicePeer(service);
+TEST_F(WatchdogPerfServiceTest, TestSystemStateSwitch) {
+ ASSERT_NO_FATAL_FAILURE(startService());
- ASSERT_NO_FATAL_FAILURE(startPeriodicCollection(&servicePeer));
- ASSERT_NO_FATAL_FAILURE(skipPeriodicMonitorEvents(&servicePeer));
+ ASSERT_NO_FATAL_FAILURE(startPeriodicCollection());
+ ASSERT_NO_FATAL_FAILURE(skipPeriodicMonitorEvents());
- EXPECT_CALL(*servicePeer.mockDataProcessor,
- onPeriodicCollection(_, SystemState::NORMAL_MODE, _, _))
+ EXPECT_CALL(*mMockDataProcessor, onPeriodicCollection(_, SystemState::NORMAL_MODE, _, _))
.Times(1);
- ASSERT_RESULT_OK(servicePeer.looperStub->pollCache());
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
- servicePeer.verifyAndClearExpectations();
+ ASSERT_NO_FATAL_FAILURE(verifyAndClearExpectations());
- ASSERT_NO_FATAL_FAILURE(skipPeriodicMonitorEvents(&servicePeer));
+ ASSERT_NO_FATAL_FAILURE(skipPeriodicMonitorEvents());
- service->setSystemState(SystemState::GARAGE_MODE);
+ mService->setSystemState(SystemState::GARAGE_MODE);
- EXPECT_CALL(*servicePeer.mockDataProcessor,
- onPeriodicCollection(_, SystemState::GARAGE_MODE, _, _))
+ EXPECT_CALL(*mMockDataProcessor, onPeriodicCollection(_, SystemState::GARAGE_MODE, _, _))
.Times(1);
- ASSERT_RESULT_OK(servicePeer.looperStub->pollCache());
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
- servicePeer.verifyAndClearExpectations();
+ ASSERT_NO_FATAL_FAILURE(verifyAndClearExpectations());
- ASSERT_NO_FATAL_FAILURE(skipPeriodicMonitorEvents(&servicePeer));
+ ASSERT_NO_FATAL_FAILURE(skipPeriodicMonitorEvents());
- service->setSystemState(SystemState::NORMAL_MODE);
+ mService->setSystemState(SystemState::NORMAL_MODE);
- EXPECT_CALL(*servicePeer.mockDataProcessor,
- onPeriodicCollection(_, SystemState::NORMAL_MODE, _, _))
+ EXPECT_CALL(*mMockDataProcessor, onPeriodicCollection(_, SystemState::NORMAL_MODE, _, _))
.Times(1);
- ASSERT_RESULT_OK(servicePeer.looperStub->pollCache());
+ ASSERT_RESULT_OK(mLooperStub->pollCache());
- servicePeer.verifyAndClearExpectations();
+ ASSERT_NO_FATAL_FAILURE(verifyAndClearExpectations());
- EXPECT_CALL(*servicePeer.mockDataProcessor, terminate()).Times(1);
+ EXPECT_CALL(*mMockDataProcessor, terminate()).Times(1);
}
-TEST(WatchdogPerfServiceTest, TestHandlesInvalidDumpArguments) {
- sp<WatchdogPerfService> service = sp<WatchdogPerfService>::make();
+TEST_F(WatchdogPerfServiceTest, TestHandlesInvalidDumpArguments) {
+ ASSERT_NO_FATAL_FAILURE(startService());
+
+ ASSERT_NO_FATAL_FAILURE(startPeriodicCollection());
+
Vector<String16> args;
args.push_back(String16(kStartCustomCollectionFlag));
args.push_back(String16("Invalid flag"));
args.push_back(String16("Invalid value"));
- ASSERT_FALSE(service->onCustomCollection(-1, args).ok());
+ ASSERT_FALSE(mService->onCustomCollection(-1, args).ok());
args.clear();
args.push_back(String16(kStartCustomCollectionFlag));
args.push_back(String16(kIntervalFlag));
args.push_back(String16("Invalid interval"));
- ASSERT_FALSE(service->onCustomCollection(-1, args).ok());
+ ASSERT_FALSE(mService->onCustomCollection(-1, args).ok());
args.clear();
args.push_back(String16(kStartCustomCollectionFlag));
args.push_back(String16(kMaxDurationFlag));
args.push_back(String16("Invalid duration"));
- ASSERT_FALSE(service->onCustomCollection(-1, args).ok());
+ ASSERT_FALSE(mService->onCustomCollection(-1, args).ok());
args.clear();
args.push_back(String16(kEndCustomCollectionFlag));
args.push_back(String16(kMaxDurationFlag));
args.push_back(String16(std::to_string(kTestCustomCollectionDuration.count()).c_str()));
- ASSERT_FALSE(service->onCustomCollection(-1, args).ok());
+ ASSERT_FALSE(mService->onCustomCollection(-1, args).ok());
args.clear();
args.push_back(String16("Invalid flag"));
- ASSERT_FALSE(service->onCustomCollection(-1, args).ok());
- service->terminate();
+ ASSERT_FALSE(mService->onCustomCollection(-1, args).ok());
}
} // namespace watchdog